From c4c8d65798c01bcb2bce8e204f719e9bbd7e7d8f Mon Sep 17 00:00:00 2001 From: sowle Date: Fri, 30 May 2025 14:49:31 +0200 Subject: [PATCH] fixed a rare issue with printing random control characters to the console, breaking it --- .../include/storages/portable_storage_from_json.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/contrib/epee/include/storages/portable_storage_from_json.h b/contrib/epee/include/storages/portable_storage_from_json.h index fa76cc9b..3b323697 100644 --- a/contrib/epee/include/storages/portable_storage_from_json.h +++ b/contrib/epee/include/storages/portable_storage_from_json.h @@ -34,7 +34,20 @@ namespace epee { namespace json { -#define CHECK_ISSPACE() if(!isspace(*it)){ ASSERT_MES_AND_THROW("Wrong JSON character at: " << std::string(it, buf_end));} + + namespace details + { + inline bool report_wrong_char_with_context(const std::string::const_iterator begin, const std::string::const_iterator it, const std::string::const_iterator end) + { + auto before_it = it - std::min(it - begin, static_cast(8)); + auto after_it = it + std::min(end - it, static_cast(8)); + std::string escaped_str(before_it, after_it); + std::replace_if(escaped_str.begin(), escaped_str.end(), [](int c){ return std::isprint(c); }, '?'); + ASSERT_MES_AND_THROW("Wrong JSON character 0x" << std::hex << (int)*it << ", context: " << escaped_str); + } + } + +#define CHECK_ISSPACE() if (!isspace(*it)) details::report_wrong_char_with_context(sec_buf_begin, it, buf_end) /*inline void parse_error() {