Skip to content

Commit f95a90b

Browse files
authored
Fix query simulation leading to query error (#549)
1 parent 10c2200 commit f95a90b

File tree

7 files changed

+75
-1
lines changed

7 files changed

+75
-1
lines changed

.phpstan-dba-mysqli.cache

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/QueryReflection/QueryReflection.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,13 @@ public function resolveQueryStrings(Expr $queryExpr, Scope $scope): iterable
280280

281281
$queryString = $this->resolveQueryExpr($queryExpr, $scope);
282282
if (null !== $queryString) {
283-
yield QuerySimulation::stripComments($this->normalizeQueryString($queryString));
283+
$normalizedQuery = QuerySimulation::stripComments($this->normalizeQueryString($queryString));
284+
285+
// query simulation might lead in a invalid query, skip those
286+
$error = $this->validateQueryString($normalizedQuery);
287+
if ($error === null) {
288+
yield $normalizedQuery;
289+
}
284290
}
285291
}
286292

tests/rules/SyntaxErrorInQueryMethodRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,9 @@ public function testBugRexstan165()
304304
{
305305
$this->analyse([__DIR__ . '/data/bug-rexstan-165.php'], []);
306306
}
307+
308+
public function testBug547(): void
309+
{
310+
$this->analyse([__DIR__ . '/data/bug-547.php'], []);
311+
}
307312
}

tests/rules/UnresolvableQueryMethodRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ public function testBug536(): void
6868
{
6969
$this->analyse([__DIR__ . '/data/bug-536.php'], []);
7070
}
71+
72+
public function testBug547(): void
73+
{
74+
$this->analyse([__DIR__ . '/data/bug-547.php'], []);
75+
}
7176
}

tests/rules/config/.phpunit-phpstan-dba-mysqli.cache

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/rules/config/.phpunit-phpstan-dba-pdo-mysql.cache

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/rules/data/bug-547.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bug547;
4+
5+
use PDO;
6+
7+
function taintEscapedAndInferencePlaceholder(PDO $pdo, string $s)
8+
{
9+
10+
$pdo->query('SELECT * FROM ' . X::escapeIdentifier($s), PDO::FETCH_ASSOC);
11+
}
12+
13+
class X {
14+
/**
15+
* Escapes and adds backsticks around.
16+
*
17+
* @param string $name
18+
*
19+
* @return string
20+
*
21+
* @psalm-taint-escape sql
22+
*/
23+
public static function escapeIdentifier($name)
24+
{
25+
return '';
26+
}
27+
28+
}

0 commit comments

Comments
 (0)