Fix bogus code in contrib/ tsearch dictionary examples.
authorTom Lane
Thu, 3 Nov 2011 23:17:59 +0000 (19:17 -0400)
committerTom Lane
Thu, 3 Nov 2011 23:17:59 +0000 (19:17 -0400)
Both dict_int and dict_xsyn were blithely assuming that whatever memory
palloc gives back will be pre-zeroed.  This would typically work for
just about long enough to run their regression tests, and no longer :-(.

The pre-9.0 code in dict_xsyn was even lamer than that, as it would
happily give back a pointer to the result of palloc(0), encouraging
its caller to access off the end of memory.  Again, this would just
barely fail to fail as long as memory contained nothing but zeroes.

Per a report from Rodrigo Hjort that code based on these examples
didn't work reliably.

contrib/dict_int/dict_int.c
contrib/dict_xsyn/dict_xsyn.c

index 8e1918613f75eb0f6ecb785ce5af4b2027e9ed1c..e478e6809341d7c9c01af093db53f4a31a0480aa 100644 (file)
@@ -73,7 +73,7 @@ dintdict_lexize(PG_FUNCTION_ARGS)
    DictInt    *d = (DictInt *) PG_GETARG_POINTER(0);
    char       *in = (char *) PG_GETARG_POINTER(1);
    char       *txt = pnstrdup(in, PG_GETARG_INT32(2));
-   TSLexeme   *res = palloc(sizeof(TSLexeme) * 2);
+   TSLexeme   *res = palloc0(sizeof(TSLexeme) * 2);
 
    res[1].lexeme = NULL;
    if (PG_GETARG_INT32(2) > d->maxlen)
index dc16d9583e2fa30a408ff7266cd8584736755012..656271fa47c192e739b2fc034a2f280ad469fc93 100644 (file)
@@ -245,6 +245,8 @@ dxsyn_lexize(PG_FUNCTION_ARGS)
            if (pos != value || d->keeporig)
            {
                res[nsyns].lexeme = pnstrdup(syn, end - syn);
+               res[nsyns].nvariant = 0;
+               res[nsyns].flags = 0;
                nsyns++;
            }