From 0292a5678a19cb3f3908cf3b267aa1f18b479aac Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 27 Oct 2023 08:45:50 -0700 Subject: [PATCH 01/11] maint: prefer c_isxdigit when that is the intent * src/digest.c (valid_digits, split_3): * src/echo.c (main): * src/printf.c (print_esc): * src/ptx.c (unescape_string): * src/stat.c (print_it): When the code is supposed to support only POSIX-locale hex digits, use c_isxdigit rather than isxdigit. Include c-ctype.h as needed. This defends against oddball locales where isxdigit != c_isxdigit. --- src/digest.c | 5 +++-- src/echo.c | 5 +++-- src/printf.c | 5 +++-- src/ptx.c | 3 ++- src/stat.c | 5 +++-- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/digest.c b/src/digest.c index b996dde11..1f3695308 100644 --- a/src/digest.c +++ b/src/digest.c @@ -23,6 +23,7 @@ #include "system.h" #include "argmatch.h" +#include "c-ctype.h" #include "quote.h" #include "xdectoint.h" #include "xstrtol.h" @@ -660,7 +661,7 @@ valid_digits (unsigned char const *s, size_t len) { for (idx_t i = 0; i < digest_hex_bytes; i++) { - if (!isxdigit (*s)) + if (!c_isxdigit (*s)) return false; ++s; } @@ -856,7 +857,7 @@ split_3 (char *s, size_t s_len, # endif unsigned char const *hp = *digest; digest_hex_bytes = 0; - while (isxdigit (*hp++)) + while (c_isxdigit (*hp++)) digest_hex_bytes++; if (digest_hex_bytes < 2 || digest_hex_bytes % 2 || BLAKE2B_MAX_LEN * 2 < digest_hex_bytes) diff --git a/src/echo.c b/src/echo.c index 278778ec6..f80ead86f 100644 --- a/src/echo.c +++ b/src/echo.c @@ -19,6 +19,7 @@ #include #include "system.h" #include "assure.h" +#include "c-ctype.h" /* The official name of this program (e.g., no 'g' prefix). */ #define PROGRAM_NAME "echo" @@ -219,12 +220,12 @@ just_echo: case 'x': { unsigned char ch = *s; - if (! isxdigit (ch)) + if (! c_isxdigit (ch)) goto not_an_escape; s++; c = hextobin (ch); ch = *s; - if (isxdigit (ch)) + if (c_isxdigit (ch)) { s++; c = c * 16 + hextobin (ch); diff --git a/src/printf.c b/src/printf.c index f36b45519..ebe09ba76 100644 --- a/src/printf.c +++ b/src/printf.c @@ -56,6 +56,7 @@ #include #include "system.h" +#include "c-ctype.h" #include "cl-strtod.h" #include "quote.h" #include "unicodeio.h" @@ -262,7 +263,7 @@ print_esc (char const *escstart, bool octal_0) { /* A hexadecimal \xhh escape sequence must have 1 or 2 hex. digits. */ for (esc_length = 0, ++p; - esc_length < 2 && isxdigit (to_uchar (*p)); + esc_length < 2 && c_isxdigit (to_uchar (*p)); ++esc_length, ++p) esc_value = esc_value * 16 + hextobin (*p); if (esc_length == 0) @@ -292,7 +293,7 @@ print_esc (char const *escstart, bool octal_0) esc_length > 0; --esc_length, ++p) { - if (! isxdigit (to_uchar (*p))) + if (! c_isxdigit (to_uchar (*p))) error (EXIT_FAILURE, 0, _("missing hexadecimal number in escape")); uni_value = uni_value * 16 + hextobin (*p); } diff --git a/src/ptx.c b/src/ptx.c index 3601875ed..3cd84b2e9 100644 --- a/src/ptx.c +++ b/src/ptx.c @@ -24,6 +24,7 @@ #include "system.h" #include #include "argmatch.h" +#include "c-ctype.h" #include "fadvise.h" #include "quote.h" #include "read-file.h" @@ -308,7 +309,7 @@ unescape_string (char *string) case 'x': /* \xhhh escape, 3 chars maximum */ value = 0; for (length = 0, string++; - length < 3 && isxdigit (to_uchar (*string)); + length < 3 && c_isxdigit (to_uchar (*string)); length++, string++) value = value * 16 + HEXTOBIN (*string); if (length == 0) diff --git a/src/stat.c b/src/stat.c index 39acfee70..522e922ed 100644 --- a/src/stat.c +++ b/src/stat.c @@ -58,6 +58,7 @@ #include "areadlink.h" #include "argmatch.h" +#include "c-ctype.h" #include "file-type.h" #include "filemode.h" #include "fs.h" @@ -1215,13 +1216,13 @@ print_it (char const *format, int fd, char const *filename, putchar (esc_value); --b; } - else if (*b == 'x' && isxdigit (to_uchar (b[1]))) + else if (*b == 'x' && c_isxdigit (to_uchar (b[1]))) { int esc_value = hextobin (b[1]); /* Value of \xhh escape. */ /* A hexadecimal \xhh escape sequence must have 1 or 2 hex. digits. */ ++b; - if (isxdigit (to_uchar (b[1]))) + if (c_isxdigit (to_uchar (b[1]))) { ++b; esc_value = esc_value * 16 + hextobin (*b); -- 2.39.2