Skip to content

Commit

Permalink
Make sure buffer is terminated on error.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Dec 6, 2023
1 parent 6906a9a commit ed723a4
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pdfio-token.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
}

if (ch == EOF)
{
*bufptr = '\0';
return (false);
}

// Check for delimiters...
if (strchr(PDFIO_DELIM_CHARS, ch) != NULL)
Expand Down Expand Up @@ -354,13 +357,15 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{
// Out of space
_pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false);
}
}

if (ch != ')')
{
_pdfioFileError(tb->pdf, "Unterminated string literal.");
*bufptr = '\0';
return (false);
}

Expand All @@ -380,6 +385,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{
// Out of space...
_pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false);
}

Expand Down Expand Up @@ -413,6 +419,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{
// Out of space...
_pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false);
}
}
Expand All @@ -436,6 +443,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{
// Out of space...
_pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false);
}
}
Expand All @@ -462,12 +470,17 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
if (!isxdigit(tch & 255))
{
_pdfioFileError(tb->pdf, "Bad # escape in name.");
*bufptr = '\0';
return (false);
}
else if (isdigit(tch))
{
ch = ((ch & 255) << 4) | (tch - '0');
}
else
{
ch = ((ch & 255) << 4) | (tolower(tch) - 'a' + 10);
}
}
}

Expand All @@ -479,6 +492,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{
// Out of space
_pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false);
}
}
Expand All @@ -501,6 +515,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
else if (!isspace(ch & 255) && !isxdigit(ch & 255))
{
_pdfioFileError(tb->pdf, "Syntax error: '<%c'", ch);
*bufptr = '\0';
return (false);
}

Expand All @@ -517,12 +532,14 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
{
// Too large
_pdfioFileError(tb->pdf, "Token too large.");
*bufptr = '\0';
return (false);
}
}
else if (!isspace(ch))
{
_pdfioFileError(tb->pdf, "Invalid hex string character '%c'.", ch);
*bufptr = '\0';
return (false);
}
}
Expand All @@ -531,6 +548,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
if (ch == EOF)
{
_pdfioFileError(tb->pdf, "Unterminated hex string.");
*bufptr = '\0';
return (false);
}
break;
Expand All @@ -543,6 +561,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
else
{
_pdfioFileError(tb->pdf, "Syntax error: '>%c'.", ch);
*bufptr = '\0';
return (false);
}
break;
Expand Down

0 comments on commit ed723a4

Please sign in to comment.