round trip for undef / NULL #16

Closed
plicease opened this Issue Dec 18, 2013 · 3 comments

2 participants

@plicease

I just noticed what I think is an inconsistency between FFI::Raw::str values as passed in from Perl to C as apposed to returned as values from C to Perl.

If I pass in undef to a C function that takes an FFI::Raw::str value, it comes in to C as "" (zero length string)

If I return NULL from C back to Perl I get undef.

I think that this should at least consistent. My preference would be for undef get translated to NULL when passed into C, and keep the return of NULL to undef as it is. This has always seemed the most natural mapping to me, and it means that undef can get passed into C and come back without loosing any information.

I think in XS it does the opposite by default, undef is translated into "" and NULL becomes "". This is arguably safer since not all C functions handle NULLs passed into string parameters, but it does make it frustrating when writing bindings for libraries that DO handle NULLs gracefully.

I created a branch with a test (07-null.t) that demonstrates the issue here:

https://github.com/plicease/p5-FFI-Raw/commits/str_null_undef

@ghedo ghedo closed this in 608cfaa Dec 19, 2013
@ghedo
Owner

I fixed this in master, thanks for the test!

@plicease

Thanks for the speedy response, a bunch of my tests started passing :)

Any estimate on when this might make it to CPAN?

@ghedo
Owner

I wanted to do the same thing for FFI::Raw::ptr, which is now done. So now I'm just waiting for the CI to finish. I think I'll publish a new release today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment