Skip to content

Commit e66a123

Browse files
committed
chore: fix warnings.
Fix warnings due to either unsigned/signed type mismatch in comparison, or ignored return values that should have been checked.
1 parent 6a22b6a commit e66a123

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

test/hardware-fakes/src/fake_memory.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ static void _init_file_if_needed(
3838
size_t data_size)
3939
{
4040
char path[512];
41-
if (snprintf(path, sizeof(path), "%s_%s", base_path, suffix) >= sizeof(path)) {
41+
int written = snprintf(path, sizeof(path), "%s_%s", base_path, suffix);
42+
if (written <0 || (size_t) written >= sizeof(path)) {
4243
fprintf(stderr, "file path %s_%s too long\n", base_path, suffix);
4344
exit(EXIT_FAILURE);
4445
}
@@ -113,7 +114,8 @@ static void _write_file_chunk(const char* suffix, uint32_t offset, const uint8_t
113114
if (!base_path) {
114115
return;
115116
}
116-
if (snprintf(path, sizeof(path), "%s_%s", base_path, suffix) >= sizeof(path)) {
117+
int written = snprintf(path, sizeof(path), "%s_%s", base_path, suffix);
118+
if (written <0 || (size_t) written >= sizeof(path)) {
117119
fprintf(stderr, "file path %s_%s too long\n", base_path, suffix);
118120
exit(EXIT_FAILURE);
119121
}
@@ -134,7 +136,8 @@ static void _read_file_chunk(const char* suffix, uint32_t offset, uint8_t* chunk
134136
if (!base_path) {
135137
return;
136138
}
137-
if (snprintf(path, sizeof(path), "%s_%s", base_path, suffix) >= sizeof(path)) {
139+
int written = snprintf(path, sizeof(path), "%s_%s", base_path, suffix);
140+
if (written <0 || (size_t) written >= sizeof(path)) {
138141
fprintf(stderr, "file path %s_%s too long\n", base_path, suffix);
139142
exit(EXIT_FAILURE);
140143
}
@@ -144,7 +147,11 @@ static void _read_file_chunk(const char* suffix, uint32_t offset, uint8_t* chunk
144147
exit(EXIT_FAILURE);
145148
}
146149
fseek(f, offset, SEEK_SET);
147-
fread(chunk, 1, len, f);
150+
size_t nread = fread(chunk, 1, len, f);
151+
if (nread != len) {
152+
fprintf(stderr, "expected %zu bytes, got %zu\n", len, nread);
153+
exit(EXIT_FAILURE);
154+
}
148155
fclose(f);
149156
}
150157

0 commit comments

Comments
 (0)