Skip to content

Memory leak with consecutive ftp_nb_fget #10562

Closed
@nielsdos

Description

@nielsdos

Description

The following code:

<?php

include "ext/ftp/tests/server.inc";

$conn_id = ftp_connect("127.0.0.1", $port) or die("no connection to ftp");

ftp_login($conn_id, 'anonymous', 'IEUser@');

$fout = fopen("test-output.tmp", "w");
$ret = ftp_nb_fget($conn_id, $fout, "fget", FTP_BINARY, 10);
$ret = ftp_nb_fget($conn_id, $fout, "fget", FTP_BINARY, 10);
fclose($fout);

Resulted in this output:

Warning: ftp_nb_fget(): Closing data Connection. in /home/niels/php-src/memleak.php on line 11
[Fri Feb 10 21:53:49 2023]  Script:  '/home/niels/php-src/memleak.php'
/home/niels/php-src/ext/ftp/ftp.c(1631) :  Freeing 0x00007f3176889000 (4108 bytes), script=/home/niels/php-src/memleak.php
=== Total 1 memory leaks detected ===

But I expected this output instead:

Warning: ftp_nb_fget(): Closing data Connection. in /home/niels/php-src/memleak.php on line 11

Seems like the ftp->data pointer gets overwritten before the previous action is done(? not sure, I haven't looked into detail). In any case the pointer is overwritten and the old one isn't freed.
Discovered while developing PR #10525.

There's actually a couple of places where ftp->data could get overwritten I think. Maybe we shouldn't allow doing two non-blocking actions at the same time?

PHP Version

PHP >=8.1

Operating System

Linux

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions