7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -198,6 +198,24 @@ textout(struct varlena *vlena)
198
198
199
199
/* ========== PUBLIC ROUTINES ========== */
200
200
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
+
201
219
/*
202
220
* textcat -
203
221
* takes two text* and returns a text* that is the concatentation of
@@ -206,28 +224,21 @@ textout(struct varlena *vlena)
206
224
text *
207
225
textcat (text * t1 , text * t2 )
208
226
{
209
- int newlen ;
210
- char * str1 , * str2 ;
227
+ int len1 , len2 , newlen ;
211
228
text * result ;
212
229
213
230
if (t1 == NULL ) return t2 ;
214
231
if (t2 == NULL ) return t1 ;
215
232
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
+
231
242
return result ;
232
243
}
233
244
0 commit comments