From 5156266ab65d67b92493663435051a8bf36ae47d Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 28 Feb 2009 18:50:25 +0000 Subject: [PATCH] Fix buffer allocations in encoding conversion routines so that they won't fail on zero-length inputs. This isn't an issue in normal use because the conversion infrastructure skips calling the converters for empty strings. However a problem was created by yesterday's patch to check whether the right conversion function is supplied in CREATE CONVERSION. The most future-proof fix seems to be to make the converters safe for this corner case. --- .../cyrillic_and_mic/cyrillic_and_mic.c | 26 +++++++++---------- .../euc_jp_and_sjis/euc_jp_and_sjis.c | 6 ++--- .../euc_tw_and_big5/euc_tw_and_big5.c | 6 ++--- .../latin2_and_win1250/latin2_and_win1250.c | 6 ++--- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c b/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c index 6d707a6a11e..18b0a48c5a8 100644 --- a/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c +++ b/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c,v 1.6.4.3 2009/01/29 19:25:12 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c,v 1.6.4.4 2009/02/28 18:50:24 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -201,7 +201,7 @@ koi8r_to_win1251(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_WIN1251); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); koi8r2mic(src, buf, len); mic2win1251(buf, dest, strlen(buf)); pfree(buf); @@ -219,7 +219,7 @@ win1251_to_koi8r(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1251, PG_KOI8R); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); win12512mic(src, buf, len); mic2koi8r(buf, dest, strlen(buf)); pfree(buf); @@ -237,7 +237,7 @@ koi8r_to_alt(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_ALT); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); koi8r2mic(src, buf, len); mic2alt(buf, dest, strlen(buf)); pfree(buf); @@ -255,7 +255,7 @@ alt_to_koi8r(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_ALT, PG_KOI8R); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); alt2mic(src, buf, len); mic2koi8r(buf, dest, strlen(buf)); pfree(buf); @@ -279,7 +279,7 @@ alt_to_win1251(PG_FUNCTION_ARGS) * not in KOI8R. As we use MULE_INTERNAL/KOI8R as an intermediary, we * will fail to convert those characters. */ - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); alt2mic(src, buf, len); mic2win1251(buf, dest, strlen(buf)); pfree(buf); @@ -298,7 +298,7 @@ win1251_to_alt(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1251, PG_ALT); /* Use mic/KOI8R as intermediary, see comment in alt_to_win1251() */ - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); win12512mic(src, buf, len); mic2alt(buf, dest, strlen(buf)); pfree(buf); @@ -316,7 +316,7 @@ iso_to_koi8r(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_ISO_8859_5, PG_KOI8R); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); iso2mic(src, buf, len); mic2koi8r(buf, dest, strlen(buf)); pfree(buf); @@ -334,7 +334,7 @@ koi8r_to_iso(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_ISO_8859_5); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); koi8r2mic(src, buf, len); mic2iso(buf, dest, strlen(buf)); pfree(buf); @@ -353,7 +353,7 @@ iso_to_win1251(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_ISO_8859_5, PG_WIN1251); /* Use mic/KOI8R as intermediary, see comment in alt_to_win1251() */ - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); iso2mic(src, buf, len); mic2win1251(buf, dest, strlen(buf)); pfree(buf); @@ -372,7 +372,7 @@ win1251_to_iso(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1251, PG_ISO_8859_5); /* Use mic/KOI8R as intermediary, see comment in alt_to_win1251() */ - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); win12512mic(src, buf, len); mic2iso(buf, dest, strlen(buf)); pfree(buf); @@ -391,7 +391,7 @@ iso_to_alt(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_ISO_8859_5, PG_ALT); /* Use mic/KOI8R as intermediary, see comment in alt_to_win1251() */ - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); iso2mic(src, buf, len); mic2alt(buf, dest, strlen(buf)); pfree(buf); @@ -410,7 +410,7 @@ alt_to_iso(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_ALT, PG_ISO_8859_5); /* Use mic/KOI8R as intermediary, see comment in alt_to_win1251() */ - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); alt2mic(src, buf, len); mic2iso(buf, dest, strlen(buf)); pfree(buf); diff --git a/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c b/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c index d8a8122d1f6..101d19c85e2 100644 --- a/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c +++ b/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c,v 1.6.4.4 2009/01/29 19:25:13 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c,v 1.6.4.5 2009/02/28 18:50:24 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -69,7 +69,7 @@ euc_jp_to_sjis(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JP, PG_SJIS); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); euc_jp2mic(src, buf, len); mic2sjis(buf, dest, strlen(buf)); pfree(buf); @@ -87,7 +87,7 @@ sjis_to_euc_jp(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_SJIS, PG_EUC_JP); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); sjis2mic(src, buf, len); mic2euc_jp(buf, dest, strlen(buf)); pfree(buf); diff --git a/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c b/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c index 3aca09f3397..18fe6930ea6 100644 --- a/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c +++ b/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c,v 1.6.4.2 2009/01/29 19:25:13 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c,v 1.6.4.3 2009/02/28 18:50:24 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -57,7 +57,7 @@ euc_tw_to_big5(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_TW, PG_BIG5); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); euc_tw2mic(src, buf, len); mic2big5(buf, dest, strlen(buf)); pfree(buf); @@ -75,7 +75,7 @@ big5_to_euc_tw(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_BIG5, PG_EUC_TW); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); big52mic(src, buf, len); mic2euc_tw(buf, dest, strlen(buf)); pfree(buf); diff --git a/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c b/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c index 8d573f6dfcd..1775230e88f 100644 --- a/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c +++ b/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c,v 1.6.4.2 2009/01/29 19:25:13 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c,v 1.6.4.3 2009/02/28 18:50:25 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -113,7 +113,7 @@ latin2_to_win1250(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN2, PG_WIN1250); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); latin22mic(src, buf, len); mic2win1250(buf, dest, strlen(buf)); pfree(buf); @@ -131,7 +131,7 @@ win1250_to_latin2(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1250, PG_LATIN2); - buf = palloc(len * ENCODING_GROWTH_RATE); + buf = palloc(len * ENCODING_GROWTH_RATE + 1); win12502mic(src, buf, len); mic2latin2(buf, dest, strlen(buf)); pfree(buf);