]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
socket-util: split out checking valid character for ifname into ifname_valid_char()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 23 Jun 2021 06:57:28 +0000 (15:57 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 23 Jun 2021 06:59:44 +0000 (15:59 +0900)
src/basic/socket-util.c
src/basic/socket-util.h

index f6e751a09fdac694341a6814f0c6eb4e4f41b279..1c353e86b07797941ec89969aa1d678f64247894 100644 (file)
@@ -748,6 +748,22 @@ static const char* const ip_tos_table[] = {
 
 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
 
+bool ifname_valid_char(char a) {
+        if ((unsigned char) a >= 127U)
+                return false;
+
+        if ((unsigned char) a <= 32U)
+                return false;
+
+        if (IN_SET(a,
+                   ':',  /* colons are used by the legacy "alias" interface logic */
+                   '/',  /* slashes cannot work, since we need to use network interfaces in sysfs paths, and in paths slashes are separators */
+                   '%')) /* %d is used in the kernel's weird foo%d format string naming feature which we really really don't want to ever run into by accident */
+                return false;
+
+        return true;
+}
+
 bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
         bool numeric = true;
 
@@ -781,16 +797,7 @@ bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
                 return false;
 
         for (const char *t = p; *t; t++) {
-                if ((unsigned char) *t >= 127U)
-                        return false;
-
-                if ((unsigned char) *t <= 32U)
-                        return false;
-
-                if (IN_SET(*t,
-                           ':',  /* colons are used by the legacy "alias" interface logic */
-                           '/',  /* slashes cannot work, since we need to use network interfaces in sysfs paths, and in paths slashes are separators */
-                           '%')) /* %d is used in the kernel's weird foo%d format string naming feature which we really really don't want to ever run into by accident */
+                if (!ifname_valid_char(*t))
                         return false;
 
                 numeric = numeric && (*t >= '0' && *t <= '9');
index e0b959f5da550cc1bd60c430bc64bee179fe5561..f92e425fd68b738febfd9c57efa269cc09039fba 100644 (file)
@@ -139,6 +139,7 @@ typedef enum {
         IFNAME_VALID_NUMERIC     = 1 << 1,
         _IFNAME_VALID_ALL        = IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC,
 } IfnameValidFlags;
+bool ifname_valid_char(char a);
 bool ifname_valid_full(const char *p, IfnameValidFlags flags);
 static inline bool ifname_valid(const char *p) {
         return ifname_valid_full(p, 0);