shithub: rgbds

Download patch

ref: b299f6fb3b8fcf24dc339caf0074707befdbfbce
parent: 645473e336e389db6bacad12eef38c9d86119638
author: ISSOtm <[email protected]>
date: Wed May 6 15:19:10 EDT 2020

Fix uninitialized memory use with `-MT` and `-MQ`

This didn't break unless the first uninitialized byte was non-zero,
which happened to be the case on someone's Windows machine.

Would it be worth it setting up Valgrind in CI?

--- a/src/asm/main.c
+++ b/src/asm/main.c
@@ -454,38 +454,43 @@
 
 		/* Long-only options */
 		case 0:
-			if (depType) {
-				switch (depType) {
-				case 'G':
-					oGeneratedMissingIncludes = true;
-					break;
-				case 'P':
-					oGeneratePhonyDeps = true;
-					break;
-				case 'Q':
-				case 'T':
-					if (optind == argc)
-						errx(1, "-M%c takes a target file name argument",
-						     depType);
-					ep = optarg;
-					if (depType == 'Q')
-						ep = make_escape(ep);
+			switch (depType) {
+			case 'G':
+				oGeneratedMissingIncludes = true;
+				break;
+			case 'P':
+				oGeneratePhonyDeps = true;
+				break;
+			case 'Q':
+			case 'T':
+				if (optind == argc)
+					errx(1, "-M%c takes a target file name argument",
+					     depType);
+				ep = optarg;
+				if (depType == 'Q')
+					ep = make_escape(ep);
 
-					nTargetFileNameLen += strlen(ep) + 1;
+				nTargetFileNameLen += strlen(ep) + 1;
+				if (!tzTargetFileName) {
+					/* On first alloc, make an empty str */
 					tzTargetFileName =
+						malloc(nTargetFileNameLen + 1);
+					*tzTargetFileName = '\0';
+				} else {
+					tzTargetFileName =
 						realloc(tzTargetFileName,
 							nTargetFileNameLen + 1);
-					if (tzTargetFileName == NULL)
-						err(1, "Cannot append new file to target file list");
-					strcat(tzTargetFileName, ep);
-					if (depType == 'Q')
-						free(ep);
-					char *ptr = tzTargetFileName +
-						strlen(tzTargetFileName);
-					*ptr++ = ' ';
-					*ptr = '\0';
-					break;
 				}
+				if (tzTargetFileName == NULL)
+					err(1, "Cannot append new file to target file list");
+				strcat(tzTargetFileName, ep);
+				if (depType == 'Q')
+					free(ep);
+				char *ptr = tzTargetFileName +
+					strlen(tzTargetFileName);
+				*ptr++ = ' ';
+				*ptr = '\0';
+				break;
 			}
 			break;