forked from freifunk-franken/firmware
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:
parent
1146a81a64
commit
a79b453de1
|
@ -1,7 +1,7 @@
|
||||||
include $(TOPDIR)/rules.mk
|
include $(TOPDIR)/rules.mk
|
||||||
|
|
||||||
PKG_NAME:=fff-random
|
PKG_NAME:=fff-random
|
||||||
PKG_RELEASE:=2
|
PKG_RELEASE:=3
|
||||||
|
|
||||||
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
|
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,36 @@
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
@ -22,8 +52,8 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
else if (argc == 3)
|
else if (argc == 3)
|
||||||
{
|
{
|
||||||
from = atoi(argv[1]);
|
from = parse_int(argv[1]);
|
||||||
to = atoi(argv[2]);
|
to = parse_int(argv[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
diff = to - from;
|
diff = to - from;
|
||||||
|
|
Loading…
Reference in New Issue