shithub: scc

Download patch

ref: 196b6aee42727015dac4a55161cebae401a3d852
parent: ed874de117ff9d2eea35b69cd6a2fcb61f521c1a
author: Roberto E. Vargas Caballero <[email protected]>
date: Sun Sep 10 04:09:46 EDT 2017

[as] Fix memory allocation in lookup()

--- a/as/emit.c
+++ b/as/emit.c
@@ -41,29 +41,30 @@
 lookup(char *name)
 {
 	unsigned h;
-	Symbol *sym;
+	Symbol *sym, **list;
 	int c;
 	char *t, *s;
 
-	s = name;
-	for (h = 0; c = *s; ++s)
+	h = 0;
+	for (s = name; c = *s; ++s)
 		h = h*33 ^ c;
 	h &= HASHSIZ-1;
 
 	c = *name;
-	for (sym = hashtbl[h]; sym; sym = sym->next) {
+	list = &hashtbl[h];
+	for (sym = *list; sym; sym = sym->next) {
 		t = sym->name;
 		if (c == *t && !strcmp(t, name))
 			return sym;
 	}
 
-	sym = xmalloc(sizeof(sym));
+	sym = xmalloc(sizeof(*sym));
 	sym->name = xstrdup(name);
 	sym->type = FUNDEF;
 	sym->desc = 0;
 	sym->value = 0;
-	sym->next = hashtbl[h];
-	hashtbl[h] = sym;
+	sym->next = *list;
+	*list = sym;
 
 	return sym;
 }