Commit 444ec169 authored by Tom Lane's avatar Tom Lane

Defend against stack overrun in a few more places.

SplitToVariants() in the ispell code, lseg_inside_poly() in geo_ops.c,
and regex_selectivity_sub() in selectivity estimation could recurse
until stack overflow; fix by adding check_stack_depth() calls.
So could next() in the regex compiler, but that case is better fixed by
converting its tail recursion to a loop.  (We probably get better code
that way too, since next() can now be inlined into its sole caller.)

There remains a reachable stack overrun in the Turkish stemmer, but
we'll need some advice from the Snowball people about how to fix that.

Per report from Egor Chindyaskin and Alexander Lakhin.  These mistakes
are old, so back-patch to all supported branches.

Richard Guo and Tom Lane

Discussion: https://postgr.es/m/1661334672.728714027@f473.i.mail.ru
parent 04f1013b
...@@ -201,6 +201,8 @@ next(struct vars *v) ...@@ -201,6 +201,8 @@ next(struct vars *v)
{ {
chr c; chr c;
next_restart: /* loop here after eating a comment */
/* errors yield an infinite sequence of failures */ /* errors yield an infinite sequence of failures */
if (ISERR()) if (ISERR())
return 0; /* the error has set nexttype to EOS */ return 0; /* the error has set nexttype to EOS */
...@@ -493,8 +495,7 @@ next(struct vars *v) ...@@ -493,8 +495,7 @@ next(struct vars *v)
if (!ATEOS()) if (!ATEOS())
v->now++; v->now++;
assert(v->nexttype == v->lasttype); assert(v->nexttype == v->lasttype);
return next(v); goto next_restart;
break;
case CHR('='): /* positive lookahead */ case CHR('='): /* positive lookahead */
NOTE(REG_ULOOKAROUND); NOTE(REG_ULOOKAROUND);
RETV(LACON, LATYPE_AHEAD_POS); RETV(LACON, LATYPE_AHEAD_POS);
......
...@@ -63,6 +63,7 @@ ...@@ -63,6 +63,7 @@
#include "postgres.h" #include "postgres.h"
#include "catalog/pg_collation.h" #include "catalog/pg_collation.h"
#include "miscadmin.h"
#include "tsearch/dicts/spell.h" #include "tsearch/dicts/spell.h"
#include "tsearch/ts_locale.h" #include "tsearch/ts_locale.h"
#include "utils/memutils.h" #include "utils/memutils.h"
...@@ -2399,6 +2400,9 @@ SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, char *word, int ...@@ -2399,6 +2400,9 @@ SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, char *word, int
char *notprobed; char *notprobed;
int compoundflag = 0; int compoundflag = 0;
/* since this function recurses, it could be driven to stack overflow */
check_stack_depth();
notprobed = (char *) palloc(wordlen); notprobed = (char *) palloc(wordlen);
memset(notprobed, 1, wordlen); memset(notprobed, 1, wordlen);
var = CopyVar(orig, 1); var = CopyVar(orig, 1);
......
...@@ -3919,6 +3919,9 @@ lseg_inside_poly(Point *a, Point *b, POLYGON *poly, int start) ...@@ -3919,6 +3919,9 @@ lseg_inside_poly(Point *a, Point *b, POLYGON *poly, int start)
bool res = true, bool res = true,
intersection = false; intersection = false;
/* since this function recurses, it could be driven to stack overflow */
check_stack_depth();
t.p[0] = *a; t.p[0] = *a;
t.p[1] = *b; t.p[1] = *b;
s.p[0] = poly->p[(start == 0) ? (poly->npts - 1) : (start - 1)]; s.p[0] = poly->p[(start == 0) ? (poly->npts - 1) : (start - 1)];
......
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
#include "catalog/pg_statistic.h" #include "catalog/pg_statistic.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h" #include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h" #include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h" #include "nodes/supportnodes.h"
...@@ -1338,6 +1339,9 @@ regex_selectivity_sub(const char *patt, int pattlen, bool case_insensitive) ...@@ -1338,6 +1339,9 @@ regex_selectivity_sub(const char *patt, int pattlen, bool case_insensitive)
int paren_pos = 0; /* dummy init to keep compiler quiet */ int paren_pos = 0; /* dummy init to keep compiler quiet */
int pos; int pos;
/* since this function recurses, it could be driven to stack overflow */
check_stack_depth();
for (pos = 0; pos < pattlen; pos++) for (pos = 0; pos < pattlen; pos++)
{ {
if (patt[pos] == '(') if (patt[pos] == '(')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment