From: oetiker Date: Sat, 9 Oct 2010 09:50:44 +0000 (+0000) Subject: Ensure that response_read() always calls fflush() or fclose(). X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e26d7cdc2170406f06a4b2f5e304e08651984d01;p=rrdtool.git Ensure that response_read() always calls fflush() or fclose(). This fixes defect #278 (unknown commands from Solaris client). -- kevin brintnall git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.4/program@2135 a5681a0c-68f1-0310-ab6d-d61299d08faa --- diff --git a/src/rrd_client.c b/src/rrd_client.c index 187afb2..47333df 100644 --- a/src/rrd_client.c +++ b/src/rrd_client.c @@ -228,37 +228,36 @@ static void response_free (rrdc_response_t *res) /* {{{ */ static int response_read (rrdc_response_t **ret_response) /* {{{ */ { - rrdc_response_t *ret; + rrdc_response_t *ret = NULL; + int status = 0; char buffer[4096]; char *buffer_ptr; size_t i; +#define DIE(code) do { status = code; goto err_out; } while(0) + if (sh == NULL) - return (-1); + DIE(-1); ret = (rrdc_response_t *) malloc (sizeof (rrdc_response_t)); if (ret == NULL) - return (-2); + DIE(-2); memset (ret, 0, sizeof (*ret)); ret->lines = NULL; ret->lines_num = 0; buffer_ptr = fgets (buffer, sizeof (buffer), sh); - if (buffer_ptr == NULL) { - close_connection(); - return (-3); - } + if (buffer_ptr == NULL) + DIE(-3); + chomp (buffer); ret->status = strtol (buffer, &ret->message, 0); if (buffer == ret->message) - { - response_free (ret); - close_connection(); - return (-4); - } + DIE(-4); + /* Skip leading whitespace of the status message */ ret->message += strspn (ret->message, " \t"); @@ -266,17 +265,13 @@ static int response_read (rrdc_response_t **ret_response) /* {{{ */ { if (ret->status < 0) rrd_set_error("rrdcached: %s", ret->message); - *ret_response = ret; - return (0); + goto out; } ret->lines = (char **) malloc (sizeof (char *) * ret->status); if (ret->lines == NULL) - { - response_free (ret); - close_connection(); - return (-5); - } + DIE(-5); + memset (ret->lines, 0, sizeof (char *) * ret->status); ret->lines_num = (size_t) ret->status; @@ -284,24 +279,27 @@ static int response_read (rrdc_response_t **ret_response) /* {{{ */ { buffer_ptr = fgets (buffer, sizeof (buffer), sh); if (buffer_ptr == NULL) - { - response_free (ret); - close_connection(); - return (-6); - } + DIE(-6); + chomp (buffer); ret->lines[i] = strdup (buffer); if (ret->lines[i] == NULL) - { - response_free (ret); - close_connection(); - return (-7); - } + DIE(-7); } +out: *ret_response = ret; - return (0); + fflush(sh); + return (status); + +err_out: + response_free(ret); + close_connection(); + return (status); + +#undef DIE + } /* }}} rrdc_response_t *response_read */ static int request (const char *buffer, size_t buffer_size, /* {{{ */