From ace4a6cc54a203f191a3c7b5803e4bc77d1fc5c5 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 28 Dec 2013 21:48:28 +0100 Subject: [PATCH] Fix unportable gid_t parsing in fgetgrent_r(3). --- libc/grp/fgetgrent_r.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/libc/grp/fgetgrent_r.cpp b/libc/grp/fgetgrent_r.cpp index de6b565e..30fbf778 100644 --- a/libc/grp/fgetgrent_r.cpp +++ b/libc/grp/fgetgrent_r.cpp @@ -49,19 +49,29 @@ static char* next_field(char** current) return result; } -static id_t next_field_id(char** current) +static bool next_field_uintmax(char** current, uintmax_t* result) { char* id_str = next_field(current); if ( !id_str ) - return -1; + return false; char* id_endptr; - intmax_t id_imax = strtoimax(id_str, &id_endptr, 10); - if ( id_imax < 0 || *id_endptr ) - return -1; - id_t id = (id_t) id_imax; - if ( id != id_imax ) - return -1; - return id; + uintmax_t id_umax = strtoumax(id_str, &id_endptr, 10); + if ( *id_endptr ) + return false; + *result = id_umax; + return true; +} + +static gid_t next_field_gid(char** current, gid_t* result) +{ + uintmax_t id_umax; + if ( !next_field_uintmax(current, &id_umax) ) + return false; + gid_t gid = (gid_t) id_umax; + if ( (uintmax_t) gid != (uintmax_t) id_umax ) + return false; + *result = gid; + return true; } static size_t count_num_members(const char* member_string) @@ -177,7 +187,7 @@ int fgetgrent_r(FILE* restrict fp, goto parse_failure; if ( !(result->gr_passwd = next_field(&parse_str)) ) goto parse_failure; - if ( !(result->gr_gid = next_field_id(&parse_str)) < 0 ) + if ( !next_field_gid(&parse_str, &result->gr_gid) ) goto parse_failure; char* member_string; if ( !(member_string = next_field(&parse_str)) )