shithub: scc

Download patch

ref: d2f2f98130f28e9e02a4210f1096c1bd3afffb93
parent: 5a522b88668d1295302293de3f86c0b2869ee57a
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Aug 29 05:42:30 EDT 2019

[libmach] Fix error codes in readobj()

There were cases where we were not setting errno and we were
returning error in case of having an empty string table.

--- a/src/libmach/coff32/coff32read.c
+++ b/src/libmach/coff32/coff32read.c
@@ -154,13 +154,15 @@
 	if (fread(buf, 4, 1, fp) != 1)
 		return 0;
 	unpack(ORDER(obj->type), buf, "l", &siz);
+	coff->strsiz = 0;
 	if (siz == 4)
-		return 0;
+		return 1;
 	if (siz > 4) {
-		if (siz > SIZE_MAX)
+		if (siz > SIZE_MAX) {
+			errno = ERANGE;
 			return 0;
-		str = malloc(siz);
-		if (!str)
+		}
+		if ((str = malloc(siz)) == NULL)
 			return 0;
 		coff->strtbl = str;
 		coff->strsiz = siz;
@@ -207,8 +209,10 @@
 			if (fread(buf, RELSZ, 1, fp) != 1)
 				return 0;
 			unpack_reloc(ORDER(obj->type), buf, &rp[i]);
-			if (rp[i].r_symndx >= hdr->f_nsyms)
+			if (rp[i].r_symndx >= hdr->f_nsyms) {
+				errno = ERANGE;
 				return 0;
+			}
 		}
 	}