@@ -406,14 +406,14 @@ pg_convert_from(PG_FUNCTION_ARGS)
406
406
Datum
407
407
pg_convert (PG_FUNCTION_ARGS )
408
408
{
409
- bytea * string = PG_GETARG_BYTEA_P (0 );
409
+ bytea * string = PG_GETARG_BYTEA_PP (0 );
410
410
char * src_encoding_name = NameStr (* PG_GETARG_NAME (1 ));
411
411
int src_encoding = pg_char_to_encoding (src_encoding_name );
412
412
char * dest_encoding_name = NameStr (* PG_GETARG_NAME (2 ));
413
413
int dest_encoding = pg_char_to_encoding (dest_encoding_name );
414
- unsigned char * result ;
414
+ const char * src_str ;
415
+ char * dest_str ;
415
416
bytea * retval ;
416
- unsigned char * str ;
417
417
int len ;
418
418
419
419
if (src_encoding < 0 )
@@ -427,26 +427,25 @@ pg_convert(PG_FUNCTION_ARGS)
427
427
errmsg ("invalid destination encoding name \"%s\"" ,
428
428
dest_encoding_name )));
429
429
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);
436
434
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 );
438
439
439
440
/*
440
441
* build bytea data type structure.
441
442
*/
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 );
450
449
451
450
/* free memory if allocated by the toaster */
452
451
PG_FREE_IF_COPY (string , 0 );
0 commit comments