Skip to content

Commit fd223c7

Browse files
committed
Remove unnecessary string null-termination in pg_convert.
We can directly verify the unterminated input with pg_verify_mbstr_len.
1 parent d583f10 commit fd223c7

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/backend/utils/mb/mbutils.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,14 @@ pg_convert_from(PG_FUNCTION_ARGS)
406406
Datum
407407
pg_convert(PG_FUNCTION_ARGS)
408408
{
409-
bytea *string = PG_GETARG_BYTEA_P(0);
409+
bytea *string = PG_GETARG_BYTEA_PP(0);
410410
char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
411411
int src_encoding = pg_char_to_encoding(src_encoding_name);
412412
char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
413413
int dest_encoding = pg_char_to_encoding(dest_encoding_name);
414-
unsigned char *result;
414+
const char *src_str;
415+
char *dest_str;
415416
bytea *retval;
416-
unsigned char *str;
417417
int len;
418418

419419
if (src_encoding < 0)
@@ -427,26 +427,25 @@ pg_convert(PG_FUNCTION_ARGS)
427427
errmsg("invalid destination encoding name \"%s\"",
428428
dest_encoding_name)));
429429

430-
/* make sure that source string is valid and null terminated */
431-
len = VARSIZE(string) - VARHDRSZ;
432-
pg_verify_mbstr(src_encoding, VARDATA(string), len, false);
433-
str = palloc(len + 1);
434-
memcpy(str, VARDATA(string), len);
435-
*(str + len) = '\0';
430+
/* make sure that source string is valid */
431+
len = VARSIZE_ANY_EXHDR(string);
432+
src_str = VARDATA_ANY(string);
433+
pg_verify_mbstr_len(src_encoding, src_str, len, false);
436434

437-
result = pg_do_encoding_conversion(str, len, src_encoding, dest_encoding);
435+
dest_str = (char *) pg_do_encoding_conversion(
436+
(unsigned char *) src_str, len, src_encoding, dest_encoding);
437+
if (dest_str != src_str)
438+
len = strlen(dest_str);
438439

439440
/*
440441
* build bytea data type structure.
441442
*/
442-
len = strlen((char *) result) + VARHDRSZ;
443-
retval = palloc(len);
444-
SET_VARSIZE(retval, len);
445-
memcpy(VARDATA(retval), result, len - VARHDRSZ);
446-
447-
if (result != str)
448-
pfree(result);
449-
pfree(str);
443+
retval = (bytea *) palloc(len + VARHDRSZ);
444+
SET_VARSIZE(retval, len + VARHDRSZ);
445+
memcpy(VARDATA(retval), dest_str, len);
446+
447+
if (dest_str != src_str)
448+
pfree(dest_str);
450449

451450
/* free memory if allocated by the toaster */
452451
PG_FREE_IF_COPY(string, 0);

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