read: Avoid possible NULL dereference in getNthPrevCFromInputFile()

Also, don't perform subtractions to check pointer bounds, to avoid
unsigned value wraparound.  This is very unlikely as it would either
mean a very large `nth` value or a very small value for the current
line pointer, but better safe than sorry.
This commit is contained in:
Colomban Wendling 2016-01-24 15:26:11 +01:00
parent 67f3add7c7
commit cdabbecd37

View File

@ -506,9 +506,10 @@ extern int fileGetc (void)
extern int fileGetNthPrevC (unsigned int nth, int def)
{
const unsigned char *base = (unsigned char *) vStringValue (File.line);
const unsigned int offset = File.ungetchIdx + 1 + nth;
if (File.currentLine - File.ungetchIdx - 1 - nth >= base)
return (int) *(File.currentLine - File.ungetchIdx - 1 - nth);
if (File.currentLine != NULL && File.currentLine >= base + offset)
return (int) *(File.currentLine - offset);
else
return def;
}