Fix broken comparison in pin_numbers

Pin names like "+V" were incorrectly parsed as doubles leading to broken
comparisons.  These caused heap overflows when sorting pin tables

This corrects the comparison so that numeric sorts are only performed
when there is an actual number in the symbol segment.  Also adds unit
tests for common error cases
This commit is contained in:
Seth Hillbrand 2022-04-19 14:43:32 -07:00
parent 4ab2c93070
commit aac6f576c2
5 changed files with 158 additions and 14 deletions

View File

@ -473,6 +473,9 @@ public:
void RebuildRows( const LIB_PINS& aPins, bool groupByName, bool groupBySelection )
{
WX_GRID* grid = dynamic_cast<WX_GRID*>( GetView() );
std::vector<LIB_PIN*> clear_flags;
clear_flags.reserve( aPins.size() );
if( grid )
{
@ -489,7 +492,10 @@ public:
for( int ii = 0; ii < selectedRowCount; ++ii )
{
for( LIB_PIN* pin : m_rows[ firstSelectedRow + ii ] )
{
pin->SetFlags( CANDIDATE );
clear_flags.push_back( pin );
}
}
}
@ -514,7 +520,7 @@ public:
if( groupByName )
rowIndex = findRow( m_rows, pin->GetName() );
else if( groupBySelection && pin->GetFlags() & CANDIDATE )
else if( groupBySelection && ( pin->GetFlags() & CANDIDATE ) )
rowIndex = 0;
if( rowIndex < 0 )
@ -550,6 +556,9 @@ public:
if( groupBySelection )
GetView()->SelectRow( 0 );
}
for( LIB_PIN* pin : clear_flags )
pin->ClearFlags( CANDIDATE );
}
void SortRows( int aSortCol, bool ascending )

View File

@ -26,9 +26,8 @@
#include <wx/crt.h>
#include <wx/translation.h>
namespace {
wxString GetNextSymbol( const wxString& str, wxString::size_type& cursor )
wxString PIN_NUMBERS::getNextSymbol( const wxString& str, wxString::size_type& cursor )
{
if( str.size() <= cursor )
return wxEmptyString;
@ -37,14 +36,15 @@ wxString GetNextSymbol( const wxString& str, wxString::size_type& cursor )
wxChar c = str[cursor];
if( wxIsdigit( c ) || c == '+' || c == '-' )
// Need to check that there is a digit in the string before we parse it as a numeric
if( ( wxIsdigit( c ) || ( ( c == '+' || c == '-' ) && ( cursor < str.size() - 1 ) && wxIsdigit( str[cursor + 1] ) ) ) )
{
// number, possibly with sign
while( ++cursor < str.size() )
{
c = str[cursor];
if( wxIsdigit( c ) || c == 'v' || c == 'V' )
if( wxIsdigit( c ) || c == 'v' || c == 'V' || c == '.' )
continue;
else
break;
@ -66,8 +66,6 @@ wxString GetNextSymbol( const wxString& str, wxString::size_type& cursor )
return str.substr( begin, cursor - begin );
}
}
wxString PIN_NUMBERS::GetSummary() const
{
@ -138,8 +136,8 @@ int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs )
for( ; ; )
{
symbol1 = GetNextSymbol( lhs, cursor1 );
symbol2 = GetNextSymbol( rhs, cursor2 );
symbol1 = getNextSymbol( lhs, cursor1 );
symbol2 = getNextSymbol( rhs, cursor2 );
if( symbol1.empty() && symbol2.empty() )
return 0;
@ -150,12 +148,12 @@ int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs )
if( symbol2.empty() )
return 2;
wxChar c1 = symbol1[0];
wxChar c2 = symbol2[0];
bool sym1_isnumeric = symbol1.find_first_of( "0123456789" ) != wxString::npos;
bool sym2_isnumeric = symbol2.find_first_of( "0123456789" ) != wxString::npos;
if( wxIsdigit( c1 ) || c1 == '-' || c1 == '+' )
if( sym1_isnumeric )
{
if( wxIsdigit( c2 ) || c2 == '-' || c2 == '+' )
if( sym2_isnumeric )
{
// numeric comparison
wxString::size_type v1 = symbol1.find_first_of( "vV" );
@ -194,7 +192,7 @@ int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs )
}
else
{
if( wxIsdigit( c2 ) || c2 == '-' || c2 == '+' )
if( sym2_isnumeric )
return 2;
int res = symbol1.Cmp( symbol2 );

View File

@ -53,6 +53,8 @@ private:
typedef std::set<wxString, less> container_type;
static wxString getNextSymbol( const wxString& str, wxString::size_type& cursor );
public:
typedef container_type::value_type value_type;
typedef container_type::iterator iterator;

View File

@ -57,6 +57,7 @@ set( QA_EESCHEMA_SRCS
test_lib_part.cpp
test_netlists.cpp
test_ee_item.cpp
test_pin_numbers.cpp
test_sch_pin.cpp
test_sch_rtree.cpp
test_sch_sheet.cpp

View File

@ -0,0 +1,134 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2022 KiCad Developers, see AUTHORS.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* https://www.gnu.org/licenses/gpl-3.0.html
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file test_pin_numbers.cpp
*
* Test suite for Pin number comparison functions.
*/
#include <qa_utils/wx_utils/unit_test_utils.h>
#include <pin_numbers.h>
#include <wx/string.h>
class TEST_PIN_NUMBERS
{
public:
TEST_PIN_NUMBERS()
{
}
};
struct TEST_PIN_NUMBER_CMP_CASE
{
wxString m_lhs;
wxString m_rhs;
int m_return;
};
/**
* Declare the test suite
*/
BOOST_FIXTURE_TEST_SUITE( SchInternalUnits, TEST_PIN_NUMBERS )
BOOST_AUTO_TEST_CASE( ComparePinNumbers )
{
const std::vector<TEST_PIN_NUMBER_CMP_CASE> cases = {
{
wxT( "+V" ),
wxT( "+V" ),
0,
},
{
wxT( "+V1234" ),
wxT( "+V" ),
2,
},
{
wxT( "+V" ),
wxT( "+V1234" ),
-2,
},
{
wxT( "Pin1" ),
wxT( "Pin2" ),
-1
},
{
wxT( "Pin2" ),
wxT( "Pin1" ),
1
},
{
wxT( "Pin1" ),
wxT( "Pin1" ),
0
},
{
wxT( "1Pin" ),
wxT( "2Pin" ),
-1
},
{
wxT( "2Pin" ),
wxT( "1Pin" ),
1
},
{
wxT( "1Pin" ),
wxT( "1Pin" ),
0
},
{
wxT( "+3V3" ),
wxT( "+3.3" ),
0
},
{
wxT( "+5V" ),
wxT( "+6V" ),
-1
},
{
wxT( "+6V" ),
wxT( "+5V" ),
1
}
};
for( auto el : cases )
{
int retval = PIN_NUMBERS::Compare( el.m_lhs, el.m_rhs );
wxString msg;
msg.Printf( "Comparing %s and %s failed [%d != %d]", el.m_lhs, el.m_rhs, retval, el.m_return );
BOOST_CHECK_MESSAGE( retval == el.m_return, msg.ToStdString() );
}
}
BOOST_AUTO_TEST_SUITE_END()