From: Ruben Kerkhof Date: Mon, 28 Mar 2016 17:56:13 +0000 (+0200) Subject: daemon: malloc + memset -> calloc X-Git-Tag: collectd-5.6.0~371^2~45 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=91103db5378a036c0e4da9d512f686d9d0096ff7;p=collectd.git daemon: malloc + memset -> calloc --- diff --git a/src/daemon/common.c b/src/daemon/common.c index 2f6da957..7b7353d9 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -115,10 +115,9 @@ char *ssnprintf_alloc (char const *format, ...) /* {{{ */ return (strdup (static_buffer)); /* Allocate a buffer large enough to hold the string. */ - alloc_buffer = malloc (alloc_buffer_size); + alloc_buffer = calloc (1, alloc_buffer_size); if (alloc_buffer == NULL) return (NULL); - memset (alloc_buffer, 0, alloc_buffer_size); /* Print again into this new buffer. */ va_start (ap, format); @@ -409,10 +408,9 @@ int escape_string (char *buffer, size_t buffer_size) if (buffer_size < 3) return (EINVAL); - temp = malloc (buffer_size); + temp = calloc (1, buffer_size); if (temp == NULL) return (ENOMEM); - memset (temp, 0, buffer_size); temp[0] = '"'; j = 1; diff --git a/src/daemon/configfile.c b/src/daemon/configfile.c index 5f49a9e6..78d4eea8 100644 --- a/src/daemon/configfile.c +++ b/src/daemon/configfile.c @@ -717,14 +717,13 @@ static oconfig_item_t *cf_read_dir (const char *dir, return (NULL); } - root = malloc (sizeof (*root)); + root = calloc (1, sizeof (*root)); if (root == NULL) { - ERROR ("configfile: malloc failed."); + ERROR ("configfile: calloc failed."); closedir (dh); return (NULL); } - memset (root, 0, sizeof (oconfig_item_t)); while ((de = readdir (dh)) != NULL) { @@ -835,13 +834,12 @@ static oconfig_item_t *cf_read_generic (const char *path, return (NULL); } - root = malloc (sizeof (*root)); + root = calloc (1, sizeof (*root)); if (root == NULL) { - ERROR ("configfile: malloc failed."); + ERROR ("configfile: calloc failed."); return (NULL); } - memset (root, '\0', sizeof (oconfig_item_t)); /* wordexp() might return a sorted list already. That's not * documented though, so let's make sure we get what we want. */ diff --git a/src/daemon/filter_chain.c b/src/daemon/filter_chain.c index b599d70e..babf3996 100644 --- a/src/daemon/filter_chain.c +++ b/src/daemon/filter_chain.c @@ -235,13 +235,12 @@ static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */ return (-1); } - m = malloc (sizeof (*m)); + m = calloc (1, sizeof (*m)); if (m == NULL) { - ERROR ("fc_config_add_match: malloc failed."); + ERROR ("fc_config_add_match: calloc failed."); return (-1); } - memset (m, 0, sizeof (*m)); sstrncpy (m->name, ptr->name, sizeof (m->name)); memcpy (&m->proc, &ptr->proc, sizeof (m->proc)); @@ -307,13 +306,12 @@ static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */ return (-1); } - t = malloc (sizeof (*t)); + t = calloc (1, sizeof (*t)); if (t == NULL) { - ERROR ("fc_config_add_target: malloc failed."); + ERROR ("fc_config_add_target: calloc failed."); return (-1); } - memset (t, 0, sizeof (*t)); sstrncpy (t->name, ptr->name, sizeof (t->name)); memcpy (&t->proc, &ptr->proc, sizeof (t->proc)); @@ -373,14 +371,12 @@ static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */ return (-1); } - rule = malloc (sizeof (*rule)); + rule = calloc (1, sizeof (*rule)); if (rule == NULL) { - ERROR ("fc_config_add_rule: malloc failed."); + ERROR ("fc_config_add_rule: calloc failed."); return (-1); } - memset (rule, 0, sizeof (*rule)); - rule->next = NULL; if (ci->values_num == 1) { @@ -469,17 +465,13 @@ static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */ if (chain == NULL) { - chain = malloc (sizeof (*chain)); + chain = calloc (1, sizeof (*chain)); if (chain == NULL) { - ERROR ("fc_config_add_chain: malloc failed."); + ERROR ("fc_config_add_chain: calloc failed."); return (-1); } - memset (chain, 0, sizeof (*chain)); sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name)); - chain->rules = NULL; - chain->targets = NULL; - chain->next = NULL; } for (i = 0; i < ci->children_num; i++) @@ -824,14 +816,12 @@ int fc_register_match (const char *name, match_proc_t proc) /* {{{ */ DEBUG ("fc_register_match (%s);", name); - m = malloc (sizeof (*m)); + m = calloc (1, sizeof (*m)); if (m == NULL) return (-ENOMEM); - memset (m, 0, sizeof (*m)); sstrncpy (m->name, name, sizeof (m->name)); memcpy (&m->proc, &proc, sizeof (m->proc)); - m->next = NULL; if (match_list_head == NULL) { @@ -858,14 +848,12 @@ int fc_register_target (const char *name, target_proc_t proc) /* {{{ */ DEBUG ("fc_register_target (%s);", name); - t = malloc (sizeof (*t)); + t = calloc (1, sizeof (*t)); if (t == NULL) return (-ENOMEM); - memset (t, 0, sizeof (*t)); sstrncpy (t->name, name, sizeof (t->name)); memcpy (&t->proc, &proc, sizeof (t->proc)); - t->next = NULL; if (target_list_head == NULL) { diff --git a/src/daemon/meta_data.c b/src/daemon/meta_data.c index 10678640..cb8a195c 100644 --- a/src/daemon/meta_data.c +++ b/src/daemon/meta_data.c @@ -84,13 +84,12 @@ static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */ { meta_entry_t *e; - e = malloc (sizeof (*e)); + e = calloc (1, sizeof (*e)); if (e == NULL) { - ERROR ("md_entry_alloc: malloc failed."); + ERROR ("md_entry_alloc: calloc failed."); return (NULL); } - memset (e, 0, sizeof (*e)); e->key = md_strdup (key); if (e->key == NULL) @@ -220,15 +219,13 @@ meta_data_t *meta_data_create (void) /* {{{ */ { meta_data_t *md; - md = malloc (sizeof (*md)); + md = calloc (1, sizeof (*md)); if (md == NULL) { - ERROR ("meta_data_create: malloc failed."); + ERROR ("meta_data_create: calloc failed."); return (NULL); } - memset (md, 0, sizeof (*md)); - md->head = NULL; pthread_mutex_init (&md->lock, /* attr = */ NULL); return (md); diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index 6c8b6b0e..e5939395 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -368,13 +368,12 @@ static int create_register_callback (llist_t **list, /* {{{ */ { callback_func_t *cf; - cf = malloc (sizeof (*cf)); + cf = calloc (1, sizeof (*cf)); if (cf == NULL) { - ERROR ("plugin: create_register_callback: malloc failed."); + ERROR ("plugin: create_register_callback: calloc failed."); return (-1); } - memset (cf, 0, sizeof (*cf)); cf->cf_callback = callback; if (ud == NULL) @@ -1244,14 +1243,13 @@ int plugin_register_read (const char *name, read_func_t *rf; int status; - rf = malloc (sizeof (*rf)); + rf = calloc (1, sizeof (*rf)); if (rf == NULL) { - ERROR ("plugin_register_read: malloc failed."); + ERROR ("plugin_register_read: calloc failed."); return (ENOMEM); } - memset (rf, 0, sizeof (read_func_t)); rf->rf_callback = (void *) callback; rf->rf_udata.data = NULL; rf->rf_udata.free_func = NULL; @@ -1278,14 +1276,13 @@ int plugin_register_complex_read (const char *group, const char *name, read_func_t *rf; int status; - rf = malloc (sizeof (*rf)); + rf = calloc (1,sizeof (*rf)); if (rf == NULL) { - ERROR ("plugin_register_complex_read: malloc failed."); + ERROR ("plugin_register_complex_read: calloc failed."); return (ENOMEM); } - memset (rf, 0, sizeof (read_func_t)); rf->rf_callback = (void *) callback; if (group != NULL) sstrncpy (rf->rf_group, group, sizeof (rf->rf_group)); @@ -2578,13 +2575,12 @@ static int plugin_notification_meta_add (notification_t *n, return (-1); } - meta = malloc (sizeof (*meta)); + meta = calloc (1, sizeof (*meta)); if (meta == NULL) { - ERROR ("plugin_notification_meta_add: malloc failed."); + ERROR ("plugin_notification_meta_add: calloc failed."); return (-1); } - memset (meta, 0, sizeof (notification_meta_t)); sstrncpy (meta->name, name, sizeof (meta->name)); meta->type = type; diff --git a/src/daemon/types_list.c b/src/daemon/types_list.c index 3ac38f23..de6fce3e 100644 --- a/src/daemon/types_list.c +++ b/src/daemon/types_list.c @@ -112,12 +112,10 @@ static void parse_line (char *buf) if (fields[0][0] == '#') return; - ds = malloc (sizeof (*ds)); + ds = calloc (1, sizeof (*ds)); if (ds == NULL) return; - memset (ds, '\0', sizeof (data_set_t)); - sstrncpy (ds->type, fields[0], sizeof (ds->type)); ds->ds_num = fields_num - 1; diff --git a/src/daemon/utils_avltree.c b/src/daemon/utils_avltree.c index bf6880ca..1680c41f 100644 --- a/src/daemon/utils_avltree.c +++ b/src/daemon/utils_avltree.c @@ -665,10 +665,9 @@ c_avl_iterator_t *c_avl_get_iterator (c_avl_tree_t *t) if (t == NULL) return (NULL); - iter = malloc (sizeof (*iter)); + iter = calloc (1, sizeof (*iter)); if (iter == NULL) return (NULL); - memset (iter, '\0', sizeof (c_avl_iterator_t)); iter->tree = t; return (iter); diff --git a/src/daemon/utils_cache.c b/src/daemon/utils_cache.c index 7f0d228f..e1cfac98 100644 --- a/src/daemon/utils_cache.c +++ b/src/daemon/utils_cache.c @@ -83,13 +83,12 @@ static cache_entry_t *cache_alloc (size_t values_num) { cache_entry_t *ce; - ce = malloc (sizeof (*ce)); + ce = calloc (1, sizeof (*ce)); if (ce == NULL) { - ERROR ("utils_cache: cache_alloc: malloc failed."); + ERROR ("utils_cache: cache_alloc: calloc failed."); return (NULL); } - memset (ce, '\0', sizeof (cache_entry_t)); ce->values_num = values_num; ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge)); diff --git a/src/daemon/utils_heap.c b/src/daemon/utils_heap.c index 1b5dca73..ae502c90 100644 --- a/src/daemon/utils_heap.c +++ b/src/daemon/utils_heap.c @@ -112,14 +112,13 @@ c_heap_t *c_heap_create (int (*compare) (const void *, const void *)) if (compare == NULL) return (NULL); - h = malloc (sizeof (*h)); + h = calloc (1, sizeof (*h)); if (h == NULL) return (NULL); - memset (h, 0, sizeof (*h)); pthread_mutex_init (&h->lock, /* attr = */ NULL); h->compare = compare; - + h->list = NULL; h->list_len = 0; h->list_size = 0; diff --git a/src/daemon/utils_ignorelist.c b/src/daemon/utils_ignorelist.c index 011b3d3d..d867c848 100644 --- a/src/daemon/utils_ignorelist.c +++ b/src/daemon/utils_ignorelist.c @@ -94,13 +94,12 @@ static int ignorelist_append_regex(ignorelist_t *il, const char *re_str) ignorelist_item_t *entry; int status; - re = malloc (sizeof (*re)); + re = calloc (1, sizeof (*re)); if (re == NULL) { - ERROR ("ignorelist_append_regex: malloc failed."); + ERROR ("ignorelist_append_regex: calloc failed."); return (ENOMEM); } - memset (re, 0, sizeof (*re)); status = regcomp (re, re_str, REG_EXTENDED); if (status != 0) @@ -113,15 +112,14 @@ static int ignorelist_append_regex(ignorelist_t *il, const char *re_str) return (status); } - entry = malloc (sizeof (*entry)); + entry = calloc (1, sizeof (*entry)); if (entry == NULL) { - ERROR ("ignorelist_append_regex: malloc failed."); + ERROR ("ignorelist_append_regex: calloc failed."); regfree (re); sfree (re); return (ENOMEM); } - memset (entry, 0, sizeof (*entry)); entry->rmatch = re; ignorelist_append (il, entry); @@ -134,12 +132,11 @@ static int ignorelist_append_string(ignorelist_t *il, const char *entry) ignorelist_item_t *new; /* create new entry */ - if ((new = malloc(sizeof (*new))) == NULL ) + if ((new = calloc(1, sizeof (*new))) == NULL ) { ERROR ("cannot allocate new entry"); return (1); } - memset (new, '\0', sizeof(ignorelist_item_t)); new->smatch = sstrdup(entry); /* append new entry */ @@ -194,10 +191,9 @@ ignorelist_t *ignorelist_create (int invert) { ignorelist_t *il; - il = malloc (sizeof (*il)); + il = calloc (1, sizeof (*il)); if (il == NULL) return NULL; - memset (il, 0, sizeof (*il)); /* * ->ignore == 0 => collect diff --git a/src/daemon/utils_llist.c b/src/daemon/utils_llist.c index 40dd0eaa..1a6188f6 100644 --- a/src/daemon/utils_llist.c +++ b/src/daemon/utils_llist.c @@ -48,12 +48,10 @@ llist_t *llist_create (void) { llist_t *ret; - ret = malloc (sizeof (*ret)); + ret = calloc (1, sizeof (*ret)); if (ret == NULL) return (NULL); - memset (ret, '\0', sizeof (llist_t)); - return (ret); } diff --git a/src/daemon/utils_match.c b/src/daemon/utils_match.c index c1d99e15..cf87b6b7 100644 --- a/src/daemon/utils_match.c +++ b/src/daemon/utils_match.c @@ -238,10 +238,9 @@ cu_match_t *match_create_callback (const char *regex, const char *excluderegex, DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s", regex, excluderegex); - obj = malloc (sizeof (*obj)); + obj = calloc (1, sizeof (*obj)); if (obj == NULL) return (NULL); - memset (obj, '\0', sizeof (cu_match_t)); status = regcomp (&obj->regex, regex, REG_EXTENDED | REG_NEWLINE); if (status != 0) @@ -275,10 +274,9 @@ cu_match_t *match_create_simple (const char *regex, cu_match_value_t *user_data; cu_match_t *obj; - user_data = malloc (sizeof (*user_data)); + user_data = calloc (1, sizeof (*user_data)); if (user_data == NULL) return (NULL); - memset (user_data, '\0', sizeof (cu_match_value_t)); user_data->ds_type = match_ds_type; obj = match_create_callback (regex, excluderegex, diff --git a/src/daemon/utils_tail.c b/src/daemon/utils_tail.c index c4b73c35..0b0a8fb4 100644 --- a/src/daemon/utils_tail.c +++ b/src/daemon/utils_tail.c @@ -119,10 +119,9 @@ cu_tail_t *cu_tail_create (const char *file) { cu_tail_t *obj; - obj = malloc (sizeof (*obj)); + obj = calloc (1, sizeof (*obj)); if (obj == NULL) return (NULL); - memset (obj, '\0', sizeof (cu_tail_t)); obj->file = strdup (file); if (obj->file == NULL) diff --git a/src/daemon/utils_tail_match.c b/src/daemon/utils_tail_match.c index c8fd05ef..1e5da95e 100644 --- a/src/daemon/utils_tail_match.c +++ b/src/daemon/utils_tail_match.c @@ -126,10 +126,9 @@ cu_tail_match_t *tail_match_create (const char *filename) { cu_tail_match_t *obj; - obj = malloc (sizeof (*obj)); + obj = calloc (1, sizeof (*obj)); if (obj == NULL) return (NULL); - memset (obj, '\0', sizeof (cu_tail_match_t)); obj->tail = cu_tail_create (filename); if (obj->tail == NULL) @@ -212,13 +211,12 @@ int tail_match_add_match_simple (cu_tail_match_t *obj, if (match == NULL) return (-1); - user_data = malloc (sizeof (*user_data)); + user_data = calloc (1, sizeof (*user_data)); if (user_data == NULL) { match_destroy (match); return (-1); } - memset (user_data, '\0', sizeof (cu_tail_match_simple_t)); sstrncpy (user_data->plugin, plugin, sizeof (user_data->plugin)); if (plugin_instance != NULL)