]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix bogus code in contrib/ tsearch dictionary examples.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 3 Nov 2011 23:18:10 +0000 (19:18 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 3 Nov 2011 23:18:10 +0000 (19:18 -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 163a5029a0b1d4dc235ca9b867c0ff1faf3bbb9f..d0941c66ad928113e0e13e0c2e8ec58544320196 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 68a631c17a7fc7e8f5d78b12edd2bd1da0566d62..892983c48b1719c063081b826e830da2322972bf 100644 (file)
@@ -200,7 +200,7 @@ dxsyn_lexize(PG_FUNCTION_ARGS)
                int                     nsyns = 0;
                bool            is_first = true;
 
-               res = palloc(0);
+               res = palloc(sizeof(TSLexeme));
 
                while (pos < value + value_length)
                {
@@ -212,13 +212,13 @@ dxsyn_lexize(PG_FUNCTION_ARGS)
                        *end = '\0';
 
                        res = repalloc(res, sizeof(TSLexeme) * (nsyns + 2));
-                       res[nsyns].lexeme = NULL;
 
                        /* first word is added to result only if KEEPORIG flag is set */
                        if (d->keeporig || !is_first)
                        {
                                res[nsyns].lexeme = pstrdup(syn);
-                               res[nsyns + 1].lexeme = NULL;
+                               res[nsyns].nvariant = 0;
+                               res[nsyns].flags = 0;
 
                                nsyns++;
                        }
@@ -228,6 +228,8 @@ dxsyn_lexize(PG_FUNCTION_ARGS)
                        pos = end + 1;
                }
 
+               res[nsyns].lexeme = NULL;
+
                pfree(value);
        }