Skip to content

Commit fa98a86

Browse files
committed
Tweak the code in a couple of places to try to deliver more user-friendly
error messages when a single COPY line is too long for us to handle. Per example from Johann Spies.
1 parent 7e72d07 commit fa98a86

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/backend/lib/stringinfo.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
12-
* $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.45 2007/03/03 19:32:54 neilc Exp $
12+
* $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.46 2007/05/28 16:43:24 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -234,14 +234,17 @@ enlargeStringInfo(StringInfo str, int needed)
234234
int newlen;
235235

236236
/*
237-
* Guard against ridiculous "needed" values, which can occur if we're fed
238-
* bogus data. Without this, we can get an overflow or infinite loop in
239-
* the following.
237+
* Guard against out-of-range "needed" values. Without this, we can get
238+
* an overflow or infinite loop in the following.
240239
*/
241-
if (needed < 0 ||
242-
((Size) needed) >= (MaxAllocSize - (Size) str->len))
243-
elog(ERROR, "invalid string enlargement request size %d",
244-
needed);
240+
if (needed < 0) /* should not happen */
241+
elog(ERROR, "invalid string enlargement request size: %d", needed);
242+
if (((Size) needed) >= (MaxAllocSize - (Size) str->len))
243+
ereport(ERROR,
244+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
245+
errmsg("out of memory"),
246+
errdetail("Cannot enlarge string buffer containing %d bytes by %d more bytes.",
247+
str->len, needed)));
245248

246249
needed += str->len + 1; /* total space required now */
247250

src/backend/utils/mb/mbutils.c

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* (currently mule internal code (mic) is used)
55
* Tatsuo Ishii
66
*
7-
* $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.62 2007/02/27 23:48:09 tgl Exp $
7+
* $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.63 2007/05/28 16:43:24 tgl Exp $
88
*/
99
#include "postgres.h"
1010

@@ -15,6 +15,17 @@
1515
#include "utils/memutils.h"
1616
#include "utils/syscache.h"
1717

18+
/*
19+
* When converting strings between different encodings, we assume that space
20+
* for converted result is 4-to-1 growth in the worst case. The rate for
21+
* currently supported encoding pairs are within 3 (SJIS JIS X0201 half width
22+
* kanna -> UTF8 is the worst case). So "4" should be enough for the moment.
23+
*
24+
* Note that this is not the same as the maximum character width in any
25+
* particular encoding.
26+
*/
27+
#define MAX_CONVERSION_GROWTH 4
28+
1829
/*
1930
* We handle for actual FE and BE encoding setting encoding-identificator
2031
* and encoding-name too. It prevent searching and conversion from encoding
@@ -207,15 +218,14 @@ pg_get_client_encoding_name(void)
207218
* conversion function is chosen from the pg_conversion system catalog
208219
* marked as "default". If it is not found in the schema search path,
209220
* it's taken from pg_catalog schema. If it even is not in the schema,
210-
* warn and returns src. We cannot raise an error, since it will cause
211-
* an infinit loop in error message sending.
221+
* warn and return src.
212222
*
213223
* In the case of no conversion, src is returned.
214224
*
215-
* XXX We assume that storage for converted result is 4-to-1 growth in
216-
* the worst case. The rate for currently supported encoding pares are within 3
217-
* (SJIS JIS X0201 half width kanna -> UTF8 is the worst case).
218-
* So "4" should be enough for the moment.
225+
* Note: we try to avoid raising error, since that could get us into
226+
* infinite recursion when this function is invoked during error message
227+
* sending. It should be OK to raise error for overlength strings though,
228+
* since the recursion will come with a shorter message.
219229
*/
220230
unsigned char *
221231
pg_do_encoding_conversion(unsigned char *src, int len,
@@ -260,7 +270,17 @@ pg_do_encoding_conversion(unsigned char *src, int len,
260270
return src;
261271
}
262272

263-
result = palloc(len * 4 + 1);
273+
/*
274+
* Allocate space for conversion result, being wary of integer overflow
275+
*/
276+
if ((Size) len >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH))
277+
ereport(ERROR,
278+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
279+
errmsg("out of memory"),
280+
errdetail("String of %d bytes is too long for encoding conversion.",
281+
len)));
282+
283+
result = palloc(len * MAX_CONVERSION_GROWTH + 1);
264284

265285
OidFunctionCall5(proc,
266286
Int32GetDatum(src_encoding),
@@ -458,7 +478,17 @@ perform_default_encoding_conversion(const char *src, int len, bool is_client_to_
458478
if (flinfo == NULL)
459479
return (char *) src;
460480

461-
result = palloc(len * 4 + 1);
481+
/*
482+
* Allocate space for conversion result, being wary of integer overflow
483+
*/
484+
if ((Size) len >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH))
485+
ereport(ERROR,
486+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
487+
errmsg("out of memory"),
488+
errdetail("String of %d bytes is too long for encoding conversion.",
489+
len)));
490+
491+
result = palloc(len * MAX_CONVERSION_GROWTH + 1);
462492

463493
FunctionCall5(flinfo,
464494
Int32GetDatum(src_encoding),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy