Ensure port number is valid

This commit is contained in:
Philip K 2020-12-06 18:55:30 +01:00 committed by Eyal Sawady
parent 9a742e0ca6
commit 6693c97749
No known key found for this signature in database
GPG Key ID: 604D3459E53A9952
1 changed files with 9 additions and 2 deletions

11
main.c
View File

@ -1414,8 +1414,15 @@ main(int argc, char *argv[])
int opt;
while ((opt = getopt(argc, argv, "hp:")) != -1) {
switch (opt) {
case 'p':
port = strtol(optarg, &endptr, 10);
case 'p':;
errno = 0;
long _port = strtol(optarg, &endptr, 10);
if (*endptr != '\0' || errno != 0 ||
_port <= 0 || _port >= 65536) {
fprintf(stderr, "Invalid port: %s\n", optarg);
exit(EXIT_FAILURE);
}
port = (uint16_t) _port;
break;
case 'h':
printf("%s", usage);