Re: Segfault when assigning a reference variable
| From: | Johannes Schlüter | Date: | Sat, 08 Mar 2014 13:33:30 +0000 |
| Subject: | Re: Segfault when assigning a reference variable | ||
| References: | 1 2 3 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-11612@lists.php.net to get a copy of this message | ||
On Fri, 2014-03-07 at 16:16 -0400, JGM wrote:
> I'm emulating the same behavior of wxWidgets since the code is being auto
> generated and I want to keep compatibility as much as possible with how
> wxWidgets works so people that comes from C++ world can use the extension
> with the same knowledge as that of original implementation. wxPHP is almost
> a 1 on 1 binding except for wxString which is a native php string and
> wxDateTime which is just a timestamp on wxphp.
Still PHP is a different language with other paradigms. See fread(): PHP
for asic functions is a wrapper over C, by reading a C manual you can
grasps many parts of PHP but still fread() uses a slightly different
way.
> It is strange that the first call works and assings the buffer reference
> properly but second call fails.
I haven't fully checked, but the suspicious part for me is
ZVAL_STRING(arguments[0], (char*) buffer, 0);
I assume that on
$buffer = fread($this->image, $bufsize);
$buffer tries to free its contents in order to be overwritten, but the
buffer passed to wxInputStream_php::OnSysRead may not be freed by PHP. I
assume simply passing a NULL zval to the function would be enough.
Some other unrelated notes on things I saw:
You are calling zend_call_function(). this is no ZEND_API so depending
on compiler flags etc. it is not exported and not callable from the
extensions .so/.dll file.
The usage of static bool is_php_user_space_implemented is suspicious. Is
there never a chance that the method at first does not exist and a user
provides it lateron?
In memcpy ((void*) buffer, (void*) Z_STRVAL_P(arguments[0]),
Z_STRLEN_P(arguments[0])); you assume that the arguments[0] zval will
still be a string, if it is passed y ref this is not guaranteed. The
user might do $buffer = NULL; (or an object with a __toString()). Also
you don't verify that the buffer is large enough (the user might ignore
$bufsize).
In return (size_t) Z_LVAL_P(return_value); you aren't verifying the type
either. If the user by mistake returns a string with the contents this
will misbehave. You also don't check whether the returned length matches
the information from the return value. Thus the consumer of
wxInputStream_php::OnSysRead can't rely on that size.
johannes