Skip to content

Calling mysqli_stmt constructor leaks memory #8084

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

Closed
kamil-tekiela opened this issue Feb 11, 2022 · 2 comments
Closed

Calling mysqli_stmt constructor leaks memory #8084

kamil-tekiela opened this issue Feb 11, 2022 · 2 comments

Comments

@kamil-tekiela
Copy link
Member

Description

The following code:

<?php

$mysqli = new mysqli('localhost', 'user', 'password', 'test');

$stmt = $mysqli->prepare("SELECT 1");
$stmt->__construct($mysqli);

Resulted in this output:

[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_connection.c(685) :  Freeing 0x00007f0ba1c01188 (24 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(349) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.h(93) :  Freeing 0x00007f0ba1c011c0 (23 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(304) : Actual location (location was relayed)
Last leak repeated 3 times
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqli/mysqli_api.c(1885) :  Freeing 0x00007f0ba1c011f8 (24 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_auth.c(104) :  Freeing 0x00007f0ba1c01230 (21 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(94) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_connection.c(1565) :  Freeing 0x00007f0ba1c02780 (56 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(94) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/Zend/zend_string.h(150) :  Freeing 0x00007f0ba1c04870 (40 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
Last leak repeated 3 times
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_connection.c(687) :  Freeing 0x00007f0ba1c04a50 (34 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(349) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_commands.c(641) :  Freeing 0x00007f0ba1c5b6c0 (32 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(349) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_connection.c(1507) :  Freeing 0x00007f0ba1c5b780 (30 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(349) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/Zend/zend_hash.c(172) :  Freeing 0x00007f0ba1c6b480 (320 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqli/mysqli_api.c(1833) :  Freeing 0x00007f0ba1c780e0 (80 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(187) :  Freeing 0x00007f0ba1c78150 (72 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(117) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_statistics.c(218) :  Freeing 0x00007f0ba1c79600 (1304 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(110) :  Freeing 0x00007f0ba1c7c500 (1224 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(136) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_statistics.c(217) :  Freeing 0x00007f0ba1c830f0 (16 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(246) :  Freeing 0x00007f0ba1c87000 (376 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(136) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(193) :  Freeing 0x00007f0ba1c88000 (816 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(117) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(224) :  Freeing 0x00007f0ba1c8e140 (272 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(136) : Actual location (location was relayed)
[Fri Feb 11 21:38:50 2022]  Script:  '/mnt/c/wamp64/www/formatter/rubbish.php'
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_driver.c(267) :  Freeing 0x00007f0ba1c8e280 (232 bytes), script=/mnt/c/wamp64/www/formatter/rubbish.php
/mnt/d/projects/php-src/ext/mysqlnd/mysqlnd_alloc.c(136) : Actual location (location was relayed)
=== Total 25 memory leaks detected ===

But I expected this output instead:

PHP Version

PHP 8.0, PHP 8.1, master

Operating System

No response

@cmb69
Copy link
Member

cmb69 commented Feb 14, 2022

Same here on Windows.

@cmb69
Copy link
Member

cmb69 commented Feb 17, 2022

At least one problem is that mysqli_stmt::__construct() allocates memory for the underlying structures, instead of allocation that memory in mysqli_objects_new(). So when the constructor is called manually, that memory leaks. I presume that other mysqli objects have the same issue. Given that calling the constructor manually is pretty uncommon, it might not be worth fixing this for any of the stable versions, but rather to target "master".

nielsdos added a commit to nielsdos/php-src that referenced this issue Feb 23, 2025
Calling the constructor twice has no real world benefit.
Block it to fix these two issues.
We also clean up the constructor code a bit:
1) `in_ctor` implies `object` exists
2) We no longer need to deal with reusing internal data.

Closes phpGH-17900.
Closes phpGH-8084.
nielsdos added a commit to nielsdos/php-src that referenced this issue Feb 23, 2025
Calling the constructor twice has no real world benefit.
Block it to fix these two issues.
We also clean up the constructor code a bit:
- `in_ctor` implies `object` exist.
- We surround the instance check with ZEND_DEBUG to avoid a runtime
  penalty.

Closes phpGH-17900.
Closes phpGH-8084.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants