changeset 11237:237d65590d2b

[gaim-migrate @ 13388] Fix a potential crash substituting AIM away message strings. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Fri, 12 Aug 2005 01:08:32 +0000
parents fd6d96ef5c6d
children 04fd99debeb9
files src/util.c
diffstat 1 files changed, 11 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/util.c	Thu Aug 11 20:45:36 2005 +0000
+++ b/src/util.c	Fri Aug 12 01:08:32 2005 +0000
@@ -2301,8 +2301,7 @@
 gaim_str_sub_away_formatters(const char *str, const char *name)
 {
 	char *c;
-	gchar *cpy;
-	int cnt = 0;
+	GString *cpy;
 	time_t t;
 	struct tm *tme;
 	char tmp[20];
@@ -2310,12 +2309,12 @@
 	g_return_val_if_fail(str  != NULL, NULL);
 	g_return_val_if_fail(name != NULL, NULL);
 
-	cpy = g_malloc(BUF_LONG);
+	/* Create an empty GString that is hopefully big enough for most messages */
+	cpy = g_string_sized_new(1024);
 
 	t = time(NULL);
 	tme = localtime(&t);
 
-	cpy[0] = '\0';
 	c = (char *)str;
 	while (*c) {
 		switch (*c) {
@@ -2324,39 +2323,35 @@
 				switch (*(c + 1)) {
 				case 'n':
 					/* append name */
-					strcpy(cpy + cnt, name);
-					cnt += strlen(name);
+					g_string_append(cpy, name);
 					c++;
 					break;
 				case 'd':
 					/* append date */
 					strftime(tmp, 20, "%m/%d/%Y", tme);
-					strcpy(cpy + cnt, tmp);
-					cnt += strlen(tmp);
+					g_string_append(cpy, tmp);
 					c++;
 					break;
 				case 't':
 					/* append time */
 					strftime(tmp, 20, "%I:%M:%S %p", tme);
-					strcpy(cpy + cnt, tmp);
-					cnt += strlen(tmp);
+					g_string_append(cpy, tmp);
 					c++;
 					break;
 				default:
-					cpy[cnt++] = *c;
+					g_string_append_c(cpy, *c);
 				}
 			} else {
-				cpy[cnt++] = *c;
+				g_string_append_c(cpy, *c);
 			}
 			break;
 		default:
-			cpy[cnt++] = *c;
+			g_string_append_c(cpy, *c);
 		}
 		c++;
 	}
-	cpy[cnt] = '\0';
-
-	return cpy;
+
+	return g_string_free(cpy, FALSE);
 }
 
 gchar *