Skip to content

Commit 685c41a

Browse files
Merge pull request #1429 from sansyse/bugfix-1428-option_parsing
Fixed option parsing errors
2 parents 9f15769 + 4832a96 commit 685c41a

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/st-util/gdb-server.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ int32_t parse_options(int32_t argc, char** argv, st_state_t *st) {
118118
{"no-reset", optional_argument, NULL, 'n'},
119119
{"hot-plug", optional_argument, NULL, 'n'},
120120
{"connect-under-reset", optional_argument, NULL, 'u'},
121-
{"freq", optional_argument, NULL, 'F'},
121+
{"freq", required_argument, NULL, 'F'},
122122
{"version", no_argument, NULL, 'V'},
123123
{"semihosting", no_argument, NULL, SEMIHOSTING_OPTION},
124124
{"serial", required_argument, NULL, SERIAL_OPTION},
@@ -152,11 +152,11 @@ int32_t parse_options(int32_t argc, char** argv, st_state_t *st) {
152152
;
153153

154154

155-
int32_t option_index = 0;
155+
int option_index = 0;
156156
int32_t c;
157157
int32_t q;
158158

159-
while ((c = getopt_long(argc, argv, "hv::p:mnu", long_options, &option_index)) != -1)
159+
while ((c = getopt_long(argc, argv, "hv::p:mnuF:V", long_options, &option_index)) != -1)
160160
switch (c) {
161161
case 0:
162162
break;

src/stlink-lib/helper.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ uint32_t time_ms() {
2323
}
2424

2525
int32_t arg_parse_freq(const char *str) {
26-
char *tail;
27-
int32_t value = (int32_t) strtol(str, &tail, 10);
28-
29-
if (tail[0] == 'M' && tail[1] == '\0') {
30-
value = value*1000;
31-
} else if ((tail[0] != 'k' || tail[1] != '\0') && tail[0] != '\0') {
32-
return -1;
33-
}
34-
35-
return value;
26+
int32_t value = -1;
27+
if (str != NULL) {
28+
char* tail = NULL;
29+
value = strtol(str, &tail, 10);
30+
if (tail != NULL) {
31+
if (tail[0] == 'M' && tail[1] == '\0') {
32+
value = value*1000;
33+
}
34+
else if (tail[0] != '\0' && !(tail[0] == 'k' && tail[1] == '\0')) {
35+
value = -1; /* error */
36+
}
37+
}
38+
}
39+
return value; /* frequency in kHz */
3640
}

0 commit comments

Comments
 (0)