Skip to content

Fix bug #60110 (fclose(), file_put_contents(), copy() do not return false properly) #12067

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,8 @@ PHP_FUNCTION(file_put_contents)
numbytes = -1;
break;
}
php_stream_close(stream);

if (numbytes < 0) {
if (php_stream_close(stream) || numbytes < 0) {
RETURN_FALSE;
}

Expand Down Expand Up @@ -782,11 +781,9 @@ PHPAPI PHP_FUNCTION(fclose)
RETURN_FALSE;
}

php_stream_free(stream,
RETURN_BOOL(!php_stream_free(stream,
PHP_STREAM_FREE_KEEP_RSRC |
(stream->is_persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE));

RETURN_TRUE;
(stream->is_persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE)));
}
/* }}} */

Expand Down Expand Up @@ -1617,11 +1614,11 @@ PHPAPI int php_copy_file_ctx(const char *src, const char *dest, int src_flg, php
if (srcstream && deststream) {
ret = php_stream_copy_to_stream_ex(srcstream, deststream, PHP_STREAM_COPY_ALL, NULL);
}
if (srcstream) {
php_stream_close(srcstream);
if (srcstream && php_stream_close(srcstream)) {
ret = FAILURE;
}
if (deststream) {
php_stream_close(deststream);
if (deststream && php_stream_close(deststream)) {
ret = FAILURE;
}
return ret;
}
Expand Down
94 changes: 94 additions & 0 deletions ext/standard/tests/file/bug60110.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
--TEST--
Bug #60110 (fclose(), file_put_contents(), copy() do not return false properly)
--FILE--
<?php
class astream {
public static $max = 100;

public $context;

protected $read = 0;

protected $flush = false;

function stream_open($path, $mode) {
$this->flush = basename($path) === 'flush';
return true;
}

function stream_write($data) {
var_dump($data);
return strlen($data);
}

function stream_read($length) {
if ($length > self::$max - $this->read) {
$length = self::$max - $this->read;
}
$this->read += $length;
return str_repeat('a', $length);
}

function stream_tell() {
return $this->read;
}

function stream_eof() {
return $this->read == self::$max;
}

function stream_flush() {
return $this->flush;
}

function stream_stat() {
return fstat(fopen('php://memory', "r"));
}

function url_stat() {
return fstat(fopen('php://memory', "r"));
}
}

stream_wrapper_register('as', 'astream');

$stream = fopen('as://flush', 'r+');
var_dump(fwrite($stream, "data"));
var_dump(fread($stream, 3));
var_dump(fclose($stream));

$stream = fopen('as://nothing', 'r+');
var_dump(fwrite($stream, "data"));
var_dump(fread($stream, 3));
var_dump(fclose($stream));

var_dump(file_put_contents('as://', 'test nothing'));
var_dump(file_put_contents('as://flush', 'test flush'));

$path = __DIR__ . '/bug60110_test_file.txt';
var_dump(file_put_contents($path, 'sdata'));
var_dump(copy($path, 'as://nothing'));
var_dump(copy($path, 'as://flush'));
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/bug60110_test_file.txt');
?>
--EXPECT--
string(4) "data"
int(4)
string(3) "aaa"
bool(true)
string(4) "data"
int(4)
string(3) "aaa"
bool(false)
string(12) "test nothing"
bool(false)
string(10) "test flush"
int(10)
int(5)
string(5) "sdata"
bool(false)
string(5) "sdata"
bool(true)
9 changes: 6 additions & 3 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,12 @@ fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remov
(close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0);
#endif

int flush_ret;
if (stream->flags & PHP_STREAM_FLAG_WAS_WRITTEN || stream->writefilters.head) {
/* make sure everything is saved */
_php_stream_flush(stream, 1);
flush_ret = _php_stream_flush(stream, 1);
} else {
flush_ret = 0;
}

/* If not called from the resource dtor, remove the stream from the resource list. */
Expand All @@ -472,10 +475,10 @@ fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remov
Let's let the cookie code clean it all up.
*/
stream->in_free = 0;
return fclose(stream->stdiocast);
return fclose(stream->stdiocast) || flush_ret;
}

ret = stream->ops->close(stream, preserve_handle ? 0 : 1);
ret = stream->ops->close(stream, preserve_handle ? 0 : 1) || flush_ret;
stream->abstract = NULL;

/* tidy up any FILE* that might have been fdopened */
Expand Down