From 387c5665f6b20faa535a7f782dcaa49390963366 Mon Sep 17 00:00:00 2001 From: Krzysztof Gabis Date: Tue, 6 Feb 2018 20:32:02 +0100 Subject: [PATCH] Better handling of CRLF line breaks. --- parson.c | 24 ++++++++++++------------ tests.c | 23 ++++++++++++----------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/parson.c b/parson.c index 6333a33..583524b 100644 --- a/parson.c +++ b/parson.c @@ -262,7 +262,8 @@ static int is_decimal(const char *string, size_t length) { static char * read_file(const char * filename) { FILE *fp = fopen(filename, "r"); - size_t file_size; + size_t size_to_read = 0; + size_t size_read = 0; long pos; char *file_contents; if (!fp) { @@ -274,22 +275,21 @@ static char * read_file(const char * filename) { fclose(fp); return NULL; } - file_size = pos; + size_to_read = pos; rewind(fp); - file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 1)); + file_contents = (char*)parson_malloc(sizeof(char) * (size_to_read + 1)); if (!file_contents) { fclose(fp); return NULL; } - if (fread(file_contents, file_size, 1, fp) < 1) { - if (ferror(fp)) { - fclose(fp); - parson_free(file_contents); - return NULL; - } + size_read = fread(file_contents, 1, size_to_read, fp); + if (size_read == 0 || ferror(fp)) { + fclose(fp); + parson_free(file_contents); + return NULL; } fclose(fp); - file_contents[file_size] = '\0'; + file_contents[size_read] = '\0'; return file_contents; } @@ -1441,7 +1441,7 @@ JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename if (serialized_string == NULL) { return JSONFailure; } - fp = fopen (filename, "w"); + fp = fopen(filename, "w"); if (fp == NULL) { json_free_serialized_string(serialized_string); return JSONFailure; @@ -1501,7 +1501,7 @@ JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *f if (serialized_string == NULL) { return JSONFailure; } - fp = fopen (filename, "w"); + fp = fopen(filename, "w"); if (fp == NULL) { json_free_serialized_string(serialized_string); return JSONFailure; diff --git a/tests.c b/tests.c index bd9ba36..1d82192 100644 --- a/tests.c +++ b/tests.c @@ -614,33 +614,34 @@ void serialization_example(void) { static char * read_file(const char * filename) { FILE *fp = fopen(filename, "r"); - size_t file_size; + size_t size_to_read = 0; + size_t size_read = 0; long pos; char *file_contents; - if (!fp) + if (!fp) { return NULL; + } fseek(fp, 0L, SEEK_END); pos = ftell(fp); if (pos < 0) { fclose(fp); return NULL; } - file_size = pos; + size_to_read = pos; rewind(fp); - file_contents = (char*)malloc(sizeof(char) * (file_size + 1)); + file_contents = (char*)malloc(sizeof(char) * (size_to_read + 1)); if (!file_contents) { fclose(fp); return NULL; } - if (fread(file_contents, file_size, 1, fp) < 1) { - if (ferror(fp)) { - fclose(fp); - free(file_contents); - return NULL; - } + size_read = fread(file_contents, 1, size_to_read, fp); + if (size_read == 0 || ferror(fp)) { + fclose(fp); + free(file_contents); + return NULL; } fclose(fp); - file_contents[file_size] = '\0'; + file_contents[size_read] = '\0'; return file_contents; }