Ensure that token size is large enough

Input tokens can be arbitrary, so output needs to keep pace

Fixes https://gitlab.com/kicad/code/kicad/issues/12555

(cherry picked from commit 19378675f1)
This commit is contained in:
Seth Hillbrand 2022-10-01 11:29:34 -07:00
parent 02d38db577
commit 5f6de83981
2 changed files with 9 additions and 7 deletions

View File

@ -77,7 +77,7 @@ NUMERIC_EVALUATOR::~NUMERIC_EVALUATOR()
void NUMERIC_EVALUATOR::Clear()
{
free( m_token.token );
delete[] m_token.token;
m_token.token = nullptr;
m_token.input = nullptr;
m_parseError = true;
@ -103,7 +103,7 @@ void NUMERIC_EVALUATOR::parseSetResult( double val )
{
// Naively printing this with %g produces "nan" on some platforms
// and "-nan(ind)" on others (e.g. MSVC). So force a "standard" string.
snprintf( m_token.token, m_token.OutLen, "%s", "NaN" );
snprintf( m_token.token, m_token.outputLen, "%s", "NaN" );
}
else
{
@ -159,12 +159,11 @@ void NUMERIC_EVALUATOR::newString( const wxString& aString )
Clear();
m_originalText = aString;
m_token.token = reinterpret_cast<decltype( m_token.token )>( malloc( TokenStat::OutLen + 1 ) );
strcpy( m_token.token, "0" );
m_token.inputLen = aString.length();
m_token.outputLen = std::max<std::size_t>( 64, m_token.inputLen + 1 );
m_token.pos = 0;
m_token.input = aString.mb_str();
m_token.token = new char[m_token.outputLen]();
m_parseFinished = false;
}

View File

@ -159,11 +159,14 @@ private:
/* Token state for input string. */
struct TokenStat
{
enum { OutLen = 32 };
TokenStat() : input( nullptr ), token( nullptr ), inputLen( 0 ), pos( 0 ) { /* empty */ }
TokenStat() :
input( nullptr ), token( nullptr ), inputLen( 0 ), outputLen( 0 ), pos( 0 )
{ /* empty */ }
const char* input; // current input string ("var=4")
char* token; // output token ("var", type:VAR; "4", type:VALUE)
size_t inputLen; // strlen(input)
size_t outputLen; // At least 64, up to input length
size_t pos; // current index
}
m_token;