Check empty string before strtol()

There is no need to call strtol() if the input string is empty.
This commit is contained in:
Romain Vimont 2018-02-01 16:31:59 +01:00
parent 3bc63708b4
commit 7fe7bbf58c

View file

@ -65,8 +65,12 @@ static int parse_args(struct args *args, int argc, char *argv[]) {
}
case 'p': {
char *endptr;
if (*optarg == '\0') {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid port parameter is empty");
return -1;
}
long value = strtol(optarg, &endptr, 0);
if (*optarg == '\0' || *endptr != '\0') {
if (*endptr != '\0') {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid port: %s", optarg);
return -1;
}
@ -79,8 +83,12 @@ static int parse_args(struct args *args, int argc, char *argv[]) {
}
case 'm': {
char *endptr;
if (*optarg == '\0') {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Max size parameter is empty");
return -1;
}
long value = strtol(optarg, &endptr, 0);
if (*optarg == '\0' || *endptr != '\0') {
if (*endptr != '\0') {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid max size: %s", optarg);
return -1;
}