Skip to content

Commit

Permalink
Stop using atoi for parsing tcp_server port number
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
  • Loading branch information
pqarmitage committed Jul 31, 2018
1 parent b3b875c commit d3baf06
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion test/tcp_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ int main(int argc, char **argv)
char *addr_str = NULL;
char addr_buf[sizeof (struct in6_addr)];
bool echo_data = false;
char *endptr;
long port_num;

while ((opt = getopt(argc, argv, "46a:p:sue")) != -1) {
switch (opt) {
Expand All @@ -56,7 +58,12 @@ int main(int argc, char **argv)
addr_str = optarg;
break;
case 'p':
port = atoi(optarg);
port_num = strtol(optarg, &endptr, 10);
if (*endptr || port_num <= 0 || port_num > 65535) {
fprintf(stderr, "Port number '%s' invalid\n", optarg);
exit(EXIT_FAILURE);
}
port = port_num;
break;
case 's':
silent = true;
Expand Down

0 comments on commit d3baf06

Please sign in to comment.