"Christopher Kings-Lynne" <[EMAIL PROTECTED]> writes:
> struct varlena *data;
> char    *word           = "john";
> char    *cur_pos        = NULL;
> int     cur_pos_length  = 0;

> data = (struct varlena *) palloc(VARHDRSZ + column_length + 1);
> word_length = strlen(word);
> cur_pos = &word[word_length - 2];

> while(cur_pos > word)
> {
>       cur_pos_length = strlen(cur_pos);
>       /* Line below causes seg fault on SECOND iteration */
>       data->vl_len = cur_pos_length + sizeof(int32);
>       memcpy(VARDATA(data), cur_pos, cur_pos_length);
>       values[0] = PointerGetDatum(data);
>       values[1] = 0;
>       values[2] = oid;

>       ret = SPI_execp(*(plan->splan), values, NULL, 0);
>       if(ret != SPI_OK_INSERT)
>               elog(ERROR, "Full Text Indexing: error executing plan in insert\n");

>       cur_pos--;
> }

Are you sure it's actually segfaulting *at* the store into data->vl_len?
This seems hard to believe, if data is a local variable.  It seems
possible that the storage data is pointing to gets freed during
SPI_execp, but that would just mean you'd be scribbling on memory that
doesn't belong to you --- which might cause a crash later, but surely
not at that line.

It would be worth looking to see which context is active when you do the
palloc() for data, and then watch to see if anything does a
MemoryContextReset on it.  (If you are running with asserts enabled,
an even simpler test is to look and see if data->vl_len gets changed
underneath you.)

Also, I'm still wondering if column_length is guaranteed to be longer
than word_length.

                        regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html

Reply via email to