Bug #75868 [Com]: is_resource() gettype() get_resources() inconsistent result on closed resource

From: Date: Thu, 25 Jan 2018 15:19:48 +0000
Subject: Bug #75868 [Com]: is_resource() gettype() get_resources() inconsistent result on closed resource
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213705@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75868&edit=1

 ID:                 75868
 Comment by:         spam2 at rhsoft dot net
 Reported by:        bobonov at gmail dot com
 Summary:            is_resource() gettype() get_resources() inconsistent
                     result on closed resource
 Status:             Not a bug
 Type:               Bug
 Package:            Variables related
 Operating System:   Ubuntu 17.10
 PHP Version:        7.2.1
 Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

and how do you imagine is_resource() to be useful when it answers yes ona closed one which
can't be used in the next line?

changing that would be a regression and break existing code for no purpose


Previous Comments:
------------------------------------------------------------------------
[2018-01-25 15:16:39] bobonov at gmail dot com

In the title I reported it as "inconsistent result on closed resource".

I know that is the intend behavior, as clearly reported in the documentation, but anyway it is a
bug.

While 2 function report it as still a resource, is_resource() say is not a resource.

Yes gettype() report it as "resource (closed)" which in my opinion is a strange type since
closed denote a status not a variable type, or at least resource type

We have 3 different result checking the same variable
is_resource() say is a resource
gettype() report "resource (closed)" (which in php does not exist)
get_resources() which still list it as resource

To make more clear the inconsistent type checking by different function for a resource I made a bit
more complete script (in which you can see that var_dump() get_resource_type() still see closed one
as resource).
In particular get_resource_type() give  a warning when used to test a non resource type.
"PHP Warning:  get_resource_type() expects parameter 1 to be resource, string given"
So if even the runtime see closed resource as resource... it is a resource

<?php
function testResource($a) {
    echo "$a\n";
    echo "gettype(): " . gettype($a)."\n";
    echo "is_resource():";
    var_dump(is_resource($a));
    echo "get_resource_type(): " . get_resource_type ( $a )."\n";
    echo "get_resources():";
    print_r(get_resources());
    echo "var_dump():";
    var_dump($a);
}

$a = fopen('http://www.google.com',
'r');
echo "OPEN resource\n";

testResource($a);
fclose($a);
echo "\n\n\nCLOSED resource\n";

testResource($a);
?>

this script will produce the following result:
OPEN resource
Resource id #6
gettype(): resource
is_resource():bool(true)
get_resource_type(): stream
get_resources():Array
(
    [1] => Resource id #1
    [2] => Resource id #2
    [3] => Resource id #3
    [4] => Resource id #4
    [6] => Resource id #6
)
var_dump():resource(6) of type (stream)


CLOSED resource
Resource id #6
gettype(): resource (closed)
is_resource():bool(false)
get_resource_type(): Unknown
get_resources():Array
(
    [1] => Resource id #1
    [2] => Resource id #2
    [3] => Resource id #3
    [4] => Resource id #4
    [6] => Resource id #6
)
var_dump():resource(6) of type (Unknown)


Probably the correct output (to make everything consistent and logic) for a closed resource should
be:
gettype(): resource
is_resource():bool(true)
get_resource_type(): closed
var_dump():resource(6) of type (closed)

All "resource provider" should change a resource type to closed when *close() is called.

------------------------------------------------------------------------
[2018-01-25 13:41:46] cmb@php.net

> Checking the closed resource with:
> is_resource() return false.

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

This is documented[1] behavior:

| is_resource() is not a strict type-checking method: it will
| return FALSE if var is a resource variable that has been closed.

[1] <http://www.php.net/manual/en/function.is-resource.php#refsect1-function.is-resource-notes>

------------------------------------------------------------------------
[2018-01-24 14:31:43] bobonov at gmail dot com

Description:
------------
Checking the closed resource with:
gettype() return resource (closed)
get_resources() list it as resource
is_resource() return false.

There is an ancient Bug #42797 (php 4.3.6) that is relevant to this bug

Test script:
---------------
<?php
$a = fopen('http://www.google.com',
'r');
echo "$a : the type is " . gettype($a)."\n";  // resource
var_dump(is_resource($a)); // true
print_r(get_resources());

fclose($a);
echo "$a : the type is " . gettype($a)."\n";  // resource closed
var_dump(is_resource($a)); // false
print_r(get_resources());


Expected result:
----------------
I expect is_resource() to return true since  get_resources() and gettype() see the var as resource

Actual result:
--------------
Resource id #6 : the type is resource
bool(true)
Array
(
    [1] => Resource id #1
    [2] => Resource id #2
    [3] => Resource id #3
    [4] => Resource id #4
    [6] => Resource id #6
)
Resource id #6 : the type is resource (closed)
bool(false)
Array
(
    [1] => Resource id #1
    [2] => Resource id #2
    [3] => Resource id #3
    [4] => Resource id #4
    [6] => Resource id #6
)



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=75868&edit=1


Thread (7 messages)

« previous php.bugs (#213705) next »