[Xapian-devel] Re: [Xapian-discuss] Fixing issue with PHP5/Windows

Olly Betts olly at survex.com
Thu Apr 5 09:53:54 BST 2007


On Thu, Apr 05, 2007 at 10:40:56AM +0200, Daniel M?nard wrote:
>        FILE * f=fopen("c:\\terms.txt", "w");
>        for (Xapian::TermIterator i = (&result)->first; i !=
>    (&result)->second; ++i) {
>          char * p = const_cast<char *>((*i).data());
>          fprintf(f,"data: [%s], p:[%s]\n",((*i).data()), p);
>          add_next_index_stringl(return_value, p, (*i).length(), 1);
>        }
>        fclose(f);
> 
> 
> I got the following output :
> 
>    data: [is], p:[]
>    data: [there], p:[]
> 
> So (*i).data() is OK, but p is not... Something to do with the cast?

It's not safe to output the result of .data() using printf, as there may
be no terminating nul.  That probably puts this into the realms of
undefined behaviour, so we perhaps need to take that output with a pinch
of salt...

But having said that, I think I can see what the problem is here.

TermIterator::operator* returns std::string, so p points to the data
from a temporary string object which is destroyed at the end of the line
where the assignment happens.  Depending on the std::string
implementation and/or memory allocation strategy, that might happen to
work, but it's undefined code.

So in that loop in we should have:

    std::string term = *i;
    add_next_index_stringl(return_value, term.data(), term.length(), 1);

Can you replace the code in xapian_wrap.cc with this and check if that
fixes the problem?

Cheers,
    Olly



More information about the Xapian-devel mailing list