Skip to content

Commit

Permalink
Handle input errors for --indent
Browse files Browse the repository at this point in the history
Malformed and overflowing arguments were not caught. Also, skip spaces
after the number to match the spaces skipped before the number.
  • Loading branch information
thaliaarchi committed Oct 31, 2024
1 parent 8ca0857 commit b1e9739
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,16 @@ 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);
while (*end && isspace((unsigned char)*end))
end++;
if (errno || indent < -1 || indent > 7 || *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)) {
Expand Down

0 comments on commit b1e9739

Please sign in to comment.