shithub: rgbds

Download patch

ref: 34c2288fd032120c3d16b9ad97528ab78f481f06
parent: 304bb9f902f2a6a5fb39f19d2be8f4698246c435
author: Marco Spataro <42452606+ZedKoS@users.noreply.github.com>
date: Thu Sep 10 08:49:04 EDT 2020

Fix __FILE__ when filename contains quotes


--- a/src/asm/fstack.c
+++ b/src/asm/fstack.c
@@ -522,7 +522,26 @@
 {
 	char tzSymFileName[_MAX_PATH + 1 + 2];
 
-	snprintf(tzSymFileName, sizeof(tzSymFileName), "\"%s\"", pFileName);
+	char *c = pFileName;
+	int fileNameIndex = 0;
+
+	tzSymFileName[fileNameIndex++] = '"';
+
+	// minus 2 to account for trailing "\"\0"
+	// minus 1 to avoid a buffer overflow in extreme cases
+	while (*c && fileNameIndex < sizeof(tzSymFileName) - 2 - 1) {
+		
+		if (*c == '"') {
+			tzSymFileName[fileNameIndex++] = '\\';
+		}
+
+		tzSymFileName[fileNameIndex++] = *c;
+		++c;
+	}
+
+	tzSymFileName[fileNameIndex++] = '"';
+	tzSymFileName[fileNameIndex]   = '\0';
+
 	sym_AddString("__FILE__", tzSymFileName);
 
 	pFileStack = NULL;