mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-01-12 18:34:36 +08:00
3fbfd40b37
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.
101 lines
1.9 KiB
C
101 lines
1.9 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* dict_int.c
|
|
* Text search dictionary for integers
|
|
*
|
|
* Copyright (c) 2007-2010, PostgreSQL Global Development Group
|
|
*
|
|
* IDENTIFICATION
|
|
* $PostgreSQL: pgsql/contrib/dict_int/dict_int.c,v 1.6 2010/01/02 16:57:32 momjian Exp $
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#include "postgres.h"
|
|
|
|
#include "commands/defrem.h"
|
|
#include "fmgr.h"
|
|
#include "tsearch/ts_public.h"
|
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
|
typedef struct
|
|
{
|
|
int maxlen;
|
|
bool rejectlong;
|
|
} DictInt;
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(dintdict_init);
|
|
Datum dintdict_init(PG_FUNCTION_ARGS);
|
|
|
|
PG_FUNCTION_INFO_V1(dintdict_lexize);
|
|
Datum dintdict_lexize(PG_FUNCTION_ARGS);
|
|
|
|
Datum
|
|
dintdict_init(PG_FUNCTION_ARGS)
|
|
{
|
|
List *dictoptions = (List *) PG_GETARG_POINTER(0);
|
|
DictInt *d;
|
|
ListCell *l;
|
|
|
|
d = (DictInt *) palloc0(sizeof(DictInt));
|
|
d->maxlen = 6;
|
|
d->rejectlong = false;
|
|
|
|
foreach(l, dictoptions)
|
|
{
|
|
DefElem *defel = (DefElem *) lfirst(l);
|
|
|
|
if (pg_strcasecmp(defel->defname, "MAXLEN") == 0)
|
|
{
|
|
d->maxlen = atoi(defGetString(defel));
|
|
}
|
|
else if (pg_strcasecmp(defel->defname, "REJECTLONG") == 0)
|
|
{
|
|
d->rejectlong = defGetBoolean(defel);
|
|
}
|
|
else
|
|
{
|
|
ereport(ERROR,
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
errmsg("unrecognized intdict parameter: \"%s\"",
|
|
defel->defname)));
|
|
}
|
|
}
|
|
|
|
PG_RETURN_POINTER(d);
|
|
}
|
|
|
|
Datum
|
|
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 = palloc0(sizeof(TSLexeme) * 2);
|
|
|
|
res[1].lexeme = NULL;
|
|
if (PG_GETARG_INT32(2) > d->maxlen)
|
|
{
|
|
if (d->rejectlong)
|
|
{
|
|
/* reject by returning void array */
|
|
pfree(txt);
|
|
res[0].lexeme = NULL;
|
|
}
|
|
else
|
|
{
|
|
/* trim integer */
|
|
txt[d->maxlen] = '\0';
|
|
res[0].lexeme = txt;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
res[0].lexeme = txt;
|
|
}
|
|
|
|
PG_RETURN_POINTER(res);
|
|
}
|