From: Florian Forster Date: Mon, 2 Mar 2009 14:27:12 +0000 (+0100) Subject: libcollectdclient: Check if the file handle is valid before writing to it. X-Git-Tag: collectd-4.6.2~11 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=4c295efaa4044d56f6222186557d35aa165ef6db;p=collectd.git libcollectdclient: Check if the file handle is valid before writing to it. And free allocated memory when connecting fails. Only assign anything to the user-provided pointer if `lcc_connect' succeeds. This should make error-handling much more straight-forward. --- diff --git a/src/libcollectdclient/client.c b/src/libcollectdclient/client.c index 93eadb75..684fa39b 100644 --- a/src/libcollectdclient/client.c +++ b/src/libcollectdclient/client.c @@ -344,6 +344,12 @@ static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */ lcc_response_t res; int status; + if (c->fh == NULL) + { + lcc_set_errno (c, EBADF); + return (-1); + } + status = lcc_send (c, command); if (status != 0) return (status); @@ -554,6 +560,7 @@ const char *lcc_version_extra (void) /* {{{ */ int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */ { lcc_connection_t *c; + int status; if (address == NULL) return (-1); @@ -566,8 +573,15 @@ int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */ return (-1); memset (c, 0, sizeof (*c)); + status = lcc_open_socket (c, address); + if (status != 0) + { + lcc_disconnect (c); + return (status); + } + *ret_con = c; - return (lcc_open_socket (c, address)); + return (0); } /* }}} int lcc_connect */ int lcc_disconnect (lcc_connection_t *c) /* {{{ */