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
(cherry picked from commit aac6f576c2
)
This commit is contained in:
parent
cc51807241
commit
cf5f36aac5
|
@ -25,9 +25,8 @@
|
||||||
#include "pin_numbers.h"
|
#include "pin_numbers.h"
|
||||||
#include <wx/crt.h>
|
#include <wx/crt.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 )
|
if( str.size() <= cursor )
|
||||||
return wxEmptyString;
|
return wxEmptyString;
|
||||||
|
@ -36,14 +35,15 @@ wxString GetNextSymbol( const wxString& str, wxString::size_type& cursor )
|
||||||
|
|
||||||
wxChar c = str[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
|
// number, possibly with sign
|
||||||
while( ++cursor < str.size() )
|
while( ++cursor < str.size() )
|
||||||
{
|
{
|
||||||
c = str[cursor];
|
c = str[cursor];
|
||||||
|
|
||||||
if( wxIsdigit( c ) || c == 'v' || c == 'V' )
|
if( wxIsdigit( c ) || c == 'v' || c == 'V' || c == '.' )
|
||||||
continue;
|
continue;
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
|
@ -65,8 +65,6 @@ wxString GetNextSymbol( const wxString& str, wxString::size_type& cursor )
|
||||||
return str.substr( begin, cursor - begin );
|
return str.substr( begin, cursor - begin );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
wxString PIN_NUMBERS::GetSummary() const
|
wxString PIN_NUMBERS::GetSummary() const
|
||||||
{
|
{
|
||||||
|
@ -117,8 +115,8 @@ int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs )
|
||||||
|
|
||||||
for( ; ; )
|
for( ; ; )
|
||||||
{
|
{
|
||||||
symbol1 = GetNextSymbol( lhs, cursor1 );
|
symbol1 = getNextSymbol( lhs, cursor1 );
|
||||||
symbol2 = GetNextSymbol( rhs, cursor2 );
|
symbol2 = getNextSymbol( rhs, cursor2 );
|
||||||
|
|
||||||
if( symbol1.empty() && symbol2.empty() )
|
if( symbol1.empty() && symbol2.empty() )
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -129,12 +127,12 @@ int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs )
|
||||||
if( symbol2.empty() )
|
if( symbol2.empty() )
|
||||||
return 2;
|
return 2;
|
||||||
|
|
||||||
wxChar c1 = symbol1[0];
|
bool sym1_isnumeric = symbol1.find_first_of( "0123456789" ) != wxString::npos;
|
||||||
wxChar c2 = symbol2[0];
|
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
|
// numeric comparison
|
||||||
wxString::size_type v1 = symbol1.find_first_of( wxT( "vV" ) );
|
wxString::size_type v1 = symbol1.find_first_of( wxT( "vV" ) );
|
||||||
|
@ -173,7 +171,7 @@ int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs )
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if( wxIsdigit( c2 ) || c2 == '-' || c2 == '+' )
|
if( sym2_isnumeric )
|
||||||
return 2;
|
return 2;
|
||||||
|
|
||||||
int res = symbol1.Cmp( symbol2 );
|
int res = symbol1.Cmp( symbol2 );
|
||||||
|
|
|
@ -47,6 +47,8 @@ private:
|
||||||
|
|
||||||
typedef std::set<wxString, less> container_type;
|
typedef std::set<wxString, less> container_type;
|
||||||
|
|
||||||
|
static wxString getNextSymbol( const wxString& str, wxString::size_type& cursor );
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef container_type::value_type value_type;
|
typedef container_type::value_type value_type;
|
||||||
typedef container_type::iterator iterator;
|
typedef container_type::iterator iterator;
|
||||||
|
|
|
@ -55,6 +55,7 @@ set( QA_EESCHEMA_SRCS
|
||||||
test_lib_part.cpp
|
test_lib_part.cpp
|
||||||
test_netlists.cpp
|
test_netlists.cpp
|
||||||
test_ee_item.cpp
|
test_ee_item.cpp
|
||||||
|
test_pin_numbers.cpp
|
||||||
test_sch_pin.cpp
|
test_sch_pin.cpp
|
||||||
test_sch_rtree.cpp
|
test_sch_rtree.cpp
|
||||||
test_sch_sheet.cpp
|
test_sch_sheet.cpp
|
||||||
|
|
|
@ -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()
|
Loading…
Reference in New Issue