From 44026628be294da236ec67694864a0460545e688 Mon Sep 17 00:00:00 2001
From: Dominik Loidolt <dominik.loidolt@univie.ac.at>
Date: Wed, 22 Jan 2025 12:01:24 +0100
Subject: [PATCH] Update cmp_tool for improved error handling

- Ensured error handling is now consistent with the --binary option when
  model file size does not match original data size see 3321f1d (Add
  model file size mismatch check and update version to 0.14, 2025-01-16)
- Enhanced cmp_tool integration tests to cover model size mismatch cases
---
 programs/cmp_io.c                          |  2 +-
 programs/cmp_tool.c                        | 18 +++++++++---
 test/cmp_tool/cmp_tool_integration_test.py | 33 +++++++++++++++++++++-
 3 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/programs/cmp_io.c b/programs/cmp_io.c
index c4668ad..ba6413e 100644
--- a/programs/cmp_io.c
+++ b/programs/cmp_io.c
@@ -1315,7 +1315,7 @@ static __inline ssize_t str2uint8_arr(const char *str, uint8_t *data, uint32_t b
 			nptr++;
 	}
 	if (*nptr != '\0') {
-		fprintf(stderr, "%s: %s: Warning: The file may contain more data than specified by the samples or cmp_size parameter.\n",
+		fprintf(stderr, "%s: %s: Warning: The file may contain more data than read from it.\n",
 			PROGRAM_NAME, file_name);
 	}
 
diff --git a/programs/cmp_tool.c b/programs/cmp_tool.c
index f7457fa..89df5ec 100644
--- a/programs/cmp_tool.c
+++ b/programs/cmp_tool.c
@@ -537,6 +537,19 @@ int CMP_MAIN(int argc, char **argv)
 		else
 			model_size = cmp_ent_get_original_size(decomp_entity);
 
+		size = read_file_data(model_file_name, cmp_type, NULL,
+				      model_size, io_flags);
+		if (size < 0)
+			goto fail;
+		if (size < (ssize_t)model_size) {
+			fprintf(stderr, "%s: %s: Error: The files do not contain enough data. Expected: 0x%x, has 0x%x.\n",
+				PROGRAM_NAME, model_file_name, model_size, (uint32_t)size);
+			goto fail;
+		}
+		if (size != (ssize_t)model_size) {
+			fprintf(stderr, "%s: %s: Error: Model file size does not match original data size.\n", PROGRAM_NAME, model_file_name);
+			goto fail;
+		}
 
 		input_model_buf = malloc(model_size);
 		if (!input_model_buf) {
@@ -548,10 +561,7 @@ int CMP_MAIN(int argc, char **argv)
 				      model_size, io_flags);
 		if (size < 0)
 			goto fail;
-		if (size != (ssize_t)model_size) {
-			fprintf(stderr, "%s: %s: Error: Model file size does not match original data size.\n", PROGRAM_NAME, model_file_name);
-			goto fail;
-		}
+
 
 		printf("DONE\n");
 
diff --git a/test/cmp_tool/cmp_tool_integration_test.py b/test/cmp_tool/cmp_tool_integration_test.py
index 3f7e004..e0e3fbe 100755
--- a/test/cmp_tool/cmp_tool_integration_test.py
+++ b/test/cmp_tool/cmp_tool_integration_test.py
@@ -1331,7 +1331,7 @@ def test_model_fiel_erros():
         del_file(output_prefix+"_upmodel.dat")
 
 
-def test_decmp_model_fiel_original_size_miss_match():
+def test_decmp_model_fiel_original_size_miss_match_binary():
     cmp_data = b'8000000d000029000004097ce800cbd5097ce800cbfe00010108d01001000000001001001110078700'
     to_large_model = b'111111111111' # should be 4 byte large in normal case
     output_prefix = 'model_file_to_large'
@@ -1356,6 +1356,37 @@ def test_decmp_model_fiel_original_size_miss_match():
     finally:
         del_file(cmp_data_file_name)
         del_file(model_file_name)
+        del_file(output_prefix+'.dat')
+        del_file(output_prefix+'_upmodel.dat')
+
+
+def test_decmp_model_fiel_original_size_miss_match_binary():
+    cmp_data = '8000000d000029000004097ce800cbd5097ce800cbfe00010108d01001000000001001001110078700'
+    to_large_model = '111111111111' # should be 4 byte large in normal case
+    output_prefix = 'model_file_to_large'
+    cmp_data_file_name = 'cmp_data.cmp'
+    model_file_name = 'to_large_model.dat'
+
+    try:
+        with open(cmp_data_file_name, 'w', encoding='utf-8') as f:
+            f.write(cmp_data)
+
+        with open(model_file_name, 'w', encoding='utf-8') as f:
+            f.write(to_large_model)
+
+        returncode, stdout, stderr = call_cmp_tool(
+            " -d "+cmp_data_file_name + " -m " + model_file_name + " -o "+output_prefix)
+        assert(returncode == EXIT_FAILURE)
+        assert(stdout == CMP_START_STR_DECMP +
+               "Importing compressed data file %s ... DONE\n" % (cmp_data_file_name) +
+               "Importing model file %s ... FAILED\n" % (model_file_name))
+        assert(stderr == "cmp_tool: %s: Error: Model file size does not match original data size.\n" % (model_file_name))
+
+    finally:
+         del_file(cmp_data_file_name)
+         del_file(model_file_name)
+         del_file(output_prefix+'.dat')
+         del_file(output_prefix+'_upmodel.dat')
 
 
 def test_rdcu_pkt():
-- 
GitLab