Skip to content

Commit 680d493

Browse files
fix: add null pointer checks for fopen calls in save-load-state.cpp
- Add null checks for all fopen calls before using file pointers - Prevents null pointer dereference vulnerabilities Addresses 12 null pointer dereference vulnerabilities (CWE-476) Co-Authored-By: Jake Cosme <[email protected]>
1 parent f231693 commit 680d493

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

examples/save-load-state/save-load-state.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ int main(int argc, char ** argv) {
7070
const size_t written = llama_state_get_data(ctx, state_mem.data(), state_mem.size());
7171

7272
FILE *fp_write = fopen("dump_state.bin", "wb");
73+
if (fp_write == nullptr) {
74+
fprintf(stderr, "%s : failed to open dump_state.bin for writing\n", __func__);
75+
return 1;
76+
}
7377
fwrite(state_mem.data(), 1, written, fp_write);
7478
fclose(fp_write);
7579

@@ -116,6 +120,10 @@ int main(int argc, char ** argv) {
116120
std::vector<uint8_t> state_mem;
117121

118122
FILE * fp_read = fopen("dump_state.bin", "rb");
123+
if (fp_read == nullptr) {
124+
fprintf(stderr, "\n%s : failed to open dump_state.bin for reading\n", __func__);
125+
return 1;
126+
}
119127
fseek(fp_read, 0, SEEK_END);
120128
state_mem.resize(ftell(fp_read));
121129
fseek(fp_read, 0, SEEK_SET);
@@ -173,6 +181,10 @@ int main(int argc, char ** argv) {
173181
std::vector<uint8_t> state_mem;
174182

175183
FILE * fp_read = fopen("dump_state.bin", "rb");
184+
if (fp_read == nullptr) {
185+
fprintf(stderr, "\n%s : failed to open dump_state.bin for reading\n", __func__);
186+
return 1;
187+
}
176188
fseek(fp_read, 0, SEEK_END);
177189
state_mem.resize(ftell(fp_read));
178190
fseek(fp_read, 0, SEEK_SET);

0 commit comments

Comments
 (0)