# php_stream_read(...) may return less than expected

- Thread: https://externals.io/message/84636
- Original archive: https://news-web.php.net/php.internals/84636
- Started by Umberto Salsi on 2015-03-12T13:28:48+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #84636 — Umberto Salsi — 2015-03-12T13:28:48+00:00

URL: https://externals.io/message/84636#84636

Hi all,
I'm not a PHP internals developer, but this might be a bug spread here and
there in the source. This coding pattern:

if(php_stream_read(..., n) != n){
    php_error_docref(NULL TSRMLS_CC, E_SOMETHING, "Read error!");

seems wrong to me because there might be streams that returns less than
n bytes at once. When exactly n bytes are expected, not more not less,
a function similar to this one should be used instead:

int php_stream_read_fully(..., int n){
	continue calling php_stream_read() until exactly n bytes are
    collected, or a premature EOF, or I/O error
}

I ran into this issue passing a custom stream to `getimagesize()`:

getimagesize("myprotocol://");

where myprotocol:// may, in some situations, return less bytes than
expected (see the http://php.net/manual/en/class.streamwrapper.php
wrapper class, stream_read() method). Looking at the implementation of
this function in ext/standard/image.c, I just found the pattern above.
If this is really a bug, many other functions might be affected.

Regards,
 ___ 
/_|_\  Umberto Salsi
\/_\/  www.icosaedro.it

---

## #84637 — Michael Wallner — 2015-03-12T13:47:45+00:00

URL: https://externals.io/message/84636#84637 · In reply to: #84636

> Hi all,
> I'm not a PHP internals developer, but this might be a bug spread here and
> there in the source. This coding pattern:
> 
> if(php_stream_read(..., n) != n){
>     php_error_docref(NULL TSRMLS_CC, E_SOMETHING, "Read error!");
> 
> seems wrong to me because there might be streams that returns less than
> n bytes at once. When exactly n bytes are expected, not more not less,
> a function similar to this one should be used instead:
> 
> int php_stream_read_fully(..., int n){
> 	continue calling php_stream_read() until exactly n bytes are
>     collected, or a premature EOF, or I/O error
> }
> 
> I ran into this issue passing a custom stream to `getimagesize()`:
> 
> getimagesize("myprotocol://");
> 
> where myprotocol:// may, in some situations, return less bytes than
> expected (see the http://php.net/manual/en/class.streamwrapper.php
> wrapper class, stream_read() method). Looking at the implementation of
> this function in ext/standard/image.c, I just found the pattern above.
> If this is really a bug, many other functions might be affected.

Why does your stream behave non-blocking by default? Unless explicitly
stated, those callees probably expect a blocking stream.


-- 
Regards,
Mike
