]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Reduce an unnecessary O(N^3) loop in lexer.
authorAndrew Gierth <rhodiumtoad@postgresql.org>
Thu, 23 Aug 2018 19:00:50 +0000 (20:00 +0100)
committerAndrew Gierth <rhodiumtoad@postgresql.org>
Thu, 23 Aug 2018 20:43:55 +0000 (21:43 +0100)
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.

src/backend/parser/scan.l
src/fe_utils/psqlscan.l
src/interfaces/ecpg/preproc/pgc.l

index 634bfa512f4fff37e3010dfedc68fd5e49fb6b3b..c1767efabb61d71cc81bc284b58de8f0eaef91f2 100644 (file)
@@ -881,20 +881,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--)
                                                {
-                                                       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();
index 27689d72da866a99e2d0c98a4ef6522766830a15..e6c7938f7527bd1b1a4375f11493e0e77d776cde 100644 (file)
@@ -800,20 +800,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--)
                                                {
-                                                       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] == '-'));
+                                               }
                                        }
 
                                        if (nchars < yyleng)
index 3598a200d0db431541ad1fb90c1950ebfc77f4ed..0a90bead921eb403d64f79e5ad08d0eb84d3a8bd 100644 (file)
@@ -686,20 +686,33 @@ cppline                   {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
                                                 * 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] == '-'));
+                                                       }
                                                }
 
                                                if (nchars < yyleng)