Skip to content

Commit 4c61afa

Browse files
committed
Check for interrupts and stack overflow in TParserGet().
TParserGet() recurses for some token types, meaning it's possible to drive it to stack overflow. Since this is a minority behavior, I chose to add the check_stack_depth() call to the two places that recurse rather than doing it during every single call. While at it, add CHECK_FOR_INTERRUPTS(), because this can run unpleasantly long for long inputs. Per bug #17995 from Zuming Jiang. This is old, so back-patch to all supported branches. Discussion: https://postgr.es/m/17995-9f20ff3e6389db4c@postgresql.org
1 parent c158992 commit 4c61afa

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/backend/tsearch/wparser_def.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "catalog/pg_collation.h"
2020
#include "commands/defrem.h"
21+
#include "miscadmin.h"
2122
#include "tsearch/ts_locale.h"
2223
#include "tsearch/ts_public.h"
2324
#include "tsearch/ts_type.h"
@@ -632,6 +633,12 @@ p_ishost(TParser *prs)
632633

633634
tmpprs->wanthost = true;
634635

636+
/*
637+
* Check stack depth before recursing. (Since TParserGet() doesn't
638+
* normally recurse, we put the cost of checking here not there.)
639+
*/
640+
check_stack_depth();
641+
635642
if (TParserGet(tmpprs) && tmpprs->type == HOST)
636643
{
637644
prs->state->posbyte += tmpprs->lenbytetoken;
@@ -655,6 +662,12 @@ p_isURLPath(TParser *prs)
655662
tmpprs->state = newTParserPosition(tmpprs->state);
656663
tmpprs->state->state = TPS_InURLPathFirst;
657664

665+
/*
666+
* Check stack depth before recursing. (Since TParserGet() doesn't
667+
* normally recurse, we put the cost of checking here not there.)
668+
*/
669+
check_stack_depth();
670+
658671
if (TParserGet(tmpprs) && tmpprs->type == URLPATH)
659672
{
660673
prs->state->posbyte += tmpprs->lenbytetoken;
@@ -1698,6 +1711,8 @@ TParserGet(TParser *prs)
16981711
{
16991712
const TParserStateActionItem *item = NULL;
17001713

1714+
CHECK_FOR_INTERRUPTS();
1715+
17011716
Assert(prs->state);
17021717

17031718
if (prs->state->posbyte >= prs->lenstr)

0 commit comments

Comments
 (0)