fff-random: Use strtol to parse numbers

Other than atoi, strtol allows to detect parsing errors.
Therefore atoi is replaced with strtol and appropriate error
checks are added.

Fixes: #33 (gitea)

Signed-off-by: Fabian Bläse <fabian@blaese.de>
Reviewed-by: Johannes Kimmel <fff@bareminimum.eu>
This commit is contained in:
Fabian Bläse 2021-01-08 19:02:25 +01:00 committed by Adrian Schmutzler
parent 1146a81a64
commit a79b453de1
2 changed files with 33 additions and 3 deletions

View File

@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=fff-random
PKG_RELEASE:=2
PKG_RELEASE:=3
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)

View File

@ -6,6 +6,36 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
static int parse_int(char *str) {
char *endptr = NULL;
errno = 0;
long val = strtol(str, &endptr, 10);
if (errno != 0) {
perror("strtol");
exit(EXIT_FAILURE);
}
if (endptr == str) {
fprintf(stderr, "No digits were found\n");
exit(EXIT_FAILURE);
}
if (*endptr != '\0') {
fprintf(stderr, "Further characters were found after number: \"%s\"\n", endptr);
exit(EXIT_FAILURE);
}
int retVal = (int) val;
if (val != retVal) {
fprintf(stderr, "Given number is out of range\n");
exit(EXIT_FAILURE);
}
return retVal;
}
int main(int argc, char **argv)
{
@ -22,8 +52,8 @@ int main(int argc, char **argv)
}
else if (argc == 3)
{
from = atoi(argv[1]);
to = atoi(argv[2]);
from = parse_int(argv[1]);
to = parse_int(argv[2]);
}
diff = to - from;