From 6a7e58737b7b690d62e93190c5baf2897234727b Mon Sep 17 00:00:00 2001 From: Dominik Loidolt <dominik.loidolt@univie.ac.at> Date: Tue, 28 Jan 2025 16:58:37 +0100 Subject: [PATCH] cmp_tool: Improve error handling for empty files - Update error message to display "Error: The file is empty" instead of "Warning: The file is empty" Prevents access of uninitialized data in the be_to_cpu functions --- programs/cmp_io.c | 6 +++--- test/cmp_tool/cmp_tool_integration_test.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/programs/cmp_io.c b/programs/cmp_io.c index 32eb996..acf9bbc 100644 --- a/programs/cmp_io.c +++ b/programs/cmp_io.c @@ -1373,9 +1373,8 @@ ssize_t read_file8(const char *file_name, uint8_t *buf, uint32_t buf_size, int f if (file_size < 0) goto fail; if (file_size == 0) { - fprintf(stderr, "%s: %s: Warning: The file is empty.\n", PROGRAM_NAME, file_name); - fclose(fp); - return 0; + fprintf(stderr, "%s: %s: Error: The file is empty.\n", PROGRAM_NAME, file_name); + goto fail; } if ((unsigned long)file_size < buf_size) { fprintf(stderr, "%s: %s: Error: The files do not contain enough data.\n", PROGRAM_NAME, file_name); @@ -1474,6 +1473,7 @@ ssize_t read_file_data(const char *file_name, enum cmp_type cmp_type, if (err) return -1; + return size; } diff --git a/test/cmp_tool/cmp_tool_integration_test.py b/test/cmp_tool/cmp_tool_integration_test.py index e0e3fbe..9ffe5ab 100755 --- a/test/cmp_tool/cmp_tool_integration_test.py +++ b/test/cmp_tool/cmp_tool_integration_test.py @@ -923,7 +923,7 @@ def test_empty_file(): assert(stdout == CMP_START_STR_CMP + "Importing configuration file %s ... DONE\n" % (cfg_file_name) + "Importing data file %s ... FAILED\n" % (data_file_name)) - assert(stderr == "cmp_tool: %s: Warning: The file is empty.\n" % (data_file_name)) + assert(stderr == "cmp_tool: %s: Error: The file is empty.\n" % (data_file_name)) assert(returncode == EXIT_FAILURE) finally: -- GitLab