Skip to content

Commit cbc8d2c

Browse files
committed
cmdline: use strtoul() for safety check on overflow username arg
References: - https://stackoverflow.com/a/3792686
1 parent 8211b10 commit cbc8d2c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

CommandLine.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ in the source distribution for its full text.
1212

1313
#include <assert.h>
1414
#include <ctype.h>
15+
#include <errno.h>
1516
#include <getopt.h>
17+
#include <limits.h>
1618
#include <locale.h>
1719
#include <stdbool.h>
1820
#include <stdio.h>
@@ -223,12 +225,14 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
223225
if (!username) {
224226
flags->userId = geteuid();
225227
} else if (!Action_setUserOnly(username, &(flags->userId))) {
226-
for (const char* itr = username; *itr; ++itr)
227-
if (!isdigit((unsigned char)*itr)) {
228-
fprintf(stderr, "Error: invalid user \"%s\".\n", username);
229-
return STATUS_ERROR_EXIT;
230-
}
231-
flags->userId = (uid_t)atol(username);
228+
char* endptr;
229+
errno = 0;
230+
unsigned long val = strtoul(username, &endptr, 10);
231+
if (errno == ERANGE || *endptr != '\0' || username == endptr || val > UINT_MAX) {
232+
fprintf(stderr, "Error: invalid user \"%s\".\n", username);
233+
return STATUS_ERROR_EXIT;
234+
}
235+
flags->userId = (uid_t)val;
232236
}
233237
break;
234238
}

0 commit comments

Comments
 (0)