[KinoSearch] Bug in binding of fetch_sim

Marvin Humphrey marvin at rectangular.com
Mon May 11 18:25:16 PDT 2009


On Mon, May 11, 2009 at 02:41:01PM -0700, webmasters at ctosonline.org wrote:

> So the argument is not being passed properly.

The problem is in this conditional from KinoSearch's autogenerated XS
bindings:

    if ( items >= 1 && SvOK(ST(1)) ) { 

ST(1) is $_[1].  SvOK is supposed to be checking whether the value is defined.
In other words, that conditional ought to translate to Perl like so:

    if ( @_ >= 2 and defined $_[1] ) {

The problem is that "SvOK" is not exactly equivalent to "defined".  From
perlapi: 

    Returns a U32 value indicating whether the value is an SV. It also tells
    whether the value is defined or not.

It seems that the value you are supplying is "not an SV", so SvOK returns
false.  However, if I short-circuit that conditional (and another in
XSBind_sv_to_kobj_or_zcb), the argument gets handled properly.

So what we need is the XS equivalent of "defined $foo".  Unfortunately,
perlapi doesn't expose such a function, so we'll need to hack.

Looking in the Perl source code (pp.c for 5.8.3, pp_hot.c for 5.10.0), I find
the function pp_defined.  Though the implementations are slightly different,
they have this in common near the top:

    if (!sv || !SvANY(sv))
        RETPUSHNO;

SvANY isn't public, but it's easy to find in sv.h:

    #define SvANY(sv)   (sv)->sv_any

It seems that for undefined scalars, the sv_any member is NULL.  So, we'll
just duplicate that logic from pp_defined any time we need a defined-ness
check.

[... time passes ...]

Grr.  That won't work.

<http://coding.derkeiler.com/Archive/Perl/comp.lang.perl.misc/2004-12/0654.html>

Seems like I'm not the only one who's had to rediscover the fact that SvOK()
and defined() aren't the same thing.  :(

Repository revision r4577 adds a function XSBind_sv_defined which includes a
mg_get() call before checking SvOK.  Hopefully that fixes the problem.

Marvin Humphrey




More information about the kinosearch mailing list