diff options
author | Andrew Gierth <rhodiumtoad@postgresql.org> | 2018-08-23 20:00:12 +0100 |
---|---|---|
committer | Andrew Gierth <rhodiumtoad@postgresql.org> | 2018-08-23 21:33:55 +0100 |
commit | ad871a9d78841592938a0b24cc961fd1d4185607 (patch) | |
tree | f9049fded8395715073f56ae1bd25a1c8beb2d11 /src/backend | |
parent | 6120f2234d7699849a429e5881fdb242fb6b941e (diff) | |
download | postgresql-ad871a9d78841592938a0b24cc961fd1d4185607.tar.gz postgresql-ad871a9d78841592938a0b24cc961fd1d4185607.zip |
Reduce an unnecessary O(N^3) loop in lexer.
The lexer's handling of operators contained an O(N^3) hazard when
dealing with long strings of + or - characters; it seems hard to
prevent this case from being O(N^2), but the additional N multiplier
was not needed.
Backpatch all the way since this has been there since 7.x, and it
presents at least a mild hazard in that trying to do Bind, PREPARE or
EXPLAIN on a hostile query could take excessive time (without
honouring cancels or timeouts) even if the query was never executed.
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/parser/scan.l | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l index aa8299d69ef..bcc6a91e044 100644 --- a/src/backend/parser/scan.l +++ b/src/backend/parser/scan.l @@ -877,20 +877,33 @@ other . * to forbid operator names like '?-' that could not be * sequences of SQL operators. */ - while (nchars > 1 && - (yytext[nchars-1] == '+' || - yytext[nchars-1] == '-')) + if (nchars > 1 && + (yytext[nchars - 1] == '+' || + yytext[nchars - 1] == '-')) { int ic; - for (ic = nchars-2; ic >= 0; ic--) + for (ic = nchars - 2; ic >= 0; ic--) { - if (strchr("~!@#^&|`?%", yytext[ic])) + char c = yytext[ic]; + if (c == '~' || c == '!' || c == '@' || + c == '#' || c == '^' || c == '&' || + c == '|' || c == '`' || c == '?' || + c == '%') break; } - if (ic >= 0) - break; /* found a char that makes it OK */ - nchars--; /* else remove the +/-, and check again */ + if (ic < 0) + { + /* + * didn't find a qualifying character, so remove + * all trailing [+-] + */ + do { + nchars--; + } while (nchars > 1 && + (yytext[nchars - 1] == '+' || + yytext[nchars - 1] == '-')); + } } SET_YYLLOC(); |