From: Jo Sutton Date: Tue, 20 Feb 2024 03:30:15 +0000 (+1300) Subject: lib:util: Correctly determine whether a character needs to be escaped X-Git-Tag: tdb-1.4.11~1678 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7e855f8d89cb0b3491d31c93da608417c38c6b00;p=thirdparty%2Fsamba.git lib:util: Correctly determine whether a character needs to be escaped The condition ‘c > 0x1F’ is clearly meant to test whether a character is a control code or not. While it works for ASCII characters, when ‘char’ is signed it fails for codepoints above 0x7f, which get represented as negative values. Make this calculation work as it was (presumably) intended by casting to ‘unsigned char’. Signed-off-by: Jo Sutton Reviewed-by: Andrew Bartlett --- diff --git a/lib/util/util_str_escape.c b/lib/util/util_str_escape.c index 750d64bfa57..b1f4162224e 100644 --- a/lib/util/util_str_escape.c +++ b/lib/util/util_str_escape.c @@ -26,7 +26,7 @@ * Calculate the encoded length of a character for log_escape * */ -static size_t encoded_length(char c) +static size_t encoded_length(unsigned char c) { if (c != '\\' && c > 0x1F) { return 1; @@ -79,7 +79,7 @@ char *log_escape(TALLOC_CTX *frame, const char *in) c = in; e = encoded; while (*c) { - if (*c != '\\' && *c > 0x1F) { + if (*c != '\\' && (unsigned char)(*c) > 0x1F) { *e++ = *c++; } else { switch (*c) {