Skip to content

Commit 200106b

Browse files
vil02p2r3
authored andcommitted
Close file on error paths to prevent resource leak
1 parent 6a65b8a commit 200106b

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

src/serialize.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int initSerializer () {
4545
size_t read = fread(block_changes, 1, sizeof(block_changes), file);
4646
if (read != sizeof(block_changes)) {
4747
printf("Read %u bytes from \"world.bin\", expected %u (block changes). Aborting.\n", read, sizeof(block_changes));
48+
fclose(file);
4849
return 1;
4950
}
5051
// Find the index of the last occupied entry to recover block_changes_count
@@ -56,6 +57,7 @@ int initSerializer () {
5657
// Seek past block changes to start reading player data
5758
if (fseek(file, sizeof(block_changes), SEEK_SET) != 0) {
5859
perror("Failed to seek to player data in \"world.bin\". Aborting.");
60+
fclose(file);
5961
return 1;
6062
}
6163
// Read player data directly into memory
@@ -86,6 +88,7 @@ int initSerializer () {
8688
"Failed to write initial block data to \"world.bin\".\n"
8789
"Consider checking permissions or disabling SYNC_WORLD_TO_DISK in \"globals.h\"."
8890
);
91+
fclose(file);
8992
return 1;
9093
}
9194
// Seek past written block changes to start writing player data
@@ -94,6 +97,7 @@ int initSerializer () {
9497
"Failed to seek past block changes in \"world.bin\"."
9598
"Consider checking permissions or disabling SYNC_WORLD_TO_DISK in \"globals.h\"."
9699
);
100+
fclose(file);
97101
return 1;
98102
}
99103
// Write initial player data to disk (should be just nulls?)

0 commit comments

Comments
 (0)