Make sure buffer is terminated on error.

This commit is contained in:
Michael R Sweet 2023-12-06 11:21:33 -05:00
parent 6906a9a708
commit ed723a46dc
No known key found for this signature in database
GPG Key ID: BE67C75EC81F3244

View File

@ -243,7 +243,10 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
} }
if (ch == EOF) if (ch == EOF)
{
*bufptr = '\0';
return (false); return (false);
}
// Check for delimiters... // Check for delimiters...
if (strchr(PDFIO_DELIM_CHARS, ch) != NULL) if (strchr(PDFIO_DELIM_CHARS, ch) != NULL)
@ -354,6 +357,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{ {
// Out of space // Out of space
_pdfioFileError(tb->pdf, "Token too large."); _pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false); return (false);
} }
} }
@ -361,6 +365,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
if (ch != ')') if (ch != ')')
{ {
_pdfioFileError(tb->pdf, "Unterminated string literal."); _pdfioFileError(tb->pdf, "Unterminated string literal.");
*bufptr = '\0';
return (false); return (false);
} }
@ -380,6 +385,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{ {
// Out of space... // Out of space...
_pdfioFileError(tb->pdf, "Token too large."); _pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false); return (false);
} }
@ -413,6 +419,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{ {
// Out of space... // Out of space...
_pdfioFileError(tb->pdf, "Token too large."); _pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false); return (false);
} }
} }
@ -436,6 +443,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{ {
// Out of space... // Out of space...
_pdfioFileError(tb->pdf, "Token too large."); _pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false); return (false);
} }
} }
@ -462,14 +470,19 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
if (!isxdigit(tch & 255)) if (!isxdigit(tch & 255))
{ {
_pdfioFileError(tb->pdf, "Bad # escape in name."); _pdfioFileError(tb->pdf, "Bad # escape in name.");
*bufptr = '\0';
return (false); return (false);
} }
else if (isdigit(tch)) else if (isdigit(tch))
{
ch = ((ch & 255) << 4) | (tch - '0'); ch = ((ch & 255) << 4) | (tch - '0');
}
else else
{
ch = ((ch & 255) << 4) | (tolower(tch) - 'a' + 10); ch = ((ch & 255) << 4) | (tolower(tch) - 'a' + 10);
} }
} }
}
if (bufptr < bufend) if (bufptr < bufend)
{ {
@ -479,6 +492,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{ {
// Out of space // Out of space
_pdfioFileError(tb->pdf, "Token too large."); _pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false); return (false);
} }
} }
@ -501,6 +515,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
else if (!isspace(ch & 255) && !isxdigit(ch & 255)) else if (!isspace(ch & 255) && !isxdigit(ch & 255))
{ {
_pdfioFileError(tb->pdf, "Syntax error: '<%c'", ch); _pdfioFileError(tb->pdf, "Syntax error: '<%c'", ch);
*bufptr = '\0';
return (false); return (false);
} }
@ -517,12 +532,14 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{ {
// Too large // Too large
_pdfioFileError(tb->pdf, "Token too large."); _pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false); return (false);
} }
} }
else if (!isspace(ch)) else if (!isspace(ch))
{ {
_pdfioFileError(tb->pdf, "Invalid hex string character '%c'.", ch); _pdfioFileError(tb->pdf, "Invalid hex string character '%c'.", ch);
*bufptr = '\0';
return (false); return (false);
} }
} }
@ -531,6 +548,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
if (ch == EOF) if (ch == EOF)
{ {
_pdfioFileError(tb->pdf, "Unterminated hex string."); _pdfioFileError(tb->pdf, "Unterminated hex string.");
*bufptr = '\0';
return (false); return (false);
} }
break; break;
@ -543,6 +561,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
else else
{ {
_pdfioFileError(tb->pdf, "Syntax error: '>%c'.", ch); _pdfioFileError(tb->pdf, "Syntax error: '>%c'.", ch);
*bufptr = '\0';
return (false); return (false);
} }
break; break;