From 21cf218a9f1853ff16255c8dd141c5e4230b9fce Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Sun, 21 Oct 2018 20:10:41 +0700 Subject: [PATCH 1/4] Remove commented out string2ostream. --- src/util/warning.cpp | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/util/warning.cpp b/src/util/warning.cpp index 6184db880..a2ebade85 100644 --- a/src/util/warning.cpp +++ b/src/util/warning.cpp @@ -25,7 +25,6 @@ Revision History: #include "util/vector.h" #ifdef _WINDOWS -#define PRF sprintf_s #define VPRF vsprintf_s void STD_CALL myInvalidParameterHandler( @@ -54,7 +53,6 @@ void STD_CALL myInvalidParameterHandler( #else -#define PRF snprintf #define VPRF vsnprintf #define BEGIN_ERR_HANDLER() {} #define END_ERR_HANDLER() {} @@ -86,23 +84,6 @@ void disable_error_msg_prefix() { g_show_error_msg_prefix = false; } -#if 0 -// [Leo]: Do we need this? -static void string2ostream(std::ostream& out, char const* msg) { - svector buff; - buff.resize(10); - BEGIN_ERR_HANDLER(); - while (true) { - int nc = PRF(buff.c_ptr(), buff.size(), msg); - if (nc >= 0 && nc < static_cast(buff.size())) - break; // success - buff.resize(buff.size()*2 + 1); - } - END_ERR_HANDLER(); - out << buff.c_ptr(); -} -#endif - void format2ostream(std::ostream & out, char const* msg, va_list args) { svector buff; #if !defined(_WINDOWS) && defined(_AMD64_) From 129353542cde46e4591827c858f5a3ff144c8768 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Sun, 21 Oct 2018 19:28:31 +0700 Subject: [PATCH 2/4] Improve format2ostream. Instead of looping to find a big enough buffer, we can call the correct function to calculate it, remembering to add an extra character for NUL termination. We also correctly do a va_copy of the args to avoid crashes on some platforms. --- src/util/warning.cpp | 46 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/src/util/warning.cpp b/src/util/warning.cpp index a2ebade85..d4de28782 100644 --- a/src/util/warning.cpp +++ b/src/util/warning.cpp @@ -16,8 +16,8 @@ Author: Revision History: --*/ -#include -#include +#include +#include #include "util/error_codes.h" #include "util/util.h" @@ -86,38 +86,22 @@ void disable_error_msg_prefix() { void format2ostream(std::ostream & out, char const* msg, va_list args) { svector buff; -#if !defined(_WINDOWS) && defined(_AMD64_) - // see comment below. - buff.resize(1024); -#else - buff.resize(128); -#endif BEGIN_ERR_HANDLER(); - while (true) { - int nc = VPRF(buff.c_ptr(), buff.size(), msg, args); -#if !defined(_WINDOWS) && defined(_AMD64_) - // For some strange reason, on Linux 64-bit version, va_list args is reset by vsnprintf. - // Z3 crashes when trying to use va_list args again. - // Hack: I truncate the message instead of expanding the buffer to make sure that - // va_list args is only used once. - END_ERR_HANDLER(); - if (nc < 0) { - // vsnprintf didn't work, so we just print the msg - out << msg; - return; - } - if (nc >= static_cast(buff.size())) { - // truncate the message - buff[buff.size() - 1] = 0; - } - out << buff.c_ptr(); - return; + + va_list args_copy; + va_copy(args_copy, args); +#ifdef _WINDOWS + size_t msg_len = _vscprintf(msg, args_copy); #else - if (nc >= 0 && nc < static_cast(buff.size())) - break; // success - buff.resize(buff.size()*2 + 1); + size_t msg_len = vsnprintf(NULL, 0, msg, args_copy); #endif - } + va_end(args_copy); + + // +1 is for NUL termination. + buff.resize(msg_len + 1); + + VPRF(buff.c_ptr(), buff.size(), msg, args); + END_ERR_HANDLER(); out << buff.c_ptr(); } From a73cf590db666f0f6adea1a42e96d58fa084e8cc Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Sun, 21 Oct 2018 20:29:01 +0700 Subject: [PATCH 3/4] Remove disable_error_msg_prefix. This wasn't used or actually implemented to do anything. --- src/util/warning.cpp | 5 ----- src/util/warning.h | 2 -- 2 files changed, 7 deletions(-) diff --git a/src/util/warning.cpp b/src/util/warning.cpp index d4de28782..10f18c21c 100644 --- a/src/util/warning.cpp +++ b/src/util/warning.cpp @@ -62,7 +62,6 @@ static bool g_warning_msgs = true; static bool g_use_std_stdout = false; static std::ostream* g_error_stream = nullptr; static std::ostream* g_warning_stream = nullptr; -static bool g_show_error_msg_prefix = true; void send_warnings_to_stdout(bool flag) { g_use_std_stdout = flag; @@ -80,10 +79,6 @@ void set_warning_stream(std::ostream* strm) { g_warning_stream = strm; } -void disable_error_msg_prefix() { - g_show_error_msg_prefix = false; -} - void format2ostream(std::ostream & out, char const* msg, va_list args) { svector buff; BEGIN_ERR_HANDLER(); diff --git a/src/util/warning.h b/src/util/warning.h index 15f1a1757..629c357fa 100644 --- a/src/util/warning.h +++ b/src/util/warning.h @@ -31,8 +31,6 @@ void set_warning_stream(std::ostream* strm); void warning_msg(const char * msg, ...); -void disable_error_msg_prefix(); - void format2ostream(std::ostream& out, char const* fmt, va_list args); class warning_displayer { From 7e35ce275a3d5860ba566d2391b7692abe58f82f Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Sun, 21 Oct 2018 20:30:07 +0700 Subject: [PATCH 4/4] Remove unused warning_displayer. --- src/util/warning.h | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/util/warning.h b/src/util/warning.h index 629c357fa..2bcc7512b 100644 --- a/src/util/warning.h +++ b/src/util/warning.h @@ -33,26 +33,5 @@ void warning_msg(const char * msg, ...); void format2ostream(std::ostream& out, char const* fmt, va_list args); -class warning_displayer { - const char * m_msg; - bool m_displayed; -public: - warning_displayer(const char * msg): - m_msg(msg), - m_displayed(false) { - } - - void sign() { - if (!m_displayed) { - warning_msg(m_msg); - m_displayed = true; - } - } - - void reset() { - m_displayed = false; - } -}; - #endif /* WARNING_H_ */