php / pecl-mail-mailparse

Email message manipulation
http://pecl.php.net/package/mailparse
Other
29 stars 30 forks source link

Memory leak when --enable-debug on PHP #32

Open youkidearitai opened 1 year ago

youkidearitai commented 1 year ago

Description

Compile to PHP with --enable-debug, then displays memory leak detected. Also tests failed.

=====================================================================
FAILED TEST SUMMARY
---------------------------------------------------------------------
mailparse_msg_free causes double free segfault [tests/bug001.phpt]
Bug #73110 (Mails with unknown MIME version are treated as plain/text) [tests/bug73110.phpt]
Fix #74233 (Parsing multi Content-Disposition causes memory leak) [tests/bug74223.phpt]
GH issue #21 (Segmentation fault with mailparse_msg_create()) [tests/gh21.phpt]
GH issue #22 (Segmentation fault with mailparse_msg_create()) [tests/gh22.phpt]
GH issue #24 (Segmentation fault with mailparse_msg_create()) [tests/gh24.phpt]
=====================================================================
$ cat tests/*.diff  | head
     No Segfault!
002+ [Tue Aug 29 10:21:32 2023]  Script:  '/home/ubuntu/src/pecl-mail-mailparse/tests/bug001.php'
003+ /home/ubuntu/src/pecl-mail-mailparse/php_mailparse_mime.c(607) :  Freeing 0x0000ffffac603028 (4 bytes), script=/home/ubuntu/src/pecl-mail-mailparse/tests/bug001.php
004+ Last leak repeated 3 times
005+ [Tue Aug 29 10:21:32 2023]  Script:  '/home/ubuntu/src/pecl-mail-mailparse/tests/bug001.php'
006+ /home/ubuntu/src/pecl-mail-mailparse/php_mailparse_mime.c(466) :  Freeing 0x0000ffffac603118 (7 bytes), script=/home/ubuntu/src/pecl-mail-mailparse/tests/bug001.php
007+ [Tue Aug 29 10:21:32 2023]  Script:  '/home/ubuntu/src/pecl-mail-mailparse/tests/bug001.php'
008+ /home/ubuntu/src/php-src/Zend/zend_hash.c(281) :  Freeing 0x0000ffffac604840 (56 bytes), script=/home/ubuntu/src/pecl-mail-mailparse/tests/bug001.php
009+ Last leak repeated 10 times
010+ [Tue Aug 29 10:21:32 2023]  Script:  '/home/ubuntu/src/pecl-mail-mailparse/tests/bug001.php'

I know deleted dtor values of bd4a675032f778d676e1244cd0880c104526b4c9. Is there a problem? If no problem, I want to revert them.

Excepted

Fix memory leaks.

youkidearitai commented 1 year ago

For manual https://www.php.net/manual/en/function.mailparse-msg-create.php recommend to use mailparse_msg_free.

It is recommended to call mailparse_msg_free() on the result of this function, when it is no longer needed, to avoid memory leaks.

remicollet commented 1 year ago

No leak reported for Release build


=====================================================================
TIME END 2023-08-29 07:16:25

=====================================================================
TEST RESULT SUMMARY
---------------------------------------------------------------------
Exts skipped    :    0
Exts tested     :   79
---------------------------------------------------------------------

Number of tests :   28                28
Tests skipped   :    0 (  0.0%) --------
Tests warned    :    0 (  0.0%) (  0.0%)
Tests failed    :    0 (  0.0%) (  0.0%)
Expected fail   :    1 (  3.6%) (  3.6%)
Tests leaked    :    0 (  0.0%) (  0.0%)
Tests passed    :   27 ( 96.4%) ( 96.4%)
---------------------------------------------------------------------
Time taken      :   53 seconds
=====================================================================
youkidearitai commented 1 year ago

From PHP Internals book, it is printed on stderr output during debug builds and nothing during release builds.

I think 2 problems.

  1. Difficult to debug.
  2. If found Zend Engine a bug for memory manager, that means real memory leaks.

Therefore, I think resolve this issue.

cmb69 commented 3 months ago

I suggest to double check this with a good memory leak detector (e.g. valgrind or MSan), using USE_ZEND_ALLOC=0; see also https://www.phpinternalsbook.com/php7/memory_management/memory_debugging.html.

cmb69 commented 3 months ago

Indeed, https://github.com/php/pecl-mail-mailparse/commit/bd4a675032f778d676e1244cd0880c104526b4c9 isn't correct; it causes the memory leaks.