fixes for textcat(), but headers were missing from archive :(

This commit is contained in:
Marc G. Fournier 1996-07-19 06:08:21 +00:00
parent 89ad633897
commit 64bfa0487b

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.1.1.1 1996/07/09 06:22:06 scrappy Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.2 1996/07/19 06:08:21 scrappy Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -198,6 +198,24 @@ textout(struct varlena *vlena)
/* ========== PUBLIC ROUTINES ========== */ /* ========== PUBLIC ROUTINES ========== */
/*
/*
* textlen -
* returns the actual length of a text* (which may be less than
* the VARSIZE of the text*)
*/
int textlen (text* t)
{
int i = 0;
int max = VARSIZE(t) - VARHDRSZ;
char *ptr = VARDATA(t);
while (i < max && *ptr++)
i++;
return i;
}
/* /*
* textcat - * textcat -
* takes two text* and returns a text* that is the concatentation of * takes two text* and returns a text* that is the concatentation of
@ -206,28 +224,21 @@ textout(struct varlena *vlena)
text* text*
textcat(text* t1, text* t2) textcat(text* t1, text* t2)
{ {
int newlen; int len1, len2, newlen;
char *str1, *str2;
text* result; text* result;
if (t1 == NULL) return t2; if (t1 == NULL) return t2;
if (t2 == NULL) return t1; if (t2 == NULL) return t1;
/* since t1, and t2 are non-null, str1 and str2 must also be non-null */ len1 = textlen (t1);
str1 = textout(t1); len2 = textlen (t2);
str2 = textout(t2); newlen = len1 + len2 + VARHDRSZ;
/* we use strlen here to calculate the length because the size fields result = (text*) palloc (newlen);
of t1, t2 may be longer than necessary to hold the string */
newlen = strlen(str1) + strlen(str2) + VARHDRSZ; VARSIZE(result) = newlen;
result = (text*)palloc(newlen); memcpy (VARDATA(result), VARDATA(t1), len1);
strcpy(VARDATA(result), str1); memcpy (VARDATA(result) + len1, VARDATA(t2), len2);
strncat(VARDATA(result), str2, newlen - VARHDRSZ);
/* [TRH] Was:
strcat(VARDATA(result), str2);
which may corrupt the malloc arena due to writing trailing \0. */
pfree(str1);
pfree(str2);
return result; return result;
} }