shithub: scc

Download patch

ref: 159f7abe9deab394da5dc48923e78c2bf3008354
parent: 8d03b46d52b27f48a684587c28e59a79be2aab01
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri May 11 04:31:26 EDT 2018

[ld/coff32] Finish the implementation of readsyms() and readsects()

--- a/ld/coff32.c
+++ b/ld/coff32.c
@@ -82,19 +82,26 @@
 static int
 readsects(Obj *obj, long off)
 {
-	unsigned i;
+	unsigned nsec, i;
 	unsigned char buff[SCNHSZ];
-	SCNHDR scn;
+	SCNHDR *scn;
 	FILHDR *hdr;
 
+	hdr = obj->filhdr;
+	nsec = hdr->f_nscns;
+	if (nsec > SIZE_MAX / sizeof(*scn))
+		return -1;
+
+	if ((scn = malloc(nsec * sizeof(*scn))) == NULL)
+		return -1;
+
 	if (fseek(obj->fp, off, SEEK_SET) == EOF)
 		return -1;
 
-	hdr = obj->filhdr;
-	for (i = 0; i < hdr->f_nscns; i++) {
+	for (i = 0; i < nsec; i++) {
 		if (fread(buff, SCNHSZ, 1, obj->fp) != 1)
 			return -1;
-		getscn(buff, &scn);
+		getscn(buff, &scn[i]);
 	}
 }
 
@@ -121,18 +128,30 @@
 }
 
 static int
-loadobj(Obj *obj, long off)
+readsyms(Obj *obj, long off)
 {
-	unsigned i;
+	unsigned i, nsym;
 	unsigned char buff[SYMESZ];
-	SYMENT sym;
+	SYMENT *ent;
 	FILHDR *hdr;
 
-	for (i = 0; i < hdr->f_nsyms; i++) {
+	hdr = obj->filhdr;
+	nsym = hdr->f_nsyms;
+	if (nsym > SIZE_MAX / sizeof(*ent))
+		return -1;
+
+	if ((ent = malloc(nsym * sizeof(*ent))) == NULL)
+		outmem();
+
+	if (fseek(obj->fp, off, SEEK_SET) == EOF)
+		return -1;
+
+	for (i = 0; i < nsym; i++) {
 		if (fread(buff, SYMESZ, 1, obj->fp) != 1)
 			return -1;
-		getsym(buff, &sym);
+		getsym(buff, &ent[i]);
 	}
+	return 0;
 }
 
 static void
@@ -163,7 +182,7 @@
 
 	if (readstr(obj, stroff) < 0)
 		goto bad_file;
-	if (loadobj(obj, symoff) < 0)
+	if (readsyms(obj, symoff) < 0)
 		goto bad_file;
 	if (readsects(obj, secoff) < 0)
 		goto bad_file;