Skip to content

Commit 984b5cd

Browse files
committed
Handle input errors for --indent
Handle malformed and overflowing arguments. Also, forbid leading and trailing spaces to match the behavior of tonumber from commit ce0e788 (improve tonumber/0 performance by parsing input as number literal, 2024-03-02).
1 parent 562d5c5 commit 984b5cd

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/main.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,15 @@ int main(int argc, char* argv[]) {
453453
fprintf(stderr, "%s: --indent takes one parameter\n", progname);
454454
die();
455455
}
456-
dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
457-
int indent = atoi(argv[i+1]);
458-
if (indent < -1 || indent > 7) {
456+
char* end = NULL;
457+
errno = 0;
458+
long indent = strtol(argv[i+1], &end, 10);
459+
if (errno || indent < -1 || indent > 7 ||
460+
isspace(*argv[i+1]) || end == NULL || *end) {
459461
fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname);
460462
die();
461463
}
464+
dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
462465
dumpopts |= JV_PRINT_INDENT_FLAGS(indent);
463466
i++;
464467
continue;

0 commit comments

Comments
 (0)