tech-net archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: ether_nonstatic_aton() and cdce(4)



On Mon, May 17, 2010 at 02:26:09AM +0000, Jonathan A. Kollasch wrote:
> I'd like to make ether_nonstatic_aton() much less strict;
> accepting 6 nibble pairs each optionally followed by a ispunct().

Proposed change in the patch attached.
(Sorry about the whitespace diff.)

It seems slightly rude to corrupt the caller's string.
Shouldn't the source string argument be const char *?

        Jonathan Kollasch
Index: sys/net/if_ethersubr.c
===================================================================
RCS file: /cvsroot/src/sys/net/if_ethersubr.c,v
retrieving revision 1.178
diff -u -r1.178 if_ethersubr.c
--- sys/net/if_ethersubr.c      5 May 2010 18:12:24 -0000       1.178
+++ sys/net/if_ethersubr.c      17 May 2010 15:02:28 -0000
@@ -1226,36 +1226,42 @@
 int
 ether_nonstatic_aton(u_char *dest, char *str)
 {
-        int i;
-        char *cp = str;
-        u_char val[6];
-
-#define set_value                       \
-        if (*cp > '9' && *cp < 'a')     \
-                *cp -= 'A' - 10;        \
-        else if (*cp > '9')             \
-                *cp -= 'a' - 10;        \
-        else                            \
-                *cp -= '0'
-
-        for (i = 0; i < 6; i++, cp++) {
-                if (!isxdigit(*cp))
-                        return (1);
-                set_value;
-                val[i] = *cp++;
-                if (isxdigit(*cp)) {
-                        set_value;
-                        val[i] *= 16;
-                        val[i] += *cp++;
-                }
-                if (*cp == ':' || i == 5)
-                        continue;
-                else
-                        return 1;
-        }
-        memcpy(dest, val, 6);
+       int i;
+       u_char val[ETHER_ADDR_LEN];
+       char *cp;
+
+#define set_value                      \
+       if (*cp > '9' && *cp < 'a')     \
+               *cp -= 'A' - 10;        \
+       else if (*cp > '9')             \
+               *cp -= 'a' - 10;        \
+       else                            \
+               *cp -= '0'
+
+       cp = str;
+
+       for (i = 0; i < ETHER_ADDR_LEN; i++) {
+               if (!isxdigit(*cp))
+                       return 1;
+               set_value;
+               val[i] = *cp++;
+               if (isxdigit(*cp)) {
+                       set_value;
+                       val[i] <<= 4;
+                       val[i] += *cp++;
+               }
+               if (*cp == ':' || *cp == '-' || *cp == '.') {
+                       cp++;
+                       continue;
+               }
+               if (isxdigit(*cp) || i == (ETHER_ADDR_LEN - 1))
+                       continue;
+               return 1;
+       }
+       memcpy(dest, val, ETHER_ADDR_LEN);
 
-        return 0;
+       return 0;
+#undef set_value
 }
 
 


Home | Main Index | Thread Index | Old Index