Skip to content

Commit a7223e7

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: ext/mysqli: Fix stmt->query leak in mysqli_execute_query() validation errors.
2 parents 237932f + d3a70ea commit a7223e7

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

ext/mysqli/mysqli_api.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,10 @@ PHP_FUNCTION(mysqli_execute_query)
530530
MYSQLND_PARAM_BIND *params;
531531

532532
if (!zend_array_is_list(input_params)) {
533+
if (stmt->query) {
534+
efree(stmt->query);
535+
stmt->query = NULL;
536+
}
533537
mysqli_stmt_close(stmt->stmt, false);
534538
stmt->stmt = NULL;
535539
efree(stmt);
@@ -540,6 +544,10 @@ PHP_FUNCTION(mysqli_execute_query)
540544
hash_num_elements = zend_hash_num_elements(input_params);
541545
param_count = mysql_stmt_param_count(stmt->stmt);
542546
if (hash_num_elements != param_count) {
547+
if (stmt->query) {
548+
efree(stmt->query);
549+
stmt->query = NULL;
550+
}
543551
mysqli_stmt_close(stmt->stmt, false);
544552
stmt->stmt = NULL;
545553
efree(stmt);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
mysqli_execute_query() does not leak stmt->query on input_params validation errors with MYSQLI_REPORT_INDEX
3+
--EXTENSIONS--
4+
mysqli
5+
--SKIPIF--
6+
<?php
7+
require_once 'skipifconnectfailure.inc';
8+
?>
9+
--FILE--
10+
<?php
11+
12+
require 'table.inc';
13+
14+
mysqli_report(MYSQLI_REPORT_INDEX);
15+
16+
try {
17+
$link->execute_query('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?', ['foo', 42]);
18+
} catch (ValueError $e) {
19+
echo '[001] '.$e->getMessage()."\n";
20+
}
21+
22+
try {
23+
$link->execute_query('SELECT label, ? AS anon, ? AS num FROM test WHERE id=?', ['foo' => 42]);
24+
} catch (ValueError $e) {
25+
echo '[002] '.$e->getMessage()."\n";
26+
}
27+
28+
print "done!";
29+
?>
30+
--CLEAN--
31+
<?php
32+
require_once 'clean_table.inc';
33+
?>
34+
--EXPECT--
35+
[001] mysqli::execute_query(): Argument #2 ($params) must consist of exactly 3 elements, 2 present
36+
[002] mysqli::execute_query(): Argument #2 ($params) must be a list array
37+
done!

0 commit comments

Comments
 (0)