Commit fd223c74 authored by Itagaki Takahiro's avatar Itagaki Takahiro

Remove unnecessary string null-termination in pg_convert.

We can directly verify the unterminated input with pg_verify_mbstr_len.
parent d583f10b
...@@ -406,14 +406,14 @@ pg_convert_from(PG_FUNCTION_ARGS) ...@@ -406,14 +406,14 @@ pg_convert_from(PG_FUNCTION_ARGS)
Datum Datum
pg_convert(PG_FUNCTION_ARGS) pg_convert(PG_FUNCTION_ARGS)
{ {
bytea *string = PG_GETARG_BYTEA_P(0); bytea *string = PG_GETARG_BYTEA_PP(0);
char *src_encoding_name = NameStr(*PG_GETARG_NAME(1)); char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
int src_encoding = pg_char_to_encoding(src_encoding_name); int src_encoding = pg_char_to_encoding(src_encoding_name);
char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2)); char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
int dest_encoding = pg_char_to_encoding(dest_encoding_name); int dest_encoding = pg_char_to_encoding(dest_encoding_name);
unsigned char *result; const char *src_str;
char *dest_str;
bytea *retval; bytea *retval;
unsigned char *str;
int len; int len;
if (src_encoding < 0) if (src_encoding < 0)
...@@ -427,26 +427,25 @@ pg_convert(PG_FUNCTION_ARGS) ...@@ -427,26 +427,25 @@ pg_convert(PG_FUNCTION_ARGS)
errmsg("invalid destination encoding name \"%s\"", errmsg("invalid destination encoding name \"%s\"",
dest_encoding_name))); dest_encoding_name)));
/* make sure that source string is valid and null terminated */ /* make sure that source string is valid */
len = VARSIZE(string) - VARHDRSZ; len = VARSIZE_ANY_EXHDR(string);
pg_verify_mbstr(src_encoding, VARDATA(string), len, false); src_str = VARDATA_ANY(string);
str = palloc(len + 1); pg_verify_mbstr_len(src_encoding, src_str, len, false);
memcpy(str, VARDATA(string), len);
*(str + len) = '\0';
result = pg_do_encoding_conversion(str, len, src_encoding, dest_encoding); dest_str = (char *) pg_do_encoding_conversion(
(unsigned char *) src_str, len, src_encoding, dest_encoding);
if (dest_str != src_str)
len = strlen(dest_str);
/* /*
* build bytea data type structure. * build bytea data type structure.
*/ */
len = strlen((char *) result) + VARHDRSZ; retval = (bytea *) palloc(len + VARHDRSZ);
retval = palloc(len); SET_VARSIZE(retval, len + VARHDRSZ);
SET_VARSIZE(retval, len); memcpy(VARDATA(retval), dest_str, len);
memcpy(VARDATA(retval), result, len - VARHDRSZ);
if (result != str) if (dest_str != src_str)
pfree(result); pfree(dest_str);
pfree(str);
/* free memory if allocated by the toaster */ /* free memory if allocated by the toaster */
PG_FREE_IF_COPY(string, 0); PG_FREE_IF_COPY(string, 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment