php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79276 PDO_API int pdo_parse_params() ignores any placeholder between two '\'
Submitted: 2020-02-15 00:19 UTC Modified: 2024-06-18 16:02 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: v-yitam at microsoft dot com Assigned: mbeccati (profile)
Status: Closed Package: PDO Core
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
 [2020-02-15 00:19 UTC] v-yitam at microsoft dot com
Description:
------------
PDO_API int pdo_parse_params() in ext/pdo/pdo_sql_parser.re ignores (skips) any placeholder in between two backslashes `\` 

If I replaced the backslash `\` with forward slash '/' in the query, the query succeeded and returned the row as expected.

If the following line is removed the 'problem' is gone:
https://github.com/php/php-src/blob/master/ext/pdo/pdo_sql_parser.re#L59

If this is intentional (by design), please explain or let us know if there is a workaround.



Test script:
---------------
$dbh = new PDO("sqlsrv:server=$server;Database = $db", $uid, $pwd);

$tableName = 'testPDO';
$create_sql = "CREATE TABLE $tableName(id int NOT NULL, langcode varchar(12), revision_id int, [path] nvarchar(255), [alias] nvarchar(255))"
$dbh->exec($create_sql1);

$insert_sql = "INSERT INTO $tableName(id, langcode, revision_id, [path], [alias]) VALUES (4, 'en', 4, '/node/3', '/')";
$dbh->exec($insert_sql);

$sql = "SELECT * FROM $tableName WHERE [path] LIKE :path ESCAPE '\' AND [langcode] like :lang ESCAPE '\'";
$args = [
    ':path' => '%node%',
    ':lang' => 'en'
];

$sth = $dbh->prepare($sql);
$sth->execute($args);
$row = $sth->fetch(PDO::FETCH_NUM);
var_dump($row);


Expected result:
----------------
array(5) {
  [0]=>
  string(1) "4"
  [1]=>
  string(2) "en"
  [2]=>
  string(1) "4"
  [3]=>
  string(7) "/node/3"
  [4]=>
  string(1) "/"
}


Actual result:
--------------
PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\Workspace\Test\pdo_1093.php:53

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-02-15 15:23 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Feature/Change Request +Type: Bug
 [2020-02-15 15:23 UTC] cmb@php.net
> If this is intentional (by design), please explain or let us
> know if there is a workaround.

These regexps have been introduced to fix[1] bug #41125 and bug
#44251.  Basically, sacrificing some compatibility with standard
SQL in favor of support for MySQL's proprietary backslash
escaping[2].

Proper standard conforming rules would be something like

    (["](ANYNOEOF\["]|"")*["]) { RET(PDO_PARSER_TEXT); }
    (['](ANYNOEOF\[']|'')*[']) { RET(PDO_PARSER_TEXT); }

Obviously, this could break queries which use backslash escapes.

[1] <http://git.php.net/?p=php-src.git;a=commit;h=1f54af9245c35f2ffdc8c708da9c8552ecada4f8>
[2] <https://dev.mysql.com/doc/refman/8.0/en/string-literals.html>
 [2020-02-19 16:18 UTC] v-yitam at microsoft dot com
Thanks for your prompt reply. Just wondering if this is going to be fixed or left as "by design"?
 [2020-02-19 20:04 UTC] beakerboy99 at yahoo dot com
I'm the person who originlly submitted this as a bug report to Microsoft, who then passed it on to the PHP-PDO team. I am updating the SQL Server driver for the Drupal CMS. Drupal uses backslash-escaped strings for LIKE expressions as its standard. SQL Server does not as its default, but can when specified in the expression as `field LIKE :parameter_x ESCAPE '\'`. When several LIKE expressions are used in one query, an exception is thrown. The Drupal core testsuite includes a test for this situation.

Other databases use the `ESCAPE '{delimiter}'` syntax, so this bug will likely affect those drivers as well.
 [2020-02-20 08:22 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=349240
Log: Note that PDO parser supports backslash escapes

Cf. bug #79276.
 [2020-02-20 11:21 UTC] mumumu@php.net
Automatic comment from SVN on behalf of mumumu
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=349243
Log: Note that PDO parser supports backslash escapes

Cf. bug #79276.
 [2020-02-20 12:04 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #79276: PDO ignores any placeholder between two '\'
On GitHub:  https://github.com/php/php-src/pull/5190
Patch:      https://github.com/php/php-src/pull/5190.patch
 [2020-02-20 12:05 UTC] cmb@php.net
In my opionion, this should be fixed, but for BC reasons only for
PHP 8.  Let's see where the PR goes. :)
 [2020-02-20 17:26 UTC] v-yitam at microsoft dot com
Thank you I've verified that this fix solves the problem. Glad to hear that this PR is planned to be merged (despite the wait ;) )
 [2020-11-12 17:19 UTC] sartorua at gmail dot com
Is there any milestone or roadmap where it will be fixed?
 [2024-06-18 16:02 UTC] mbeccati@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: mbeccati
 [2024-06-18 16:02 UTC] mbeccati@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

Fixed in https://github.com/php/php-src/commit/fbe317bf2179c65b750cc945c3530b28db1670e0

The fix will be part of 8.4
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 22:01:33 2024 UTC