What changed, and why it matters
This commit fixes compiler warnings in a test-only fake memory file. It makes the code more robust by properly checking return values of string-formatting and file-read functions, but the changes are in test helper code, not the actual device firmware that users rely on.
No urgent action needed. Treat as routine code-quality/test-hardening patch. If reviewing broader test suite, consider applying the same snprintf/fread pattern to other hardware-fake files for consistency.
Security signals we found
Unchecked snprintf return value could mask encoding or path-length errors in test helper
Ignored fread return value could allow silent partial reads in test fixture
Signed/unsigned comparison warning resolved by explicit negative check
Evidence from the diff
The patch modifies test/hardware-fakes/src/fake_memory.c, which is a test fixture that simulates device memory using files. It addresses two categories of warnings: (1) comparing the signed int return value of snprintf with an unsigned size_t without first checking for negative error values, and (2) ignoring the return value of fread. The fix adds explicit negative-result checks for snprintf and validates that fread read the expected number of bytes, aborting the test if not. These are defensive correctness improvements in test infrastructure.
Changed components
test/hardware-fakes/src/fake_memory.cInspect captured patch +11 / −4
diff --git a/test/hardware-fakes/src/fake_memory.c b/test/hardware-fakes/src/fake_memory.c
index 583bd16..22b01d8 100644
--- a/test/hardware-fakes/src/fake_memory.c
+++ b/test/hardware-fakes/src/fake_memory.c
@@ -38,7 +38,8 @@ static void _init_file_if_needed(
size_t data_size)
{
char path[512];
- if (snprintf(path, sizeof(path), "%s_%s", base_path, suffix) >= sizeof(path)) {
+ int written = snprintf(path, sizeof(path), "%s_%s", base_path, suffix);
+ if (written <0 || (size_t) written >= sizeof(path)) {
fprintf(stderr, "file path %s_%s too long\n", base_path, suffix);
exit(EXIT_FAILURE);
}
@@ -113,7 +114,8 @@ static void _write_file_chunk(const char* suffix, uint32_t offset, const uint8_t
if (!base_path) {
return;
}
- if (snprintf(path, sizeof(path), "%s_%s", base_path, suffix) >= sizeof(path)) {
+ int written = snprintf(path, sizeof(path), "%s_%s", base_path, suffix);
+ if (written <0 || (size_t) written >= sizeof(path)) {
fprintf(stderr, "file path %s_%s too long\n", base_path, suffix);
exit(EXIT_FAILURE);
}
@@ -134,7 +136,8 @@ static void _read_file_chunk(const char* suffix, uint32_t offset, uint8_t* chunk
if (!base_path) {
return;
}
- if (snprintf(path, sizeof(path), "%s_%s", base_path, suffix) >= sizeof(path)) {
+ int written = snprintf(path, sizeof(path), "%s_%s", base_path, suffix);
+ if (written <0 || (size_t) written >= sizeof(path)) {
fprintf(stderr, "file path %s_%s too long\n", base_path, suffix);
exit(EXIT_FAILURE);
}
@@ -144,7 +147,11 @@ static void _read_file_chunk(const char* suffix, uint32_t offset, uint8_t* chunk
exit(EXIT_FAILURE);
}
fseek(f, offset, SEEK_SET);
- fread(chunk, 1, len, f);
+ size_t nread = fread(chunk, 1, len, f);
+ if (nread != len) {
+ fprintf(stderr, "expected %zu bytes, got %zu\n", len, nread);
+ exit(EXIT_FAILURE);
+ }
fclose(f);
}
Why this scored 20/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.