shithub: scc

Download patch

ref: d07fbdefdfa111ca1c6d61104c7f69adcf54baac
parent: 676fafd99e52b851ed18c606b8c2072bf38b4e84
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Apr 4 16:36:38 EDT 2022

cc1: Fix a use after free bug in setloc()

When setloc() is called in delinput() we pass to setloc() a pointer
to the file nameof the current input, then we free that pointer and
we use it to allocate a new buffer with the content that the file
name pointer of the current input had.

--- a/src/cmd/cc/cc1/lex.c
+++ b/src/cmd/cc/cc1/lex.c
@@ -34,8 +34,15 @@
 		memmove(filenam, fname, len);
 		filenam[len] = '\0';
 
-		free(input->filenam);
-		input->filenam = xstrdup(fname);
+		/*
+		 * There are cases where we want to call setloc()
+		 * with the data in input, and then we have t be
+		 * careful about freeing input->filenam
+		 */
+		if (fname != input->filenam) {
+			free(input->filenam);
+			input->filenam = xstrdup(fname);
+		}
 	}
 
 	lineno = input->lineno = line;