Skip to content

Commit

Permalink
use realloc for config_str
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyen committed May 19, 2009
1 parent fbc73db commit f6c168f
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions agent_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,17 @@ void cproxy_on_new_config(void *data0, void *data1) {

if (servers != NULL &&
pool_port > 0) {
// Create a config string that libmemcached likes,
// first by counting up buffer size needed.
// Create a config string that libmemcached likes.
// See memcached_servers_parse().
//
int n = 0;
int s = 0; // Number of servers in this pool.
int config_len = 200;
char *config_str = calloc(config_len, 1);

while (servers[s]) {
n = n + strlen(servers[s]) + 20; // Extra for weight.
// Number of servers in this pool.
//
int s = 0;
while (servers[s])
s++;
}

char *config_str = calloc(n, 1);

// Array of behaviors, one entry for each server.
//
Expand All @@ -313,8 +311,6 @@ void cproxy_on_new_config(void *data0, void *data1) {
if (config_str != NULL &&
behaviors != NULL) {
for (int j = 0; servers[j]; j++) {
assert(j < s);

char svr_key[800];

snprintf(svr_key, sizeof(svr_key),
Expand All @@ -330,14 +326,22 @@ void cproxy_on_new_config(void *data0, void *data1) {
&behaviors[j]);
}

int x = 40 + // For port and weight.
strlen(config_str) +
strlen(behaviors[j].host);
if (config_len < x) {
config_len = 2 * (config_len + x);
config_str = realloc(config_str, config_len);
}

char *config_end = config_str + strlen(config_str);
if (config_end != config_str)
*config_end++ = ',';

if (strlen(behaviors[j].host) > 0 &&
behaviors[j].port > 0) {
snprintf(config_end,
n - (config_end - config_str),
config_len - (config_end - config_str),
"%s:%u",
behaviors[j].host,
behaviors[j].port);
Expand All @@ -353,7 +357,7 @@ void cproxy_on_new_config(void *data0, void *data1) {
if (behaviors[j].downstream_weight > 0) {
config_end = config_str + strlen(config_str);
snprintf(config_end,
n - (config_end - config_str),
config_len - (config_end - config_str),
":%u",
behaviors[j].downstream_weight);
}
Expand Down

0 comments on commit f6c168f

Please sign in to comment.