Skip to content

Commit

Permalink
Check for buffer EOF in scanner (#3753)
Browse files Browse the repository at this point in the history
Change-Id: I10c42b4a7955e21a718f871c54c54aaecb9c9a58
  • Loading branch information
sauwming authored and aberaud committed Jan 8, 2024
1 parent 9363d59 commit f38a5b3
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions pjlib-util/src/pjlib-util/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,17 @@ PJ_DEF(void) pj_scan_skip_whitespace( pj_scanner *scanner )
{
register char *s = scanner->curptr;

while (PJ_SCAN_IS_SPACE(*s)) {
while (PJ_SCAN_CHECK_EOF(s) && PJ_SCAN_IS_SPACE(*s)) {
++s;
}

if (PJ_SCAN_IS_NEWLINE(*s) && (scanner->skip_ws & PJ_SCAN_AUTOSKIP_NEWLINE)) {
for (;;) {
if (PJ_SCAN_CHECK_EOF(s) && PJ_SCAN_IS_NEWLINE(*s) &&
(scanner->skip_ws & PJ_SCAN_AUTOSKIP_NEWLINE))
{
for (; PJ_SCAN_CHECK_EOF(s); ) {
if (*s == '\r') {
++s;
if (!PJ_SCAN_CHECK_EOF(s)) break;
if (*s == '\n') ++s;
++scanner->line;
scanner->curptr = scanner->start_line = s;
Expand All @@ -159,30 +162,33 @@ PJ_DEF(void) pj_scan_skip_whitespace( pj_scanner *scanner )
} else if (PJ_SCAN_IS_SPACE(*s)) {
do {
++s;
} while (PJ_SCAN_IS_SPACE(*s));
} while (PJ_SCAN_CHECK_EOF(s) && PJ_SCAN_IS_SPACE(*s));
} else {
break;
}
}
}

if (PJ_SCAN_IS_NEWLINE(*s) && (scanner->skip_ws & PJ_SCAN_AUTOSKIP_WS_HEADER)==PJ_SCAN_AUTOSKIP_WS_HEADER) {
if (PJ_SCAN_CHECK_EOF(s) && PJ_SCAN_IS_NEWLINE(*s) &&
(scanner->skip_ws & PJ_SCAN_AUTOSKIP_WS_HEADER)==
PJ_SCAN_AUTOSKIP_WS_HEADER)
{
/* Check for header continuation. */
scanner->curptr = s;

if (*s == '\r') {
++s;
}
if (*s == '\n') {
if (PJ_SCAN_CHECK_EOF(s) && *s == '\n') {
++s;
}
scanner->start_line = s;

if (PJ_SCAN_IS_SPACE(*s)) {
if (PJ_SCAN_CHECK_EOF(s) && PJ_SCAN_IS_SPACE(*s)) {
register char *t = s;
do {
++t;
} while (PJ_SCAN_IS_SPACE(*t));
} while (PJ_SCAN_CHECK_EOF(t) && PJ_SCAN_IS_SPACE(*t));

++scanner->line;
scanner->curptr = t;
Expand Down Expand Up @@ -219,8 +225,7 @@ PJ_DEF(int) pj_scan_peek( pj_scanner *scanner,
return -1;
}

/* Don't need to check EOF with PJ_SCAN_CHECK_EOF(s) */
while (pj_cis_match(spec, *s))
while (PJ_SCAN_CHECK_EOF(s) && pj_cis_match(spec, *s))
++s;

pj_strset3(out, scanner->curptr, s);
Expand Down Expand Up @@ -276,17 +281,15 @@ PJ_DEF(void) pj_scan_get( pj_scanner *scanner,

do {
++s;
} while (pj_cis_match(spec, *s));
/* No need to check EOF here (PJ_SCAN_CHECK_EOF(s)) because
* buffer is NULL terminated and pj_cis_match(spec,0) should be
* false.
*/
} while (PJ_SCAN_CHECK_EOF(s) && pj_cis_match(spec, *s));

pj_strset3(out, scanner->curptr, s);

scanner->curptr = s;

if (PJ_SCAN_IS_PROBABLY_SPACE(*s) && scanner->skip_ws) {
if (!pj_scan_is_eof(scanner) &&
PJ_SCAN_IS_PROBABLY_SPACE(*s) && scanner->skip_ws)
{
pj_scan_skip_whitespace(scanner);
}
}
Expand Down Expand Up @@ -329,18 +332,20 @@ PJ_DEF(void) pj_scan_get_unescape( pj_scanner *scanner,
char *start = s;
do {
++s;
} while (pj_cis_match(spec, *s));
} while (PJ_SCAN_CHECK_EOF(s) && pj_cis_match(spec, *s));

if (dst != start) pj_memmove(dst, start, s-start);
dst += (s-start);
}

} while (*s == '%');
} while (PJ_SCAN_CHECK_EOF(s) && (*s == '%'));

scanner->curptr = s;
out->slen = (dst - out->ptr);

if (PJ_SCAN_IS_PROBABLY_SPACE(*s) && scanner->skip_ws) {
if (!pj_scan_is_eof(scanner) &&
PJ_SCAN_IS_PROBABLY_SPACE(*s) && scanner->skip_ws)
{
pj_scan_skip_whitespace(scanner);
}
}
Expand Down Expand Up @@ -421,7 +426,9 @@ PJ_DEF(void) pj_scan_get_quotes(pj_scanner *scanner,

scanner->curptr = s;

if (PJ_SCAN_IS_PROBABLY_SPACE(*s) && scanner->skip_ws) {
if (!pj_scan_is_eof(scanner) &&
PJ_SCAN_IS_PROBABLY_SPACE(*s) && scanner->skip_ws)
{
pj_scan_skip_whitespace(scanner);
}
}
Expand Down

0 comments on commit f38a5b3

Please sign in to comment.