Fix buffer overflow when file ends with \

This commit is contained in:
dbrotz
2018-12-01 07:10:59 -08:00
parent 5cb6c4af4b
commit bad66e54fa

View File

@@ -145,7 +145,7 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f)
size = ftell(f); size = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
pBuffer->pBufferRealStart = malloc(size + 2 + SAFETYMARGIN); pBuffer->pBufferRealStart = malloc(size + 3 + SAFETYMARGIN);
if (pBuffer->pBufferRealStart == NULL) if (pBuffer->pBufferRealStart == NULL)
fatalerror("%s: Out of memory for buffer!", __func__); fatalerror("%s: Out of memory for buffer!", __func__);
@@ -156,8 +156,9 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f)
size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f); size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f);
pBuffer->pBuffer[size] = '\n'; pBuffer->pBuffer[size] = '\n';
pBuffer->pBuffer[size + 1] = 0; pBuffer->pBuffer[size + 1] = '\n'; /* in case the file ends with \ */
pBuffer->nBufferSize = size + 1; pBuffer->pBuffer[size + 2] = 0;
pBuffer->nBufferSize = size + 2;
/* Convert all line endings to LF and spaces */ /* Convert all line endings to LF and spaces */