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.
This commit is contained in:
Ben10do
2017-02-19 22:20:21 +00:00
parent 5ee058f217
commit b07c04cd74

View File

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