shithub: scc

Download patch

ref: 17a2ac09a4306129d5ec039ff52f74a48b226e48
parent: 3a079f24c514f9783a7dd15e85a02eae4c25e695
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Aug 21 07:49:10 EDT 2019

[ld] Fix merge()

We were incrementing the number of sections per segment in every
iteration instead of every match. This was creating NULL entries
in the section array of every segment.

--- a/src/cmd/ld/pass2.c
+++ b/src/cmd/ld/pass2.c
@@ -36,17 +36,17 @@
 merge(Segment *seg)
 {
 	Section *sec, **p;
-	int n;
+	int n = 0;
 
-	for (n = 0, sec = sechead; sec; sec = sec->next, ++n) {
+	for (sec = sechead; sec; sec = sec->next) {
 		if (sec->type != seg->type)
 			continue;
-		p = realloc(seg->sections, n * sizeof(*p));
+		p = realloc(seg->sections, (n+1) * sizeof(*p));
 		if (!p) {
 			error("ou of memory");
 			exit(EXIT_FAILURE);
 		}
-		p[n] = sec;
+		p[n++] = sec;
 		seg->sections = p;
 		seg->size += sec->size;
 	}