Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed incorrect handling of single quotes in SQL-Strings #842

Merged
merged 2 commits into from
Jan 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/SQLParserUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SQLParserUtils
const NAMED_TOKEN = '(?<!:):[a-zA-Z_][a-zA-Z0-9_]*';

// Quote characters within string literals can be preceded by a backslash.
const ESCAPED_SINGLE_QUOTED_TEXT = "'(?:[^'\\\\]|\\\\'?)*'";
const ESCAPED_SINGLE_QUOTED_TEXT = "'(?:[^'\\\\]|\\\\'?|'')*'";
const ESCAPED_DOUBLE_QUOTED_TEXT = '"(?:[^"\\\\]|\\\\"?)*"';
const ESCAPED_BACKTICK_QUOTED_TEXT = '`(?:[^`\\\\]|\\\\`?)*`';
const ESCAPED_BRACKET_QUOTED_TEXT = '\[(?:[^\]])*\]';
Expand Down
22 changes: 22 additions & 0 deletions tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public function dataGetPlaceholderPositions()
array('SELECT foo::date as date FROM Foo WHERE bar > :start_date AND baz > :start_date', false, array(46 => 'start_date', 68 => 'start_date')), // Ticket GH-259
array('SELECT `d.ns:col_name` FROM my_table d WHERE `d.date` >= :param1', false, array(57 => 'param1')), // Ticket DBAL-552
array('SELECT [d.ns:col_name] FROM my_table d WHERE [d.date] >= :param1', false, array(57 => 'param1')), // Ticket DBAL-552
array(
<<<'SQLDATA'
SELECT * FROM foo WHERE
bar = ':not_a_param1 ''":not_a_param2"'''
OR bar=:a_param1
OR bar=:a_param2||':not_a_param3'
OR bar=':not_a_param4 '':not_a_param5'' :not_a_param6'
OR bar=''
OR bar=':a_param3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. OR bar='' is correct and comparing to an empty string so the subsequent OR bar=':a_param3 is invalid SQL

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you are right. In fact it should not have found this parameter, but I guess the regex considered it as outside quotes because it did not end with a quote. But I am happy enough if it does not find parameters in a "correct" string.

SQLDATA
, false, array(74 => 'a_param1', 91 => 'a_param2', 191 => 'a_param3')
),

);
}

Expand Down Expand Up @@ -340,6 +353,15 @@ public function dataExpandListParameters()
array(1, null),
array(\PDO::PARAM_INT, \PDO::PARAM_NULL)
),
// DBAL-1205 - Escaped single quotes SQL- and C-Style
array(
"SELECT * FROM Foo WHERE foo = :foo||''':not_a_param''\\'' OR bar = ''':not_a_param''\\'':bar",
array(':foo' => 1, ':bar' => 2),
array(':foo' => \PDO::PARAM_INT, 'bar' => \PDO::PARAM_INT),
'SELECT * FROM Foo WHERE foo = ?||\'\'\':not_a_param\'\'\\\'\' OR bar = \'\'\':not_a_param\'\'\\\'\'?',
array(1, 2),
array(\PDO::PARAM_INT, \PDO::PARAM_INT)
),
);
}

Expand Down