]> granicus.if.org Git - postgresql/commitdiff
Reduce an unnecessary O(N^3) loop in lexer.
authorAndrew Gierth <rhodiumtoad@postgresql.org>
Thu, 23 Aug 2018 15:35:33 +0000 (16:35 +0100)
committerAndrew Gierth <rhodiumtoad@postgresql.org>
Thu, 23 Aug 2018 20:42:40 +0000 (21:42 +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 0cd782827ac8cfb9d73b72ce465585b5b33edf71..96f51bfd596daa17ec0e49f518fe1e02ba8c11ca 100644 (file)
@@ -885,20 +885,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 1cc587be34c25819f8584eda97c00e5927476646..989284dc6fe985baaa92b95aea1f1e7f141c874d 100644 (file)
@@ -817,20 +817,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 405dee73b039dc7fea0316ff1479bbbd65af36c3..9ad50b99119f248ef3fdf53ef39b86e1f36dbbed 100644 (file)
@@ -690,20 +690,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)