From 60b1ac1dcc697b562ea8acee9d80c7701659cbcb Mon Sep 17 00:00:00 2001 From: Thalia Archibald Date: Thu, 31 Oct 2024 01:36:33 -0700 Subject: [PATCH] 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). --- src/main.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 372342c94c..2a4c32c410 100644 --- a/src/main.c +++ b/src/main.c @@ -431,12 +431,15 @@ int main(int argc, char* argv[]) { fprintf(stderr, "%s: --indent takes one parameter\n", progname); die(); } - dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7)); - int indent = atoi(argv[i+1]); - if (indent < -1 || indent > 7) { + char* end = NULL; + errno = 0; + long indent = strtol(argv[i+1], &end, 10); + if (errno || indent < -1 || indent > 7 || + isspace(*argv[i+1]) || end == NULL || *end) { fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname); die(); } + dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7)); dumpopts |= JV_PRINT_INDENT_FLAGS(indent); i++; } else if (isoption(&text, 0, "seq", is_short)) {