Re: FFI extension / NULL-Checks
| From: | Philip Hofstetter | Date: | Mon, 09 Sep 2019 11:33:14 +0000 |
| Subject: | Re: FFI extension / NULL-Checks | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-106917@lists.php.net to get a copy of this message | ||
In response to these findings so far, I have now submitted
https://github.com/php/php-src/pull/4691
that adds an FFI::isNull() method that returns true if the passed CData
instance points to NULL.
If you agree that something like this is indeed needed, I will gladly
update the PR and add unit tests.
Philip
On Mon, Sep 9, 2019 at 8:03 AM Philip Hofstetter <phofstetter@sensational.ch>
wrote:
> Hello,
>
> (I'm writing to the internals list because I don't think that at this
> point, FFI usage is wide-spread enough to get an opinion on other venues)
>
> maybe I'm just stupid, but I think there has been a slight oversight in
> the API design for the FFI interface.
>
> My problem is with functions that return a pointer to a struct as an out
> parameter.
>
> So in C, it would look like this:
>
> Error* err;
> func(&err);
>
> if (err != NULL){
> do_error_handling();
> }
>
> If I translate this to PHP FFI, I would do
>
> $err = $ffi->new("Error*");
> $ffi->func(FFI::addr($err));
>
> if I
var_dump $err, I do see a public {0} property be set to
> NULL or to
> actual error data.
>
> My issue is though: How do I check whether err* as been assgined to?
>
> isset($err)
> $err[0] != null
> -> is always true, regardless of whether func as assigned an error or not
>
> isset($err->{0})
> isset($err[0])
> -> Cannot use object of type FFI\CData as array
>
> $err->{0} != NULL;
> -> FFI\Exception: NULL pointer dereference
>
> $err[0];
> -> segfault
>
> I'm sure I must be doing something wrong, but I don't know what. Also, the
> documentation could do with an example on how to achieve the desired result
> and, TBH, I think some of the various attempts produce a somewhat
> unexpected output.
>
> Any comments?
>
> Philip
>
>
> var_dump(isset($r->{0}));
> -> Error: Cannot use object of type FFI\CData as array
>
> $a = '{0}';
> var_dump(isset($r->$a));
> -> Error: Cannot use object of type FFI\CData as array
>
>
> var_dump(isset($r[0]));
> var_dump($r[0]);
>
>
>
--
Sensational AG
Giesshübelstrasse 62c, Postfach 1966, 8021 Zürich
Tel. +41 43 544 09 60, Mobile +41 79 341 01 99
info@sensational.ch, http://www.sensational.ch