From d177944b75fc7fd116c09d90d71e6fcbd1c2ed52 Mon Sep 17 00:00:00 2001 From: William Langford Date: Thu, 28 Aug 2014 21:51:33 -0400 Subject: [PATCH] Properly handle incomplete json when input is file Fix #562 --- main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 32db92c9..0fcf5e67 100644 --- a/main.c +++ b/main.c @@ -148,7 +148,7 @@ FILE* current_input; const char** input_filenames = NULL; int ninput_files; int next_input_idx; -static int read_more(char* buf, size_t size) { +static int read_more(char* buf, size_t size, int* islast) { while (!current_input || feof(current_input)) { if (current_input) { fclose(current_input); @@ -168,6 +168,7 @@ static int read_more(char* buf, size_t size) { next_input_idx++; } + if (next_input_idx == ninput_files) *islast = 1; if (!fgets(buf, size, current_input)) buf[0] = 0; return 1; } @@ -439,7 +440,8 @@ int main(int argc, char* argv[]) { } struct jv_parser* parser = jv_parser_new(0); char buf[4096]; - while (read_more(buf, sizeof(buf))) { + int is_last = 0; + while (read_more(buf, sizeof(buf), &is_last)) { if (options & RAW_INPUT) { int len = strlen(buf); if (len > 0) { @@ -451,7 +453,7 @@ int main(int argc, char* argv[]) { } } } else { - jv_parser_set_buf(parser, buf, strlen(buf), !feof(stdin)); + jv_parser_set_buf(parser, buf, strlen(buf), !(is_last || feof(stdin))); jv value; while (jv_is_valid((value = jv_parser_next(parser)))) { if (options & SLURP) {