shithub: rgbds

Download patch

ref: b07c04cd74cb97c9dd2d1c083cfac54c8e22f718
parent: 5ee058f21718406ff4cd5cd3c0c4811512ed167a
author: Ben10do <[email protected]>
date: Sun Feb 19 17:20:21 EST 2017

Implement a malloc-based readasciiz()

Instead of reading into a pre-sized buffer, this function now uses malloc to create a buffer, and resizes it if necessary.

This reduces the risk of memory issues if a long string (< 255 chars) was encountered.

--- a/src/link/object.c
+++ b/src/link/object.c
@@ -46,21 +46,41 @@
 
 	return (r);
 }
+
 /*
  * Read a NULL terminated string from a file
  *
  */
-
-SLONG 
-readasciiz(char *s, FILE * f)
+SLONG
+readasciiz(char **dest, FILE *f)
 {
 	SLONG r = 0;
-
-	while (((*s++) = fgetc(f)) != 0)
+	
+	size_t bufferLength = 16;
+	char *start = malloc(bufferLength);
+	char *s = start;
+	
+	if (!s) {
+		err(1, NULL);
+	}
+		
+	while (((*s++) = fgetc(f)) != 0) {
 		r += 1;
-
+		
+		if (r >= bufferLength) {
+			bufferLength *= 2;
+			start = realloc(start, bufferLength);
+			if (!start) {
+				err(1, NULL);
+			}
+			s = start + r;
+		}
+	}
+	
+	*dest = start;
 	return (r + 1);
 }
+
 /*
  * Allocate a new section and link it into the list
  *
@@ -97,7 +117,6 @@
 struct sSymbol *
 obj_ReadSymbol(FILE * f)
 {
-	char s[256];
 	struct sSymbol *pSym;
 
 	pSym = malloc(sizeof *pSym);
@@ -105,13 +124,7 @@
 		err(1, NULL);
 	}
 
-	readasciiz(s, f);
-	pSym->pzName = malloc(strlen(s) + 1);
-	if (!pSym->pzName) {
-		err(1, NULL);
-	}
-
-	strcpy(pSym->pzName, s);
+	readasciiz(&pSym->pzName, f);
 	if ((pSym->Type = (enum eSymbolType) fgetc(f)) != SYM_IMPORT) {
 		pSym->nSectionID = readlong(f);
 		pSym->nOffset = readlong(f);
@@ -153,7 +166,6 @@
 
 			SLONG nNumberOfPatches;
 			struct sPatch **ppPatch, *pPatch;
-			char s[256];
 
 			fread(pSection->pData, sizeof(UBYTE),
 			    pSection->nByteSize, f);
@@ -171,15 +183,8 @@
 				}
 
 				*ppPatch = pPatch;
-				readasciiz(s, f);
+				readasciiz(&pPatch->pzFilename, f);
 
-				pPatch->pzFilename = malloc(strlen(s) + 1);
-				if (!pPatch->pzFilename) {
-					err(1, NULL);
-				}
-
-				strcpy(pPatch->pzFilename, s);
-
 				pPatch->nLineNo =
 				    readlong(f);
 				pPatch->nOffset =
@@ -306,7 +311,6 @@
 
 			SLONG nNumberOfPatches;
 			struct sPatch **ppPatch, *pPatch;
-			char s[256];
 
 			fread(pSection->pData, sizeof(UBYTE),
 			    pSection->nByteSize, f);
@@ -324,13 +328,7 @@
 				}
 
 				*ppPatch = pPatch;
-				readasciiz(s, f);
-				pPatch->pzFilename = malloc(strlen(s) + 1);
-				if (!pPatch->pzFilename) {
-					err(1, NULL);
-				}
-
-				strcpy(pPatch->pzFilename, s);
+				readasciiz(&pPatch->pzFilename, f);
 				pPatch->nLineNo = readlong(f);
 				pPatch->nOffset = readlong(f);
 				pPatch->Type = (enum ePatchType) fgetc(f);
@@ -482,9 +480,9 @@
 
 	size = file_Length(f) - 4;
 	while (size) {
-		char name[256];
+		char *name;
 
-		size -= readasciiz(name, f);
+		size -= readasciiz(&name, f);
 		readword(f);
 		size -= 2;
 		readword(f);
@@ -492,5 +490,6 @@
 		size -= readlong(f);
 		size -= 4;
 		obj_ReadOpenFile(f, name);
+		free(name);
 	}
 }