Skip to content

Commit

Permalink
Fix #12 memory leak in dio_read
Browse files Browse the repository at this point in the history
  • Loading branch information
remicollet committed Jan 23, 2024
1 parent 3fd676e commit e040398
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 1 addition & 2 deletions php7/dio.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,9 @@ PHP_FUNCTION(dio_read)
RETURN_NULL();
}

data = erealloc(data, res + 1);
data[res] = 0;

RETURN_STRINGL(data, res);
RETVAL_STRINGL(data, res);
efree(data);
}
/* }}} */
Expand Down
26 changes: 26 additions & 0 deletions tests/gh12.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-12: Memory leak in dio_read
--SKIPIF--
<?php
if (!extension_loaded("dio")) print "skip";
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') print "skip Linux only";
?>
--INI--
memory_limit=10M
--FILE--
<?php
file_put_contents(__DIR__ . '/dio-test-file', str_repeat('0', 10 * 1024));
$hFile = dio_open(__DIR__ . '/dio-test-file', O_RDONLY);

for($i=0 ; $i<10000; $i++) {
dio_seek($hFile, 0);
$data = dio_read($hFile, 10000);
}
?>
Done
--CLEAN--
<?php
@unlink(__DIR__ . '/dio-test-file');
?>
--EXPECT--
Done

0 comments on commit e040398

Please sign in to comment.