77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.1.1.1 1996/07/09 06:22:06 scrappy Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.2 1996/07/19 06:08:21 scrappy Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -198,6 +198,24 @@ textout(struct varlena *vlena)
198198
199199/* ========== PUBLIC ROUTINES ========== */
200200
201+ /*
202+
203+ /*
204+ * textlen -
205+ * returns the actual length of a text* (which may be less than
206+ * the VARSIZE of the text*)
207+ */
208+
209+ int textlen (text * t )
210+ {
211+ int i = 0 ;
212+ int max = VARSIZE (t )- VARHDRSZ ;
213+ char * ptr = VARDATA (t );
214+ while (i < max && * ptr ++ )
215+ i ++ ;
216+ return i ;
217+ }
218+
201219/*
202220 * textcat -
203221 * takes two text* and returns a text* that is the concatentation of
@@ -206,28 +224,21 @@ textout(struct varlena *vlena)
206224text *
207225textcat (text * t1 ,text * t2 )
208226{
209- int newlen ;
210- char * str1 ,* str2 ;
227+ int len1 ,len2 ,newlen ;
211228text * result ;
212229
213230if (t1 == NULL )return t2 ;
214231if (t2 == NULL )return t1 ;
215232
216- /* since t1, and t2 are non-null, str1 and str2 must also be non-null */
217- str1 = textout (t1 );
218- str2 = textout (t2 );
219- /* we use strlen here to calculate the length because the size fields
220- of t1, t2 may be longer than necessary to hold the string */
221- newlen = strlen (str1 )+ strlen (str2 )+ VARHDRSZ ;
222- result = (text * )palloc (newlen );
223- strcpy (VARDATA (result ),str1 );
224- strncat (VARDATA (result ),str2 ,newlen - VARHDRSZ );
225- /* [TRH] Was:
226- strcat(VARDATA(result), str2);
227- which may corrupt the malloc arena due to writing trailing \0. */
228-
229- pfree (str1 );
230- pfree (str2 );
233+ len1 = textlen (t1 );
234+ len2 = textlen (t2 );
235+ newlen = len1 + len2 + VARHDRSZ ;
236+ result = (text * )palloc (newlen );
237+
238+ VARSIZE (result )= newlen ;
239+ memcpy (VARDATA (result ),VARDATA (t1 ),len1 );
240+ memcpy (VARDATA (result )+ len1 ,VARDATA (t2 ),len2 );
241+
231242return result ;
232243}
233244