From 5f6de83981187a41e296e7a7fa95ce3869f1b9ac Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Sat, 1 Oct 2022 11:29:34 -0700 Subject: [PATCH] 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 19378675f1ced79951d18e865b67c50cb5d09f75) --- common/libeval/numeric_evaluator.cpp | 9 ++++----- include/libeval/numeric_evaluator.h | 7 +++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/common/libeval/numeric_evaluator.cpp b/common/libeval/numeric_evaluator.cpp index 8e81bcdde4..db63105321 100644 --- a/common/libeval/numeric_evaluator.cpp +++ b/common/libeval/numeric_evaluator.cpp @@ -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( malloc( TokenStat::OutLen + 1 ) ); - strcpy( m_token.token, "0" ); m_token.inputLen = aString.length(); + m_token.outputLen = std::max( 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; } diff --git a/include/libeval/numeric_evaluator.h b/include/libeval/numeric_evaluator.h index bf2a044f9d..91bac4a4a6 100644 --- a/include/libeval/numeric_evaluator.h +++ b/include/libeval/numeric_evaluator.h @@ -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;