From e25d937fd809017ed6e059b0211c04a625f4dc88 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Fri, 2 Sep 2022 00:47:53 +0100 Subject: [PATCH] Null-ptr safety for diff-pair-coupling test. Fixes https://gitlab.com/kicad/code/kicad/issues/12329 (cherry picked from commit 854acd4c772005366903c26ad20f3f03511ba3a0) --- .../drc_test_provider_diff_pair_coupling.cpp | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/pcbnew/drc/drc_test_provider_diff_pair_coupling.cpp b/pcbnew/drc/drc_test_provider_diff_pair_coupling.cpp index 5d2937e5af..63d73f239e 100644 --- a/pcbnew/drc/drc_test_provider_diff_pair_coupling.cpp +++ b/pcbnew/drc/drc_test_provider_diff_pair_coupling.cpp @@ -452,7 +452,7 @@ bool test::DRC_TEST_PROVIDER_DIFF_PAIR_COUPLING::Run() bool uncoupledViolation = false; - if( maxUncoupledConstraint ) + if( maxUncoupledConstraint && ( !it.second.itemsP.empty() || ! it.second.itemsN.empty() ) ) { auto val = maxUncoupledConstraint->GetValue(); @@ -468,23 +468,35 @@ bool test::DRC_TEST_PROVIDER_DIFF_PAIR_COUPLING::Run() drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); - auto pit = it.second.itemsP.begin(); - auto nit = it.second.itemsN.begin(); + BOARD_CONNECTED_ITEM* item = nullptr; + auto pit = it.second.itemsP.begin(); + auto nit = it.second.itemsN.begin(); - drce->AddItem( *pit ); - drce->AddItem( *nit ); - - for( pit++; pit != it.second.itemsP.end(); pit++ ) + if( pit != it.second.itemsP.end() ) + { + item = *pit; drce->AddItem( *pit ); + pit++; + } - for( nit++; nit != it.second.itemsN.end(); nit++ ) + if( nit != it.second.itemsN.end() ) + { + item = *nit; drce->AddItem( *nit ); + nit++; + } + + while( pit != it.second.itemsP.end() ) + drce->AddItem( *pit++ ); + + while( nit != it.second.itemsN.end() ) + drce->AddItem( *nit++ ); uncoupledViolation = true; drce->SetViolatingRule( maxUncoupledConstraint->GetParentRule() ); - reportViolation( drce, ( *it.second.itemsP.begin() )->GetPosition() ); + reportViolation( drce, item->GetPosition() ); } } @@ -492,7 +504,7 @@ bool test::DRC_TEST_PROVIDER_DIFF_PAIR_COUPLING::Run() { for( auto& cpair : it.second.coupled ) { - if( !cpair.couplingOK ) + if( !cpair.couplingOK && ( cpair.parentP || cpair.parentN ) ) { auto val = gapConstraint->GetValue(); auto drcItem = DRC_ITEM::Create( DRCE_DIFF_PAIR_GAP_OUT_OF_RANGE ); @@ -514,12 +526,23 @@ bool test::DRC_TEST_PROVIDER_DIFF_PAIR_COUPLING::Run() drcItem->SetErrorMessage( msg ); - drcItem->AddItem( cpair.parentP ); - drcItem->AddItem( cpair.parentN ); + BOARD_CONNECTED_ITEM* item = nullptr; + + if( cpair.parentP ) + { + item = cpair.parentP; + drcItem->AddItem( cpair.parentP ); + } + + if( cpair.parentN ) + { + item = cpair.parentN; + drcItem->AddItem( cpair.parentN ); + } drcItem->SetViolatingRule( gapConstraint->GetParentRule() ); - reportViolation( drcItem, cpair.parentP->GetPosition() ); + reportViolation( drcItem, item->GetPosition() ); } } }