fix typo in table for getprotoent that caused out-of-bound reads
[musl] / src / network / __ipparse.c
1 #include <stdlib.h>
2 #include <ctype.h>
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include "__dns.h"
7
8 int __ipparse(void *dest, int family, const char *s0)
9 {
10         const char *s = s0;
11         unsigned char *d = dest;
12         unsigned long a[16] = { 0 };
13         char *z;
14         int i;
15
16         if (family == AF_INET6) goto not_v4;
17
18         for (i=0; i<4; i++) {
19                 a[i] = strtoul(s, &z, 0);
20                 if (z==s || (*z && *z != '.') || !isdigit(*s)) {
21                         if (family == AF_INET) return -1;
22                         goto not_v4;
23                 }
24                 if (!*z) break;
25                 s=z+1;
26         }
27         if (i==4) return -1;
28         switch (i) {
29         case 0:
30                 a[1] = a[0] & 0xffffff;
31                 a[0] >>= 24;
32         case 1:
33                 a[2] = a[1] & 0xffff;
34                 a[1] >>= 16;
35         case 2:
36                 a[3] = a[2] & 0xff;
37                 a[2] >>= 8;
38         }
39         ((struct sockaddr_in *)d)->sin_family = AF_INET;
40         d = (void *)&((struct sockaddr_in *)d)->sin_addr;
41         for (i=0; i<4; i++) {
42                 if (a[i] > 255) return -1;
43                 d[i] = a[i];
44         }
45         return 0;
46
47 not_v4:
48         s = s0;
49         ((struct sockaddr_in6 *)d)->sin6_family = AF_INET6;
50         return inet_pton(AF_INET6, s, (void *)&((struct sockaddr_in6 *)d)->sin6_addr) <= 0 ? -1 : 0;
51 }