From 43d4ef45a3f0ef8c04f9fb9426be8ee2cf25f68a Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 26 Dec 2013 13:49:55 +0100 Subject: [PATCH] Fixed some issues identified by static code analysis. The following issues were identified and fixed: * Allocator sizeof operand mismatch in dbi utils. * Dead increment in tools/sysdb/input. * Memory leak (and leaving behind invalid memory) in tools/sysdbd/configfile. * Out-of-bound array access in core/plugin (the original report by the clang analyzer was wrong but pointed to a location affected by a related problem). * Stack address stored into global variable in liboconfig. Thanks to cppcheck and the clang analyzer! --- src/core/plugin.c | 2 +- src/liboconfig/oconfig.c | 1 + src/tools/sysdb/input.c | 6 ++---- src/tools/sysdbd/configfile.c | 6 +++--- src/utils/dbi.c | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/core/plugin.c b/src/core/plugin.c index 46976d0..a3aba99 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -386,7 +386,7 @@ plugin_add_callback(sdb_llist_t **list, const char *type, int sdb_plugin_load(const char *name, const sdb_plugin_ctx_t *plugin_ctx) { - char real_name[strlen(name) > 0 ? strlen(name) : 1]; + char real_name[name ? strlen(name) + 1 : 1]; const char *name_ptr; char *tmp; diff --git a/src/liboconfig/oconfig.c b/src/liboconfig/oconfig.c index 629775a..427f957 100644 --- a/src/liboconfig/oconfig.c +++ b/src/liboconfig/oconfig.c @@ -61,6 +61,7 @@ oconfig_item_t *oconfig_parse_fh (FILE *fh) if (status != 0) { fprintf (stderr, "yyparse returned error #%i\n", status); + c_file = NULL; return (NULL); } diff --git a/src/tools/sysdb/input.c b/src/tools/sysdb/input.c index 3366fd9..0513ee0 100644 --- a/src/tools/sysdb/input.c +++ b/src/tools/sysdb/input.c @@ -91,10 +91,9 @@ sdb_input_readline(sdb_input_t *input, char *buf, int *n_chars, size_t max_chars) { const char *query; - size_t buflen, len; + size_t len; - buflen = sdb_strbuf_len(input->buf); - len = buflen - input->tokenizer_pos; + len = sdb_strbuf_len(input->buf) - input->tokenizer_pos; if (! len) { size_t n = input_readline(input->buf); @@ -102,7 +101,6 @@ sdb_input_readline(sdb_input_t *input, char *buf, *n_chars = 0; /* YY_NULL */ return 0; } - buflen += n; len += n; } diff --git a/src/tools/sysdbd/configfile.c b/src/tools/sysdbd/configfile.c index 9527626..9c7308d 100644 --- a/src/tools/sysdbd/configfile.c +++ b/src/tools/sysdbd/configfile.c @@ -129,15 +129,15 @@ daemon_add_listener(oconfig_item_t *ci) return -1; } - tmp[listen_addresses_num] = strdup(address); - if (! tmp[listen_addresses_num]) { + listen_addresses = tmp; + listen_addresses[listen_addresses_num] = strdup(address); + if (! listen_addresses[listen_addresses_num]) { char buf[1024]; sdb_log(SDB_LOG_ERR, "config: Failed to allocate memory: %s", sdb_strerror(errno, buf, sizeof(buf))); return -1; } - listen_addresses = tmp; ++listen_addresses_num; return 0; } /* daemon_add_listener */ diff --git a/src/utils/dbi.c b/src/utils/dbi.c index d9bd369..1de2f1a 100644 --- a/src/utils/dbi.c +++ b/src/utils/dbi.c @@ -172,7 +172,7 @@ sdb_dbi_options_create(void) { sdb_dbi_options_t *options; - options = malloc(sizeof(options)); + options = malloc(sizeof(*options)); if (! options) return NULL; -- 2.30.2