Skip to content

Commit

Permalink
Fix escape_json() compilation error due to type limits check
Browse files Browse the repository at this point in the history
Compilation failed on arm64 with

  error: comparison is always true due to limited range of data type
  [-Werror=type-limits]

for
  if (0x0 <= *c && *c <= 0x1f)

This was because char is unsigned on arm64 and thus always greater
than zero.

Fixed by using std::iscntrl() instead of explicit range check.
This adds also backspace into set of escaped characters.
  • Loading branch information
temeo committed Jan 23, 2023
1 parent 275a0af commit 940ba9b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/reporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ static std::string escape_json(const std::string& str)
case '\r': os << "\\r"; break;
case '\t': os << "\\t"; break;
default:
if (0x0 <= *c && *c <= 0x1f)
// std::iscntrl() returns non-zero for [0x00, 0x1f] and
// backspace character. Argument type is int so cast to
// unsigned char is needed to make it safe, see
// https://en.cppreference.com/w/cpp/string/byte/iscntrl
if (std::iscntrl(static_cast<unsigned char>(*c)))
{
os << "\\u" << std::hex << std::setw(4) <<
std::setfill('0') << static_cast<int>(*c);
Expand Down

0 comments on commit 940ba9b

Please sign in to comment.