shithub: rgbds

Download patch

ref: 3e0b7d428f7a600ab9d899996176a2b514b116e2
parent: bcb78f5d186592d66127f7f07e6c85d059f7ccb9
author: Rangi <[email protected]>
date: Tue Apr 13 05:19:59 EDT 2021

Fix an unclosed file and unfreed memory in out_BinaryFileSlice

Use 'goto cleanup' in both out_BinaryFileSlice and out_BinaryFile

--- a/src/asm/section.c
+++ b/src/asm/section.c
@@ -781,8 +781,7 @@
 
 		if (startPos > fsize) {
 			error("Specified start position is greater than length of file\n");
-			fclose(f);
-			return;
+			goto cleanup;
 		}
 
 		fseek(f, startPos, SEEK_SET);
@@ -805,6 +804,7 @@
 	if (ferror(f))
 		error("Error reading INCBIN file '%s': %s\n", s, strerror(errno));
 
+cleanup:
 	fclose(f);
 }
 
@@ -828,16 +828,16 @@
 
 	if (fstk_FindFile(s, &fullPath, &size))
 		f = fopen(fullPath, "rb");
+	free(fullPath);
 
 	if (!f) {
-		free(fullPath);
 		if (oGeneratedMissingIncludes) {
 			if (verbose)
 				printf("Aborting (-MG) on INCBIN file '%s' (%s)\n", s, strerror(errno));
 			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;
 	}
 
@@ -851,13 +851,13 @@
 
 		if (start_pos > fsize) {
 			error("Specified start position is greater than length of file\n");
-			return;
+			goto cleanup;
 		}
 
 		if ((start_pos + length) > fsize) {
 			error("Specified range in INCBIN is out of bounds (%" PRIu32 " + %" PRIu32
 			      " > %" PRIu32 ")\n", start_pos, length, fsize);
-			return;
+			goto cleanup;
 		}
 
 		fseek(f, start_pos, SEEK_SET);
@@ -885,8 +885,8 @@
 		}
 	}
 
+cleanup:
 	fclose(f);
-	free(fullPath);
 }
 
 /*