lib:util: Fix printing hex‐escaped characters
authorJo Sutton <josutton@catalyst.net.nz>
Tue, 20 Feb 2024 03:35:43 +0000 (16:35 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 27 Feb 2024 01:11:37 +0000 (01:11 +0000)
A signed char, passed to snprintf(), will be promoted to an ‘int’, and
then interpreted (according to the format string) as an ‘unsigned int’.
Any negative values passed in will thus be interpreted as large unsigned
values, too large to be represented in the two characters allocated for
them. In practice, they will always be represented as ‘\xFF’.

Cast these characters to ‘unsigned char’, and use the appropriate length
modifier for that type.

Signed-off-by: Jo Sutton <josutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/util/util_str_escape.c

index b1f4162224ecacff0d2e7fd4edf4daac1b164de7..8f1f34912ee6929011e468713f8043235da7ea5f 100644 (file)
@@ -116,7 +116,7 @@ char *log_escape(TALLOC_CTX *frame, const char *in)
                                *e++ = '\\';
                                break;
                        default:
-                               snprintf(e, 5, "\\x%02X", *c);
+                               snprintf(e, 5, "\\x%02hhX", (unsigned char)(*c));
                                e += 4;
                        }
                        c++;