Fix an unclosed file and unfreed memory in out_BinaryFileSlice

Use 'goto cleanup' in both out_BinaryFileSlice and out_BinaryFile
This commit is contained in:
Rangi
2021-04-13 09:19:59 -04:00
parent bcb78f5d18
commit 3e0b7d428f

View File

@@ -781,8 +781,7 @@ void out_BinaryFile(char const *s, int32_t startPos)
if (startPos > fsize) { if (startPos > fsize) {
error("Specified start position is greater than length of file\n"); error("Specified start position is greater than length of file\n");
fclose(f); goto cleanup;
return;
} }
fseek(f, startPos, SEEK_SET); fseek(f, startPos, SEEK_SET);
@@ -805,6 +804,7 @@ void out_BinaryFile(char const *s, int32_t startPos)
if (ferror(f)) if (ferror(f))
error("Error reading INCBIN file '%s': %s\n", s, strerror(errno)); error("Error reading INCBIN file '%s': %s\n", s, strerror(errno));
cleanup:
fclose(f); fclose(f);
} }
@@ -828,16 +828,16 @@ void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
if (fstk_FindFile(s, &fullPath, &size)) if (fstk_FindFile(s, &fullPath, &size))
f = fopen(fullPath, "rb"); f = fopen(fullPath, "rb");
free(fullPath);
if (!f) { if (!f) {
free(fullPath);
if (oGeneratedMissingIncludes) { if (oGeneratedMissingIncludes) {
if (verbose) if (verbose)
printf("Aborting (-MG) on INCBIN file '%s' (%s)\n", s, strerror(errno)); printf("Aborting (-MG) on INCBIN file '%s' (%s)\n", s, strerror(errno));
oFailedOnMissingInclude = true; oFailedOnMissingInclude = true;
return; } else {
}
error("Error opening INCBIN file '%s': %s\n", s, strerror(errno)); error("Error opening INCBIN file '%s': %s\n", s, strerror(errno));
}
return; return;
} }
@@ -851,13 +851,13 @@ void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
if (start_pos > fsize) { if (start_pos > fsize) {
error("Specified start position is greater than length of file\n"); error("Specified start position is greater than length of file\n");
return; goto cleanup;
} }
if ((start_pos + length) > fsize) { if ((start_pos + length) > fsize) {
error("Specified range in INCBIN is out of bounds (%" PRIu32 " + %" PRIu32 error("Specified range in INCBIN is out of bounds (%" PRIu32 " + %" PRIu32
" > %" PRIu32 ")\n", start_pos, length, fsize); " > %" PRIu32 ")\n", start_pos, length, fsize);
return; goto cleanup;
} }
fseek(f, start_pos, SEEK_SET); fseek(f, start_pos, SEEK_SET);
@@ -885,8 +885,8 @@ void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
} }
} }
cleanup:
fclose(f); fclose(f);
free(fullPath);
} }
/* /*