diff --git a/lib/util/gidlist.c b/lib/util/gidlist.c index 0f555cd45e..1850c2d600 100644 --- a/lib/util/gidlist.c +++ b/lib/util/gidlist.c @@ -65,17 +65,19 @@ sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp if (basegid != NULL) gids[ngids++] = *basegid; cp = gidstr; - do { - gids[ngids] = (GETGROUPS_T) sudo_strtoidx(cp, ",", &ep, &errstr); - if (errstr != NULL) { - sudo_warnx(U_("%s: %s"), cp, U_(errstr)); - free(gids); - debug_return_int(-1); - } - if (basegid == NULL || gids[ngids] != *basegid) - ngids++; - cp = ep + 1; - } while (*ep != '\0'); + if (*cp != '\0') { + do { + gids[ngids] = (GETGROUPS_T) sudo_strtoidx(cp, ",", &ep, &errstr); + if (errstr != NULL) { + sudo_warnx(U_("%s: %s"), cp, U_(errstr)); + free(gids); + debug_return_int(-1); + } + if (basegid == NULL || gids[ngids] != *basegid) + ngids++; + cp = ep + 1; + } while (*ep != '\0'); + } *gidsp = gids; } debug_return_int(ngids); diff --git a/lib/util/regress/parse_gids/parse_gids_test.c b/lib/util/regress/parse_gids/parse_gids_test.c index 1edf29f34b..9a523caeb6 100644 --- a/lib/util/regress/parse_gids/parse_gids_test.c +++ b/lib/util/regress/parse_gids/parse_gids_test.c @@ -44,12 +44,14 @@ struct parse_gids_test { static const GETGROUPS_T test1_out[] = { 0, 1, 2, 3, 4 }; static const GETGROUPS_T test2_out[] = { 1, 2, 3, 4 }; static const GETGROUPS_T test3_out[] = { 0, 1, (gid_t)-2, 3, 4 }; +static const GETGROUPS_T test4_out[] = { 0 }; /* XXX - test syntax errors too */ static struct parse_gids_test test_data[] = { { "1,2,3,4", &test_data[0].basegid, 0, 5, test1_out }, { "1,2,3,4", NULL, 0, 4, test2_out }, { "1,-2,3,4", &test_data[2].basegid, 0, 5, test3_out }, + { "", &test_data[3].basegid, 0, 1, test4_out }, { NULL, NULL, 0, 0, NULL } };