Skip to content

Commit cd02760

Browse files
committed
arg_parse.c: validate the arguments of type number
Signed-off-by: Menglong Dong <imagedong@tencent.com>
1 parent 047a628 commit cd02760

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

component/arg_parse.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <malloc.h>
55
#include <string.h>
66
#include <stdlib.h>
7+
#include <inttypes.h>
78
#define _LINUX_IN_H
89
#include <netinet/in.h>
910
#include <arpa/inet.h>
@@ -94,15 +95,24 @@ int parse_args(int argc, char *argv[], arg_config_t *config,
9495
S_SET(bool, true);
9596
break;
9697
case OPTION_INT: {
97-
int val = atoi(optarg);
98+
char buf[32];
99+
int val;
100+
101+
if (sscanf(optarg, "%d%s", &val, buf) != 1) {
102+
printf("invalid arg value: %s\n",
103+
optarg);
104+
goto err;
105+
}
98106
S_DST(int, val);
99107
S_SET(bool, true);
100108
break;
101109
}
102110
case OPTION_U16BE:
103111
case OPTION_U16: {
104-
int val = atoi(optarg);
105-
if (val <=0 || val > 65535) {
112+
char buf[32];
113+
u16 val;
114+
115+
if (sscanf(optarg, "%hu%s", &val, buf) != 1) {
106116
printf("invalid arg value: %s\n",
107117
optarg);
108118
goto err;
@@ -114,8 +124,10 @@ int parse_args(int argc, char *argv[], arg_config_t *config,
114124
break;
115125
}
116126
case OPTION_U32: {
117-
long val = atol(optarg);
118-
if (val < 0 || val > 0xFFFFFFFF) {
127+
char buf[32];
128+
u32 val;
129+
130+
if (sscanf(optarg, "%u%s", &val, buf) != 1) {
119131
printf("invalid arg value: %s\n",
120132
optarg);
121133
goto err;

0 commit comments

Comments
 (0)