From 282349ce7988fbbab7209fc67f4faceac93d2ebb Mon Sep 17 00:00:00 2001 From: oetiker Date: Sat, 9 Oct 2010 09:50:44 +0000 Subject: [PATCH] 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/trunk@2135 a5681a0c-68f1-0310-ab6d-d61299d08faa --- program/src/rrd_client.c | 58 +++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/program/src/rrd_client.c b/program/src/rrd_client.c index 043a144a..21139f0b 100644 --- a/program/src/rrd_client.c +++ b/program/src/rrd_client.c @@ -369,37 +369,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"); @@ -407,17 +406,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; @@ -425,24 +420,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, /* {{{ */ -- 2.30.2