]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: use DIGITS and friends
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 3 Mar 2026 13:34:41 +0000 (22:34 +0900)
committerMike Yuan <me@yhndnzj.com>
Tue, 3 Mar 2026 21:18:26 +0000 (22:18 +0100)
src/basic/hexdecoct.c
src/basic/user-util.c
src/boot/cpio.c
src/boot/efi-string.c
src/fundamental/string-util-fundamental.h
src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-json/sd-json.c
src/login/pam_systemd.c

index a00c6289cca02abeea13367f0be7bf80c829c804..bbe624cc4f2ed9edb72ff364123372ad8e1992d3 100644 (file)
@@ -33,8 +33,7 @@ int undecchar(char c) {
 }
 
 char hexchar(int x) {
-        static const char table[] = "0123456789abcdef";
-
+        const char *table = LOWERCASE_HEXDIGITS;
         return table[x & 15];
 }
 
@@ -165,9 +164,7 @@ int unhexmem_full(
  * useful when representing NSEC3 hashes, as one can then verify the
  * order of hashes directly from their representation. */
 char base32hexchar(int x) {
-        static const char table[] = "0123456789"
-                                    "ABCDEFGHIJKLMNOPQRSTUV";
-
+        const char *table = DIGITS "ABCDEFGHIJKLMNOPQRSTUV";
         return table[x & 31];
 }
 
@@ -516,9 +513,7 @@ int unbase32hexmem(const char *p, size_t l, bool padding, void **mem, size_t *_l
 
 /* https://tools.ietf.org/html/rfc4648#section-4 */
 char base64char(int x) {
-        static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-                                    "abcdefghijklmnopqrstuvwxyz"
-                                    "0123456789+/";
+        const char *table = UPPERCASE_LETTERS LOWERCASE_LETTERS DIGITS "+/";
         return table[x & 63];
 }
 
@@ -526,9 +521,7 @@ char base64char(int x) {
  * since we don't want "/" appear in interface names (since interfaces appear in sysfs as filenames).
  * See section #5 of RFC 4648. */
 char urlsafe_base64char(int x) {
-        static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-                                    "abcdefghijklmnopqrstuvwxyz"
-                                    "0123456789-_";
+        const char *table = UPPERCASE_LETTERS LOWERCASE_LETTERS DIGITS "-_";
         return table[x & 63];
 }
 
index e434fbec8f98508f73ea3001ab8d161806bcdcb2..a4ae020c2c6bc5203a58db73559c7c618f41953f 100644 (file)
@@ -762,18 +762,17 @@ bool valid_user_group_name(const char *u, ValidUserFlags flags) {
                                        * don't allow slashes. */
                         return false;
 
-                if (in_charset(u, "0123456789")) /* Don't allow fully numeric strings, they might be confused
-                                                  * with UIDs (note that this test is more broad than
-                                                  * the parse_uid() test above, as it will cover more than
-                                                  * the 32-bit range, and it will detect 65535 (which is in
-                                                  * invalid UID, even though in the unsigned 32 bit range) */
+                if (in_charset(u, DIGITS)) /* Don't allow fully numeric strings, they might be confused with
+                                            * UIDs (note that this test is more broad than the parse_uid()
+                                            * test above, as it will cover more than the 32-bit range, and it
+                                            * will detect 65535 (which is in invalid UID, even though in the
+                                            * unsigned 32 bit range) */
                         return false;
 
-                if (u[0] == '-' && in_charset(u + 1, "0123456789")) /* Don't allow negative fully numeric
-                                                                     * strings either. After all some people
-                                                                     * write 65535 as -1 (even though that's
-                                                                     * not even true on 32-bit uid_t
-                                                                     * anyway) */
+                if (u[0] == '-' && in_charset(u + 1, DIGITS)) /* Don't allow negative fully numeric strings
+                                                               * either. After all some people write 65535 as
+                                                               * -1 (even though that's not even true on
+                                                               * 32-bit uid_t anyway) */
                         return false;
 
                 if (dot_or_dot_dot(u)) /* User names typically become home directory names, and these two are
index 8a15253dedad16b5d723ca3965b48c172c6c6750..7ad4b470fae022846e160587741b46e324c7c9af 100644 (file)
@@ -8,7 +8,7 @@
 #include "util.h"
 
 static char *write_cpio_word(char *p, uint32_t v) {
-        static const char hex[] = "0123456789abcdef";
+        const char *hex = LOWERCASE_HEXDIGITS;
 
         assert(p);
 
index ee430c1b36a40bb1190b2dc627ea253b13594dd7..cde10d0abd4376c22d12edf66a0f903ca93c5f3e 100644 (file)
@@ -510,7 +510,7 @@ char* line_get_key_value(char *s, const char *sep, size_t *pos, char **ret_key,
 }
 
 char16_t *hexdump(const void *data, size_t size) {
-        static const char hex[] = "0123456789abcdef";
+        const char *hex = LOWERCASE_HEXDIGITS;
         const uint8_t *d = data;
 
         assert(data || size == 0);
@@ -676,7 +676,7 @@ static bool push_str(FormatContext *ctx, SpecifierContext *sp) {
 }
 
 static bool push_num(FormatContext *ctx, SpecifierContext *sp, uint64_t u) {
-        const char *digits = sp->lowercase ? "0123456789abcdef" : "0123456789ABCDEF";
+        const char *digits = sp->lowercase ? LOWERCASE_HEXDIGITS : UPPERCASE_HEXDIGITS;
         char16_t tmp[32];
         size_t n = 0;
 
index e2eb73a4a9dee76022829496a7de5aeb9be7984f..83b90e4e9e543b41bd490249d3a18245a054eab7 100644 (file)
@@ -24,6 +24,7 @@
 #define ALPHANUMERICAL      LETTERS DIGITS
 #define HEXDIGITS           DIGITS "abcdefABCDEF"
 #define LOWERCASE_HEXDIGITS DIGITS "abcdef"
+#define UPPERCASE_HEXDIGITS DIGITS "ABCDEF"
 #define URI_RESERVED        ":/?#[]@!$&'()*+;="         /* [RFC3986] */
 #define URI_UNRESERVED      ALPHANUMERICAL "-._~"       /* [RFC3986] */
 #define URI_VALID           URI_RESERVED URI_UNRESERVED /* [RFC3986] */
index 9fdd8cbd66bf7181cea091c6dee958e99402764e..2a3ea8eb8f35623939596adcc9cd44e3d10e7c18 100644 (file)
@@ -1480,7 +1480,7 @@ int bus_set_address_system_remote(sd_bus *b, const char *host) {
                         got_forward_slash = true;
                 }
 
-                if (!in_charset(p, "0123456789") || *p == '\0') {
+                if (!in_charset(p, DIGITS) || *p == '\0') {
                         if (!hostname_is_valid(p, 0) || got_forward_slash)
                                 return -EINVAL;
 
@@ -1496,7 +1496,7 @@ int bus_set_address_system_remote(sd_bus *b, const char *host) {
 interpret_port_as_machine_old_syntax:
                 /* Let's make sure this is not a port of some kind,
                  * and is a valid machine name. */
-                if (!in_charset(m, "0123456789") && hostname_is_valid(m, 0))
+                if (!in_charset(m, DIGITS) && hostname_is_valid(m, 0))
                         c = strjoina(",argv", p ? "7" : "5", "=--machine=", m);
         }
 
index 647b56555a7bbffba0b8db57ff4e81e8f2466268..5b6bc90c02c236f081ccbd772d8008d7d8f44e17 100644 (file)
@@ -2761,21 +2761,21 @@ static int json_parse_number(const char **p, JsonValue *ret) {
                         x = 10.0 * x + (*c - '0');
 
                         c++;
-                } while (strchr("0123456789", *c) && *c != 0);
+                } while (strchr(DIGITS, *c) && *c != 0);
         }
 
         if (*c == '.') {
                 is_real = true;
                 c++;
 
-                if (!strchr("0123456789", *c) || *c == 0)
+                if (!strchr(DIGITS, *c) || *c == 0)
                         return -EINVAL;
 
                 do {
                         y = 10.0 * y + (*c - '0');
                         shift = 10.0 * shift;
                         c++;
-                } while (strchr("0123456789", *c) && *c != 0);
+                } while (strchr(DIGITS, *c) && *c != 0);
         }
 
         if (IN_SET(*c, 'e', 'E')) {
@@ -2788,13 +2788,13 @@ static int json_parse_number(const char **p, JsonValue *ret) {
                 } else if (*c == '+')
                         c++;
 
-                if (!strchr("0123456789", *c) || *c == 0)
+                if (!strchr(DIGITS, *c) || *c == 0)
                         return -EINVAL;
 
                 do {
                         exponent = 10.0 * exponent + (*c - '0');
                         c++;
-                } while (strchr("0123456789", *c) && *c != 0);
+                } while (strchr(DIGITS, *c) && *c != 0);
         }
 
         *p = c;
@@ -2904,7 +2904,7 @@ int json_tokenize(
                         *state = INT_TO_PTR(STATE_VALUE_POST);
                         goto finish;
 
-                } else if (strchr("-0123456789", *c)) {
+                } else if (strchr("-" DIGITS, *c)) {
 
                         r = json_parse_number(&c, ret_value);
                         if (r < 0)
index cf8fe30ebeac15f398d045dff3b4c940b656732f..f7aa6f9b8f626ac8503b5087d15ac05921418c44 100644 (file)
@@ -279,7 +279,7 @@ static int socket_from_display(const char *display) {
         if (!display_is_local(display))
                 return -EINVAL;
 
-        k = strspn(display+1, "0123456789");
+        k = strspn(display + 1, DIGITS);
 
         /* Try abstract socket first. */
         f = new(char, STRLEN("@/tmp/.X11-unix/X") + k + 1);