]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib:util: Correctly determine whether a character needs to be escaped
authorJo Sutton <josutton@catalyst.net.nz>
Tue, 20 Feb 2024 03:30:15 +0000 (16:30 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 27 Feb 2024 01:11:37 +0000 (01:11 +0000)
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 <josutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/util/util_str_escape.c

index 750d64bfa570ed44828f6dc4e974c56b8f515511..b1f4162224ecacff0d2e7fd4edf4daac1b164de7 100644 (file)
@@ -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) {