Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
Change atoi to the safter strtol and display warning for unexpected -…
Browse files Browse the repository at this point in the history
…-loc values
  • Loading branch information
jvm3487 committed Jul 31, 2018
1 parent b2525fe commit 06344a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 16 additions & 9 deletions fw1-loggrabber.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,28 @@ main (int argc, char *argv[])
else if (strcmp (argv[i], "--loc") == 0)
{
i++;
if ( argv[i] == NULL )
if ( argv[i] == NULL || argv[i][0] == '-')
{
fprintf (stderr, "ERROR: Value expected for argument: %s\n", argv[i - 1]);
usage (argv[0]);
exit_loggrabber (1);
}
if (argv[i][0] == '-')
{
fprintf (stderr, "ERROR: Value expected for argument %s\n",
fprintf (stderr, "ERROR: 0 or positive value expected for argument %s\n",
argv[i - 1]);
usage (argv[0]);
exit_loggrabber (1);
}

record_num = atoi ( argv[i] );
errno = 0;
long raw_record_num = strtol ( argv[i], NULL, 10 );
if ( errno == ERANGE || raw_record_num < 0 || raw_record_num > INT_MAX)
{
fprintf (stderr,
"WARNING: %s expects an integer range value greater than or equal to 0\n"
" The provided value will be treated as if it were 0\n",
argv[i-1] );
record_num = 0;
}
else
{
record_num = (int) raw_record_num;
}
}
else if ((strcmp (argv[i], "-f") == 0)
|| (strcmp (argv[i], "--logfile") == 0))
Expand Down
2 changes: 2 additions & 0 deletions fw1-loggrabber.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <ctype.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>

#define SLEEP(sec) sleep(sec)
#include <netinet/in.h>
Expand Down

0 comments on commit 06344a6

Please sign in to comment.