Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 89b3c6c

Browse files
committed
Fix one-byte buffer overrun in contrib/test_parser.
The original coding examined the next character before verifying that there *is* a next character. In the worst case with the input buffer right up against the end of memory, this would result in a segfault. Problem spotted by Paul Guyot; this commit extends his patch to fix an additional case. In addition, make the code a tad more readable by not overloading the usage of *tlen.
1 parent 743ed08 commit 89b3c6c

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

contrib/test_parser/test_parser.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,32 @@ testprs_getlexeme(PG_FUNCTION_ARGS)
7373
ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
7474
char **t = (char **) PG_GETARG_POINTER(1);
7575
int *tlen = (int *) PG_GETARG_POINTER(2);
76+
int startpos = pst->pos;
7677
int type;
7778

78-
*tlen = pst->pos;
7979
*t = pst->buffer + pst->pos;
8080

81-
if ((pst->buffer)[pst->pos] == ' ')
81+
if (pst->pos < pst->len &&
82+
(pst->buffer)[pst->pos] == ' ')
8283
{
8384
/* blank type */
8485
type = 12;
85-
/* go to the next non-white-space character */
86-
while ((pst->buffer)[pst->pos] == ' ' &&
87-
pst->pos < pst->len)
86+
/* go to the next non-space character */
87+
while (pst->pos < pst->len &&
88+
(pst->buffer)[pst->pos] == ' ')
8889
(pst->pos)++;
8990
}
9091
else
9192
{
9293
/* word type */
9394
type = 3;
94-
/* go to the next white-space character */
95-
while ((pst->buffer)[pst->pos] != ' ' &&
96-
pst->pos < pst->len)
95+
/* go to the next space character */
96+
while (pst->pos < pst->len &&
97+
(pst->buffer)[pst->pos] != ' ')
9798
(pst->pos)++;
9899
}
99100

100-
*tlen = pst->pos - *tlen;
101+
*tlen = pst->pos - startpos;
101102

102103
/* we are finished if (*tlen == 0) */
103104
if (*tlen == 0)

0 commit comments

Comments
 (0)