1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Fix contrib/hstore to throw an error for keys or values that don't fit in its

data structure, rather than silently truncating them.  Andrew Gierth
This commit is contained in:
Tom Lane
2009-03-15 22:05:17 +00:00
parent 7a52a8f829
commit f3a72bd40b
4 changed files with 46 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/*
* $PostgreSQL: pgsql/contrib/hstore/hstore_io.c,v 1.8 2008/05/12 00:00:42 alvherre Exp $
* $PostgreSQL: pgsql/contrib/hstore/hstore_io.c,v 1.9 2009/03/15 22:05:17 tgl Exp $
*/
#include "postgres.h"
@ -188,7 +188,7 @@ parse_hstore(HSParser * state)
state->pairs = (Pairs *) repalloc(state->pairs, sizeof(Pairs) * state->plen);
}
state->pairs[state->pcur].key = state->word;
state->pairs[state->pcur].keylen = state->cur - state->word;
state->pairs[state->pcur].keylen = hstoreCheckKeyLen(state->cur - state->word);
state->pairs[state->pcur].val = NULL;
state->word = NULL;
st = WEQ;
@ -228,7 +228,7 @@ parse_hstore(HSParser * state)
if (!get_val(state, true, &escaped))
elog(ERROR, "Unexpected end of string");
state->pairs[state->pcur].val = state->word;
state->pairs[state->pcur].vallen = state->cur - state->word;
state->pairs[state->pcur].vallen = hstoreCheckValLen(state->cur - state->word);
state->pairs[state->pcur].isnull = false;
state->pairs[state->pcur].needfree = true;
if (state->cur - state->word == 4 && !escaped)
@ -268,11 +268,9 @@ comparePairs(const void *a, const void *b)
{
if (((Pairs *) a)->keylen == ((Pairs *) b)->keylen)
{
int res = strncmp(
((Pairs *) a)->key,
int res = strncmp(((Pairs *) a)->key,
((Pairs *) b)->key,
((Pairs *) a)->keylen
);
((Pairs *) a)->keylen);
if (res)
return res;
@ -347,6 +345,27 @@ freeHSParse(HSParser * state)
pfree(state->pairs);
}
size_t
hstoreCheckKeyLen(size_t len)
{
if (len > HSTORE_MAX_KEY_LEN)
ereport(ERROR,
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
errmsg("string too long for hstore key")));
return len;
}
size_t
hstoreCheckValLen(size_t len)
{
if (len > HSTORE_MAX_VALUE_LEN)
ereport(ERROR,
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
errmsg("string too long for hstore value")));
return len;
}
PG_FUNCTION_INFO_V1(hstore_in);
Datum hstore_in(PG_FUNCTION_ARGS);
Datum