Fix inet_pton(3) parsing 1:: and ::1.2.3.4 cases.

This commit is contained in:
Jonas 'Sortie' Termansen 2020-08-01 14:23:26 +02:00
parent a969564af4
commit f22d736633
1 changed files with 8 additions and 6 deletions

View File

@ -23,6 +23,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
int inet_pton(int af, const char* restrict src, void* restrict dst) int inet_pton(int af, const char* restrict src, void* restrict dst)
{ {
@ -65,23 +66,24 @@ int inet_pton(int af, const char* restrict src, void* restrict dst)
int i; int i;
for ( i = 0; i < 8; i++ ) for ( i = 0; i < 8; i++ )
{ {
if ( compressed_at == -1 && bool compressing = compressed_at == -1 &&
src[index + 0] == ':' && src[index + 0] == ':' &&
src[index + 1] == ':' ) src[index + 1] == ':';
if ( compressing )
{ {
index += 2; index += 2;
compressed_at = i; compressed_at = i;
} }
else if ( !src[index] ) if ( !src[index] )
break; break;
else if ( i && src[index++] != ':' ) if ( i && !compressing && src[index++] != ':' )
return 0; return 0;
int num = 0; int num = 0;
for ( int j = 0; j < 4; j++ ) for ( int j = 0; j < 4; j++ )
{ {
if ( src[index] == '.' && if ( src[index] == '.' &&
((compressed_at == -1 && i == 6) || ((compressed_at == -1 && i == 6) ||
(0 < compressed_at && i <= 6)) ) (0 <= compressed_at && i <= 6)) )
{ {
index -= j; index -= j;
for ( int n = 0; n < 4; n++ ) for ( int n = 0; n < 4; n++ )