Skip to content

Commit d2e9237

Browse files
tglsfdcpull[bot]
authored andcommitted
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 567c218 commit d2e9237

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"
@@ -631,6 +632,12 @@ p_ishost(TParser *prs)
631632

632633
tmpprs->wanthost = true;
633634

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

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

1713+
CHECK_FOR_INTERRUPTS();
1714+
17001715
Assert(prs->state);
17011716

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

0 commit comments

Comments
 (0)