From bcc29cc2c4ef766cd679c66317a01fa5daff3ecd Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Mon, 16 Apr 2007 08:42:33 +0000 Subject: [PATCH] simplify add_connsction string handling, use StringInfo --- src/cluster.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 45f962a..33ec9b9 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -158,38 +158,32 @@ free_connlist(ProxyCluster *cluster) static ProxyConnection * add_connection(ProxyCluster *cluster, char *connstr) { - int i, - len; + int i; ProxyConnection *conn; - MemoryContext old_ctx; - char *username, - *newstr; + char *username; + StringInfo final; + + final = makeStringInfo(); + appendStringInfoString(final, connstr); /* append current user if not specified in connstr */ if (strstr(connstr, "user=") == NULL) { username = GetUserNameFromId(GetSessionUserId()); - len = strlen(connstr) + strlen(username) + 6 + 1; - newstr = palloc(len); - strcpy(newstr, connstr); - strcat(newstr, " user="); - strcat(newstr, username); - connstr = newstr; + appendStringInfo(final, " user=%s", username); } /* check if already have it */ for (i = 0; i < cluster->conn_count; i++) { conn = &cluster->conn_list[i]; - if (strcmp(conn->connstr, connstr) == 0) + if (strcmp(conn->connstr, final->data) == 0) return conn; } /* add new connection */ - old_ctx = MemoryContextSwitchTo(cluster_mem); conn = &cluster->conn_list[cluster->conn_count++]; - conn->connstr = pstrdup(connstr); - MemoryContextSwitchTo(old_ctx); + conn->connstr = MemoryContextStrdup(cluster_mem, final->data); return conn; } -- 2.39.5