From 77f48853933e52a8cf03b7969e546c4c561a9282 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 15 Mar 2000 17:24:18 +0000 Subject: Fix busted TRANSLATE() code --- it coredumped due to pfree()'ing the wrong pointer. --- src/backend/utils/adt/oracle_compat.c | 94 ++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 41 deletions(-) (limited to 'src/backend/utils/adt/oracle_compat.c') diff --git a/src/backend/utils/adt/oracle_compat.c b/src/backend/utils/adt/oracle_compat.c index b18cbf4a208..36524798823 100644 --- a/src/backend/utils/adt/oracle_compat.c +++ b/src/backend/utils/adt/oracle_compat.c @@ -1,7 +1,7 @@ /* * Edmund Mergl * - * $Id: oracle_compat.c,v 1.21 2000/03/14 23:06:37 thomas Exp $ + * $Id: oracle_compat.c,v 1.22 2000/03/15 17:24:18 tgl Exp $ * */ @@ -500,64 +500,76 @@ substr(text *string, int4 m, int4 n) * * Purpose: * - * Returns string after replacing all occurences of from with - * the corresponding character in to. TRANSLATE will not remove - * characters. - * Modified to work with strings rather than single character - * for the substitution arguments. - * Modifications from Edwin Ramirez . + * Returns string after replacing all occurrences of characters in from + * with the corresponding character in to. If from is longer than to, + * occurrences of the extra characters in from are deleted. + * Improved by Edwin Ramirez . * ********************************************************************/ text * translate(text *string, text *from, text *to) { - text *ret; - char *ptr_ret, *from_ptr, *to_ptr; - char *source, *target, *temp, rep; + text *result; + char *from_ptr, *to_ptr; + char *source, *target; int m, fromlen, tolen, retlen, i; - if ((string == (text *) NULL) || - ((m = VARSIZE(string) - VARHDRSZ) <= 0)) - return string; + if (string == (text *) NULL || + from == (text *) NULL || + to == (text *) NULL) + return (text *) NULL; - target = (char *) palloc(VARSIZE(string) - VARHDRSZ); - source = VARDATA(string); - temp = target; + if ((m = VARSIZE(string) - VARHDRSZ) <= 0) + return string; fromlen = VARSIZE(from) - VARHDRSZ; from_ptr = VARDATA(from); tolen = VARSIZE(to) - VARHDRSZ; - to_ptr = VARDATA(to); + to_ptr = VARDATA(to); + + result = (text *) palloc(VARSIZE(string)); + + source = VARDATA(string); + target = VARDATA(result); retlen = 0; - while (m--) + + while (m-- > 0) { - rep = *source; - for(i=0;i