Skip to content

Commit dc45332

Browse files
committed
Add regression test for GH-24578
1 parent a11e54e commit dc45332

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

‎ext/opcache/tests/jit/gh24578.phpt‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
--TEST--
2+
GH-24578 (Function JIT produces an incorrect result in a string-scanning loop)
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit_buffer_size=64M
8+
opcache.jit=function
9+
--EXTENSIONS--
10+
opcache
11+
--FILE--
12+
<?php
13+
final class Repro {
14+
public static function skipComment(string $sql, int $position): int {
15+
$byte = $sql[$position];
16+
$next = $sql[$position + 1] ?? '';
17+
if ($byte === '/' && $next === '*') {
18+
$end = strpos($sql, '*/', $position + 2);
19+
return $end === false ? strlen($sql) : $end + 2;
20+
}
21+
$dashes = $byte === '-' && $next === '-' && strspn($sql[$position + 2] ?? "\n", " \t\r\n") === 1;
22+
if ($byte !== '#' && !$dashes) {
23+
return $position;
24+
}
25+
$end = strpos($sql, "\n", $position);
26+
return $end === false ? strlen($sql) : $end + 1;
27+
}
28+
29+
public static function skipQuoted(string $sql, int $position): int {
30+
$quote = $sql[$position];
31+
$length = strlen($sql);
32+
$stops = $quote === '`' ? '`' : $quote . '\\';
33+
$position++;
34+
while ($position < $length) {
35+
$position += strcspn($sql, $stops, $position);
36+
if ($position >= $length) {
37+
break;
38+
}
39+
if ($sql[$position] === '\\') {
40+
$position += 2;
41+
} elseif (($sql[$position + 1] ?? '') === $quote) {
42+
$position += 2;
43+
} else {
44+
return $position + 1;
45+
}
46+
}
47+
return $length;
48+
}
49+
50+
public static function interpolate(string $sql, array $params): ?string {
51+
$length = strlen($sql);
52+
$count = count($params);
53+
$text = '';
54+
$used = 0;
55+
$copied = 0;
56+
$position = 0;
57+
while ($position < $length) {
58+
$position += strcspn($sql, "?'\"`#-/", $position);
59+
if ($position >= $length) {
60+
break;
61+
}
62+
$byte = $sql[$position];
63+
if ($byte === '?') {
64+
$text .= substr($sql, $copied, $position - $copied) . (string) $params[$used];
65+
$used++;
66+
$position++;
67+
$copied = $position;
68+
} elseif ($byte === "'" || $byte === '"' || $byte === '`') {
69+
$position = self::skipQuoted($sql, $position);
70+
} else {
71+
$after = self::skipComment($sql, $position);
72+
$position = $after === $position ? $position + 1 : $after;
73+
}
74+
}
75+
return $used === $count ? $text . substr($sql, $copied) : null;
76+
}
77+
}
78+
79+
var_dump(Repro::interpolate('SELECT ? AS v', [3]));
80+
var_dump(Repro::interpolate('SELECT 5 -', []));
81+
var_dump(Repro::interpolate('SELECT 5 / 2', []));
82+
?>
83+
--EXPECT--
84+
string(13) "SELECT 3 AS v"
85+
string(10) "SELECT 5 -"
86+
string(12) "SELECT 5 / 2"

0 commit comments

Comments
 (0)