shithub: scc

Download patch

ref: ed1d2d62298cb6661c8cf31409f20c16c8c80851
parent: e58b45d858955d8ffd748a68df69171e30f0ab0d
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Mar 16 12:44:54 EDT 2022

cc2: Fix parameters order after popctx() fix

The commit f124441b fixed a memory corruption problem but it also
introduced an important regression because the order of parameters
was reversed.

--- a/src/cmd/cc/cc2/cc2.h
+++ b/src/cmd/cc/cc2/cc2.h
@@ -168,7 +168,7 @@
 		Node *stmt;
 		Inst *inst;
 	} u;
-	Symbol *next;
+	Symbol *next, *prev;
 	Symbol *h_next;
 };
 
--- a/src/cmd/cc/cc2/symbol.c
+++ b/src/cmd/cc/cc2/symbol.c
@@ -10,6 +10,7 @@
 #define NR_SYMHASH  64
 
 Symbol *locals;
+static Symbol *tail;
 
 static Symbol *symtab[NR_SYMHASH];
 static int infunction;
@@ -31,11 +32,11 @@
 void
 popctx(void)
 {
-	Symbol *sym, *next;
+	Symbol *sym, *prev;
 
 	infunction = 0;
-	for (sym = locals; sym; sym = next) {
-		next = sym->next;
+	for (sym = tail; sym; sym = prev) {
+		prev = sym->prev;
 		/*
 		 * Symbols are inserted in the hash in the inverted
 		 * order they are found in locals and it is impossible
@@ -51,7 +52,7 @@
 			symtab[sym->id & NR_SYMHASH-1] = sym->h_next;
 		freesym(sym);
 	}
-	locals = NULL;
+	locals = tail = NULL;
 }
 
 Symbol *
@@ -74,8 +75,13 @@
 	sym = xcalloc(1, sizeof(*sym));
 	sym->id = id;
 	if (infunction) {
-		sym->next = locals;
-		locals = sym;
+		sym->next = NULL;
+		sym->prev = tail;
+		if (tail)
+			tail->next = sym;
+		tail = sym;
+		if (!locals)
+			locals = sym;
 	}
 	if (id != TMPSYM) {
 		sym->h_next = *htab;