~mirabilos/klibc/master

« back to all changes in this revision

Viewing changes to usr/klibc/inet/inet_pton.c

  • Committer: Ben Hutchings
  • Date: 2024-03-21 22:30:45 UTC
  • Revision ID: git-v1:7359f104c202a6e36212324cdd5aba7964737e9d
[klibc] inet: Stricter IPv6 field parsing in inet_pton()

We currently don't range-check the fields of an IPv6 address, so the
following strings are wrongly accepted:

"10000::"
"::10000"

Since we currently only support hexadecimal fields, implement the
range check by limiting the number of digits to 4.

Signed-off-by: Ben Hutchings <ben@decadent.org.uk>

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
        case AF_INET6:
33
33
                {
34
34
                        struct in6_addr *d = (struct in6_addr *)dst;
35
 
                        int colons = 0, dcolons = 0;
 
35
                        int colons = 0, dcolons = 0, digits = 0;
36
36
                        int i;
37
37
                        const char *p;
38
38
 
43
43
                                        colons++;
44
44
                                        if (p[1] == ':')
45
45
                                                dcolons++;
46
 
                                } else if (!isxdigit((unsigned char)*p))
 
46
                                        digits = 0;
 
47
                                } else if (!isxdigit((unsigned char)*p)
 
48
                                           || ++digits > 4)
47
49
                                        return 0;       /* Invalid address */
48
50
                        }
49
51