From 6300e09966bf79b116eef379c5840b22108fb35c Mon Sep 17 00:00:00 2001 From: Abhay D <a.deshpande012@gmail.com> Date: Fri, 18 Mar 2022 23:20:18 -0700 Subject: [PATCH 1/2] Remove hardcoded line limit --- src/Arduino_DebugUtils.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Arduino_DebugUtils.cpp b/src/Arduino_DebugUtils.cpp index b1e3e75..327c44f 100644 --- a/src/Arduino_DebugUtils.cpp +++ b/src/Arduino_DebugUtils.cpp @@ -102,16 +102,27 @@ void Arduino_DebugUtils::print(int const debug_level, const __FlashStringHelper ******************************************************************************/ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) { - static size_t const MSG_BUF_SIZE = 120; - char msg_buf[MSG_BUF_SIZE] = {0}; + // calculate required buffer length + int msg_buf_size = vsnprintf(nullptr, 0, fmt, args) + 1; // add one for null terminator +#if __STDC_NO_VLA__ == 1 + // in the rare case where VLA is not allowed by compiler, fall back on heap-allocated memory + char * msg_buf = new char[msg_buf_size]; +#else + char msg_buf[msg_buf_size] = {0}; +#endif - vsnprintf(msg_buf, MSG_BUF_SIZE, fmt, args); + vsnprintf(msg_buf, msg_buf_size, fmt, args); if (_newline_on) { _debug_output_stream->println(msg_buf); } else { _debug_output_stream->print(msg_buf); } + +#if __STDC_NO_VLA__ == 1 + // remember to clean up memory + delete[] msg_buf; +#endif } void Arduino_DebugUtils::printTimestamp() From 0f3038ba721f0ecee722f20f7d6f9cbe76ce784f Mon Sep 17 00:00:00 2001 From: Abhay D <a.deshpande012@gmail.com> Date: Fri, 18 Mar 2022 23:44:03 -0700 Subject: [PATCH 2/2] Fix compiler error --- src/Arduino_DebugUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Arduino_DebugUtils.cpp b/src/Arduino_DebugUtils.cpp index 327c44f..7111715 100644 --- a/src/Arduino_DebugUtils.cpp +++ b/src/Arduino_DebugUtils.cpp @@ -108,7 +108,7 @@ void Arduino_DebugUtils::vPrint(char const * fmt, va_list args) { // in the rare case where VLA is not allowed by compiler, fall back on heap-allocated memory char * msg_buf = new char[msg_buf_size]; #else - char msg_buf[msg_buf_size] = {0}; + char msg_buf[msg_buf_size]; #endif vsnprintf(msg_buf, msg_buf_size, fmt, args);