mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	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.
This commit is contained in:
		@@ -881,7 +881,7 @@ other			.
 | 
			
		||||
					 * to forbid operator names like '?-' that could not be
 | 
			
		||||
					 * sequences of SQL operators.
 | 
			
		||||
					 */
 | 
			
		||||
					while (nchars > 1 &&
 | 
			
		||||
					if (nchars > 1 &&
 | 
			
		||||
						(yytext[nchars - 1] == '+' ||
 | 
			
		||||
						 yytext[nchars - 1] == '-'))
 | 
			
		||||
					{
 | 
			
		||||
@@ -889,12 +889,25 @@ other			.
 | 
			
		||||
 | 
			
		||||
						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();
 | 
			
		||||
 
 | 
			
		||||
@@ -798,7 +798,7 @@ other			.
 | 
			
		||||
					 * to forbid operator names like '?-' that could not be
 | 
			
		||||
					 * sequences of SQL operators.
 | 
			
		||||
					 */
 | 
			
		||||
					while (nchars > 1 &&
 | 
			
		||||
					if (nchars > 1 &&
 | 
			
		||||
						(yytext[nchars - 1] == '+' ||
 | 
			
		||||
						 yytext[nchars - 1] == '-'))
 | 
			
		||||
					{
 | 
			
		||||
@@ -806,12 +806,25 @@ other			.
 | 
			
		||||
 | 
			
		||||
						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)
 | 
			
		||||
 
 | 
			
		||||
@@ -687,20 +687,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)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user