]> granicus.if.org Git - postgresql/commitdiff
Fix buffer allocations in encoding conversion routines so that they won't
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 28 Feb 2009 18:50:08 +0000 (18:50 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 28 Feb 2009 18:50:08 +0000 (18:50 +0000)
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.

src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c
src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c
src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c

index 140fb45fde676cb39d5405006845b7bd9c4d84d8..7b59cde943bbbb6ba51588e80c0abff7b3537f0b 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c,v 1.11.2.3 2009/01/29 19:24:36 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c,v 1.11.2.4 2009/02/28 18:50:08 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((char *) 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((char *) buf));
        pfree(buf);
@@ -237,7 +237,7 @@ koi8r_to_win866(PG_FUNCTION_ARGS)
 
        CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_WIN866);
 
-       buf = palloc(len * ENCODING_GROWTH_RATE);
+       buf = palloc(len * ENCODING_GROWTH_RATE + 1);
        koi8r2mic(src, buf, len);
        mic2win866(buf, dest, strlen((char *) buf));
        pfree(buf);
@@ -255,7 +255,7 @@ win866_to_koi8r(PG_FUNCTION_ARGS)
 
        CHECK_ENCODING_CONVERSION_ARGS(PG_WIN866, PG_KOI8R);
 
-       buf = palloc(len * ENCODING_GROWTH_RATE);
+       buf = palloc(len * ENCODING_GROWTH_RATE + 1);
        win8662mic(src, buf, len);
        mic2koi8r(buf, dest, strlen((char *) buf));
        pfree(buf);
@@ -279,7 +279,7 @@ win866_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);
        win8662mic(src, buf, len);
        mic2win1251(buf, dest, strlen((char *) buf));
        pfree(buf);
@@ -298,7 +298,7 @@ win1251_to_win866(PG_FUNCTION_ARGS)
        CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1251, PG_WIN866);
 
        /* Use mic/KOI8R as intermediary, see comment in win866_to_win1251() */
-       buf = palloc(len * ENCODING_GROWTH_RATE);
+       buf = palloc(len * ENCODING_GROWTH_RATE + 1);
        win12512mic(src, buf, len);
        mic2win866(buf, dest, strlen((char *) 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((char *) 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((char *) 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 win866_to_win1251() */
-       buf = palloc(len * ENCODING_GROWTH_RATE);
+       buf = palloc(len * ENCODING_GROWTH_RATE + 1);
        iso2mic(src, buf, len);
        mic2win1251(buf, dest, strlen((char *) 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 win866_to_win1251() */
-       buf = palloc(len * ENCODING_GROWTH_RATE);
+       buf = palloc(len * ENCODING_GROWTH_RATE + 1);
        win12512mic(src, buf, len);
        mic2iso(buf, dest, strlen((char *) buf));
        pfree(buf);
@@ -391,7 +391,7 @@ iso_to_win866(PG_FUNCTION_ARGS)
        CHECK_ENCODING_CONVERSION_ARGS(PG_ISO_8859_5, PG_WIN866);
 
        /* Use mic/KOI8R as intermediary, see comment in win866_to_win1251() */
-       buf = palloc(len * ENCODING_GROWTH_RATE);
+       buf = palloc(len * ENCODING_GROWTH_RATE + 1);
        iso2mic(src, buf, len);
        mic2win866(buf, dest, strlen((char *) buf));
        pfree(buf);
@@ -410,7 +410,7 @@ win866_to_iso(PG_FUNCTION_ARGS)
        CHECK_ENCODING_CONVERSION_ARGS(PG_WIN866, PG_ISO_8859_5);
 
        /* Use mic/KOI8R as intermediary, see comment in win866_to_win1251() */
-       buf = palloc(len * ENCODING_GROWTH_RATE);
+       buf = palloc(len * ENCODING_GROWTH_RATE + 1);
        win8662mic(src, buf, len);
        mic2iso(buf, dest, strlen((char *) buf));
        pfree(buf);
index fab4db86f5f375b7b8e7bf48585362fb1af4b358..dd67f67fac956b64415caf498de68a8fa8a6055e 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c,v 1.13.2.3 2009/01/29 19:24:36 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c,v 1.13.2.4 2009/02/28 18:50:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -27,8 +27,6 @@
  */
 #include "sjis.map"
 
-#define ENCODING_GROWTH_RATE 4
-
 PG_FUNCTION_INFO_V1(euc_jp_to_sjis);
 PG_FUNCTION_INFO_V1(sjis_to_euc_jp);
 PG_FUNCTION_INFO_V1(euc_jp_to_mic);
index 7e4612f5ada49c3c5ee3ad6cafb5291d54004758..d101b69b9a6574097f54a8f1a5b100c462e346d6 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c,v 1.10.2.2 2009/01/29 19:24:36 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c,v 1.10.2.3 2009/02/28 18:50:08 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((char *) 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((char *) buf));
        pfree(buf);
index e5acb94a584885fe5f79dc69faeab9c9e745fe4f..2df1f358458e6bcdc5c231773d8cc9db90380123 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c,v 1.10.2.2 2009/01/29 19:24:36 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c,v 1.10.2.3 2009/02/28 18:50:08 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((char *) 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((char *) buf));
        pfree(buf);