escapeReserved function cannot escape sql like below:
UPDATE {users} SET uid=uid - uid WHERE (name = :db_condition_placeholder_0)
It returns UPDATE "USERS" SET "UID"=uid - "UID" WHERE (name = :db_condition_placeholder_0).
It should be UPDATE "USERS" SET "UID"="UID" - "UID" WHERE (name = :db_condition_placeholder_0).

The sql comes from file of "DRUPAL_ROOT./modules/system/system.admin.inc" line 2296 in Drupal 7.12, It excuted when you click "admin/reports/status".

I made some change and works for me:

  private function escapeReserved($query)
  {
        $ddl= !((boolean)preg_match('/^(select|insert|update|delete)/i',$query));
        $search = array ("/({)(\w+)(})/e", // escapes all table names
                         "/({L#)([0-9]+)(})/e", // escapes long id
		                 "/(\:)(uid|session|file|access|mode|comment|desc|size|start|end)/e",
			   	         "/(<uid>|<session>|<file>|<access>|<mode>|<comment>|<desc>|<size>".($ddl?'':'|<date>').")/e",
			   	         '/([\(\.\s,\=])(uid|session|file|access|mode|comment|desc|size'.($ddl?'':'|date').')([,\s\=)])/e',
                         '/([\(\.\s,])(uid|session|file|access|mode|comment|desc|size'.($ddl?'':'|date').')$/e',
        				 '/(\=)(uid)(\s)/e');

	    $replace = array ("'\"\\1'.strtoupper('\\2').'\\3\"'",
				          "'\"\\1'.strtoupper('\\2').'\\3\"'",
				          "'\\1'.'db_'.'\\2'.'\\3'",
					      "strtoupper('\"\\1\"')",
					      "'\\1'.strtoupper('\"\\2\"').'\\3'",
	                      "'\\1'.strtoupper('\"\\2\"')",
	    				  "'\\1'.strtoupper('\"\\2\"')");

        return preg_replace($search, $replace, $query);  	
  }

Comments

brianV’s picture

Version: 7.x-1.12 » 7.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new1.23 KB
new2.35 KB

Rolled this into two patches. The -no-whitespace-fixes file includes just the updates to the regex itself. The second makes the whitespace line up and look consistent with the rest of the file.

So the -no-whitespace-fixes patch can be used to actually see what the regex changes were. The other is the one that I actually want to have committed.

I tested this fix, and it works exactly as described.

brianV’s picture

Title: escapeReserved work incorrectly » escapeReserved doesn't escape multiple arguments in an expression

updated title to something more descriptive.

bohart’s picture

Status: Needs review » Closed (outdated)

D7 reached its EOL back in January 2025, and there is no active release for D7 for this module anymore.
Development or support is not planned for D7. All D7-related issues are marked as outdated in a bunch.

Everyone can apply the patches/suggestions above (not tested by the maintainers, tested by the community) to their D7 projects.
If the issue remains relevant for D10+ versions, merge requests with proposed solutions for a new module version (D10+) are welcome in a new follow-up issue.

Thanks!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.