From f22d736633a5c53a5715365f65a34e69d6cf5291 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 1 Aug 2020 14:23:26 +0200 Subject: [PATCH] Fix inet_pton(3) parsing 1:: and ::1.2.3.4 cases. --- libc/arpa/inet/inet_pton.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libc/arpa/inet/inet_pton.c b/libc/arpa/inet/inet_pton.c index 73fb2d21..320f1357 100644 --- a/libc/arpa/inet/inet_pton.c +++ b/libc/arpa/inet/inet_pton.c @@ -23,6 +23,7 @@ #include #include #include +#include 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; for ( i = 0; i < 8; i++ ) { - if ( compressed_at == -1 && - src[index + 0] == ':' && - src[index + 1] == ':' ) + bool compressing = compressed_at == -1 && + src[index + 0] == ':' && + src[index + 1] == ':'; + if ( compressing ) { index += 2; compressed_at = i; } - else if ( !src[index] ) + if ( !src[index] ) break; - else if ( i && src[index++] != ':' ) + if ( i && !compressing && src[index++] != ':' ) return 0; int num = 0; for ( int j = 0; j < 4; j++ ) { if ( src[index] == '.' && ((compressed_at == -1 && i == 6) || - (0 < compressed_at && i <= 6)) ) + (0 <= compressed_at && i <= 6)) ) { index -= j; for ( int n = 0; n < 4; n++ )