From: Ruben Kerkhof Date: Sat, 2 Apr 2016 10:33:11 +0000 (+0200) Subject: Merge pull request #1646 from rubenk/cleanup-malloc-calls X-Git-Tag: collectd-5.6.0~371 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=f14bb45d570e0b34bdd3a2a0cadfe414c3dfda40;hp=6894d846ac42cbe88073db074d75279f5a6e31b8;p=collectd.git Merge pull request #1646 from rubenk/cleanup-malloc-calls Cleanup malloc calls --- diff --git a/src/aggregation.c b/src/aggregation.c index 56e4955c..3d5f396a 100644 --- a/src/aggregation.c +++ b/src/aggregation.c @@ -240,13 +240,12 @@ static agg_instance_t *agg_instance_create (data_set_t const *ds, /* {{{ */ DEBUG ("aggregation plugin: Creating new instance."); - inst = malloc (sizeof (*inst)); + inst = calloc (1, sizeof (*inst)); if (inst == NULL) { - ERROR ("aggregation plugin: malloc() failed."); + ERROR ("aggregation plugin: calloc() failed."); return (NULL); } - memset (inst, 0, sizeof (*inst)); pthread_mutex_init (&inst->lock, /* attr = */ NULL); inst->ds_type = ds->ds[0].type; @@ -259,14 +258,13 @@ static agg_instance_t *agg_instance_create (data_set_t const *ds, /* {{{ */ #define INIT_STATE(field) do { \ inst->state_ ## field = NULL; \ if (agg->calc_ ## field) { \ - inst->state_ ## field = malloc (sizeof (*inst->state_ ## field)); \ + inst->state_ ## field = calloc (1, sizeof (*inst->state_ ## field)); \ if (inst->state_ ## field == NULL) { \ agg_instance_destroy (inst); \ free (inst); \ - ERROR ("aggregation plugin: malloc() failed."); \ + ERROR ("aggregation plugin: calloc() failed."); \ return (NULL); \ } \ - memset (inst->state_ ## field, 0, sizeof (*inst->state_ ## field)); \ } \ } while (0) @@ -528,13 +526,12 @@ static int agg_config_aggregation (oconfig_item_t *ci) /* {{{ */ int status; int i; - agg = malloc (sizeof (*agg)); + agg = calloc (1, sizeof (*agg)); if (agg == NULL) { - ERROR ("aggregation plugin: malloc failed."); + ERROR ("aggregation plugin: calloc failed."); return (-1); } - memset (agg, 0, sizeof (*agg)); sstrncpy (agg->ident.host, "/.*/", sizeof (agg->ident.host)); sstrncpy (agg->ident.plugin, "/.*/", sizeof (agg->ident.plugin)); diff --git a/src/amqp.c b/src/amqp.c index 4206bdc1..ec794302 100644 --- a/src/amqp.c +++ b/src/amqp.c @@ -925,15 +925,14 @@ static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */ int status; int i; - conf = malloc (sizeof (*conf)); + conf = calloc (1, sizeof (*conf)); if (conf == NULL) { - ERROR ("amqp plugin: malloc failed."); + ERROR ("amqp plugin: calloc failed."); return (ENOMEM); } /* Initialize "conf" {{{ */ - memset (conf, 0, sizeof (*conf)); conf->publish = publish; conf->name = NULL; conf->format = CAMQP_FORMAT_COMMAND; diff --git a/src/apache.c b/src/apache.c index 38035caa..baa6081a 100644 --- a/src/apache.c +++ b/src/apache.c @@ -175,13 +175,12 @@ static int config_add (oconfig_item_t *ci) int i; int status; - st = malloc (sizeof (*st)); + st = calloc (1, sizeof (*st)); if (st == NULL) { - ERROR ("apache plugin: malloc failed."); + ERROR ("apache plugin: calloc failed."); return (-1); } - memset (st, 0, sizeof (*st)); st->timeout = -1; diff --git a/src/barometer.c b/src/barometer.c index 0e8deaf8..f14ac049 100644 --- a/src/barometer.c +++ b/src/barometer.c @@ -343,9 +343,9 @@ static temperature_list_t * temp_list = NULL; */ static int temp_list_add(temperature_list_t * list, const char * sensor) { - temperature_list_t * new_temp; + temperature_list_t *new_temp; - new_temp = (temperature_list_t *) malloc(sizeof(*new_temp)); + new_temp = malloc(sizeof (*new_temp)); if(new_temp == NULL) return -1; diff --git a/src/ceph.c b/src/ceph.c index b1ca3b66..04a39743 100644 --- a/src/ceph.c +++ b/src/ceph.c @@ -623,7 +623,7 @@ static int ceph_daemon_add_ds_entry(struct ceph_daemon *d, const char *name, return -ENOMEM; } - d->ds_names[d->ds_num] = malloc(sizeof(char) * DATA_MAX_NAME_LEN); + d->ds_names[d->ds_num] = malloc(DATA_MAX_NAME_LEN); if(!d->ds_names[d->ds_num]) { return -ENOMEM; @@ -747,7 +747,7 @@ static int cc_add_daemon_config(oconfig_item_t *ci) } g_daemons = tmp; - nd = malloc(sizeof(*nd)); + nd = malloc(sizeof (*nd)); if(!nd) { return ENOMEM; @@ -845,7 +845,7 @@ node_handler_define_schema(void *arg, const char *val, const char *key) static int add_last(struct ceph_daemon *d, const char *ds_n, double cur_sum, uint64_t cur_count) { - d->last_poll_data[d->last_idx] = malloc(1 * sizeof(struct last_data)); + d->last_poll_data[d->last_idx] = malloc(sizeof (*d->last_poll_data[d->last_idx])); if(!d->last_poll_data[d->last_idx]) { return -ENOMEM; @@ -873,7 +873,7 @@ static int update_last(struct ceph_daemon *d, const char *ds_n, int index, if(!d->last_poll_data) { - d->last_poll_data = malloc(1 * sizeof(struct last_data *)); + d->last_poll_data = malloc(sizeof (*d->last_poll_data)); if(!d->last_poll_data) { return -ENOMEM; diff --git a/src/collectd-nagios.c b/src/collectd-nagios.c index 49e84f35..712c24fa 100644 --- a/src/collectd-nagios.c +++ b/src/collectd-nagios.c @@ -136,14 +136,14 @@ static int filter_ds (size_t *values_num, new_values = (gauge_t *)calloc (match_ds_num_g, sizeof (*new_values)); if (new_values == NULL) { - fprintf (stderr, "malloc failed: %s\n", strerror (errno)); + fprintf (stderr, "calloc failed: %s\n", strerror (errno)); return (RET_UNKNOWN); } new_names = (char **)calloc (match_ds_num_g, sizeof (*new_names)); if (new_names == NULL) { - fprintf (stderr, "malloc failed: %s\n", strerror (errno)); + fprintf (stderr, "calloc failed: %s\n", strerror (errno)); free (new_values); return (RET_UNKNOWN); } diff --git a/src/collectd-tg.c b/src/collectd-tg.c index 53fd3a17..04989dce 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -156,13 +156,12 @@ static lcc_value_list_t *create_value_list (void) /* {{{ */ lcc_value_list_t *vl; int host_num; - vl = malloc (sizeof (*vl)); + vl = calloc (1, sizeof (*vl)); if (vl == NULL) { - fprintf (stderr, "malloc failed.\n"); + fprintf (stderr, "calloc failed.\n"); return (NULL); } - memset (vl, 0, sizeof (*vl)); vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values)); if (vl->values == NULL) diff --git a/src/cpu.c b/src/cpu.c index 7f8c9858..9c432d17 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -814,8 +814,7 @@ static int cpu_read (void) if (pnumcpu != numcpu || perfcpu == NULL) { - if (perfcpu != NULL) - free(perfcpu); + free(perfcpu); perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t)); } pnumcpu = numcpu; diff --git a/src/curl.c b/src/curl.c index 16ae3aba..5f782074 100644 --- a/src/curl.c +++ b/src/curl.c @@ -270,13 +270,12 @@ static int cc_config_add_match (web_page_t *page, /* {{{ */ WARNING ("curl plugin: Ignoring arguments for the `Match' block."); } - match = (web_match_t *) malloc (sizeof (*match)); + match = calloc (1, sizeof (*match)); if (match == NULL) { - ERROR ("curl plugin: malloc failed."); + ERROR ("curl plugin: calloc failed."); return (-1); } - memset (match, 0, sizeof (*match)); status = 0; for (i = 0; i < ci->children_num; i++) @@ -388,7 +387,7 @@ static int cc_page_init_curl (web_page_t *wp) /* {{{ */ if (wp->pass != NULL) credentials_size += strlen (wp->pass); - wp->credentials = (char *) malloc (credentials_size); + wp->credentials = malloc (credentials_size); if (wp->credentials == NULL) { ERROR ("curl plugin: malloc failed."); @@ -436,13 +435,12 @@ static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */ return (-1); } - page = (web_page_t *) malloc (sizeof (*page)); + page = calloc (1, sizeof (*page)); if (page == NULL) { - ERROR ("curl plugin: malloc failed."); + ERROR ("curl plugin: calloc failed."); return (-1); } - memset (page, 0, sizeof (*page)); page->url = NULL; page->user = NULL; page->pass = NULL; diff --git a/src/curl_json.c b/src/curl_json.c index 45898c3e..33b1a6fe 100644 --- a/src/curl_json.c +++ b/src/curl_json.c @@ -474,13 +474,12 @@ static int cj_config_add_key (cj_t *db, /* {{{ */ return (-1); } - key = (cj_key_t *) malloc (sizeof (*key)); + key = calloc (1, sizeof (*key)); if (key == NULL) { - ERROR ("curl_json plugin: malloc failed."); + ERROR ("curl_json plugin: calloc failed."); return (-1); } - memset (key, 0, sizeof (*key)); key->magic = CJ_KEY_MAGIC; if (strcasecmp ("Key", ci->key) == 0) @@ -615,7 +614,7 @@ static int cj_init_curl (cj_t *db) /* {{{ */ if (db->pass != NULL) credentials_size += strlen (db->pass); - db->credentials = (char *) malloc (credentials_size); + db->credentials = malloc (credentials_size); if (db->credentials == NULL) { ERROR ("curl_json plugin: malloc failed."); @@ -667,13 +666,12 @@ static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */ return (-1); } - db = (cj_t *) malloc (sizeof (*db)); + db = calloc (1, sizeof (*db)); if (db == NULL) { - ERROR ("curl_json plugin: malloc failed."); + ERROR ("curl_json plugin: calloc failed."); return (-1); } - memset (db, 0, sizeof (*db)); db->timeout = -1; diff --git a/src/curl_xml.c b/src/curl_xml.c index 39d6fd04..b9f0c24f 100644 --- a/src/curl_xml.c +++ b/src/curl_xml.c @@ -686,7 +686,7 @@ static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */ sfree (xpath->values); xpath->values_len = 0; - xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num); + xpath->values = malloc (sizeof (cx_values_t) * ci->values_num); if (xpath->values == NULL) return (-1); xpath->values_len = (size_t) ci->values_num; @@ -709,13 +709,12 @@ static int cx_config_add_xpath (cx_t *db, oconfig_item_t *ci) /* {{{ */ int status; int i; - xpath = malloc (sizeof (*xpath)); + xpath = calloc (1, sizeof (*xpath)); if (xpath == NULL) { - ERROR ("curl_xml plugin: malloc failed."); + ERROR ("curl_xml plugin: calloc failed."); return (-1); } - memset (xpath, 0, sizeof (*xpath)); status = cf_util_get_string (ci, &xpath->path); if (status != 0) @@ -873,7 +872,7 @@ static int cx_init_curl (cx_t *db) /* {{{ */ if (db->pass != NULL) credentials_size += strlen (db->pass); - db->credentials = (char *) malloc (credentials_size); + db->credentials = malloc (credentials_size); if (db->credentials == NULL) { ERROR ("curl_xml plugin: malloc failed."); @@ -923,13 +922,12 @@ static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */ return (-1); } - db = (cx_t *) malloc (sizeof (*db)); + db = calloc (1, sizeof (*db)); if (db == NULL) { - ERROR ("curl_xml plugin: malloc failed."); + ERROR ("curl_xml plugin: calloc failed."); return (-1); } - memset (db, 0, sizeof (*db)); db->timeout = -1; diff --git a/src/daemon/common.c b/src/daemon/common.c index dd4f9b15..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); @@ -144,7 +143,7 @@ char *sstrdup (const char *s) /* Do not use `strdup' here, because it's not specified in POSIX. It's * ``only'' an XSI extension. */ sz = strlen (s) + 1; - r = (char *) malloc (sizeof (char) * sz); + r = malloc (sz); if (r == NULL) { ERROR ("sstrdup: Out of memory."); @@ -409,10 +408,9 @@ int escape_string (char *buffer, size_t buffer_size) if (buffer_size < 3) return (EINVAL); - temp = (char *) 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 8a7621d2..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 = (oconfig_item_t *) malloc (sizeof (oconfig_item_t)); + 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 = (oconfig_item_t *) malloc (sizeof (oconfig_item_t)); + 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. */ @@ -1067,7 +1065,7 @@ void cf_register (const char *type, cf_unregister (type); /* This pointer will be free'd in `cf_unregister' */ - if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL) + if ((cf_cb = malloc (sizeof (*cf_cb))) == NULL) return; cf_cb->type = type; @@ -1084,7 +1082,7 @@ int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *)) { cf_complex_callback_t *new; - new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t)); + new = malloc (sizeof (*new)); if (new == NULL) return (-1); diff --git a/src/daemon/filter_chain.c b/src/daemon/filter_chain.c index ecc7f165..babf3996 100644 --- a/src/daemon/filter_chain.c +++ b/src/daemon/filter_chain.c @@ -174,7 +174,7 @@ static char *fc_strdup (const char *orig) /* {{{ */ return (NULL); sz = strlen (orig) + 1; - dest = (char *) malloc (sz); + dest = malloc (sz); if (dest == NULL) return (NULL); @@ -235,13 +235,12 @@ static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */ return (-1); } - m = (fc_match_t *) 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 = (fc_target_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 = (fc_rule_t *) 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 = (fc_chain_t *) 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 = (fc_match_t *) 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 = (fc_target_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 e1d0ec5b..cb8a195c 100644 --- a/src/daemon/meta_data.c +++ b/src/daemon/meta_data.c @@ -71,7 +71,7 @@ static char *md_strdup (const char *orig) /* {{{ */ return (NULL); sz = strlen (orig) + 1; - dest = (char *) malloc (sz); + dest = malloc (sz); if (dest == NULL) return (NULL); @@ -84,13 +84,12 @@ static meta_entry_t *md_entry_alloc (const char *key) /* {{{ */ { meta_entry_t *e; - e = (meta_entry_t *) 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 = (meta_data_t *) 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 e7993f1e..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 = (callback_func_t *) 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)); @@ -1351,7 +1348,7 @@ static char *plugin_flush_callback_name (const char *name) prefix_size = strlen(flush_prefix); name_size = strlen(name); - flush_name = malloc (sizeof(char) * (name_size + prefix_size + 1)); + flush_name = malloc (name_size + prefix_size + 1); if (flush_name == NULL) { ERROR ("plugin_flush_callback_name: malloc failed."); @@ -1384,7 +1381,7 @@ int plugin_register_flush (const char *name, if (flush_name == NULL) return (-1); - cb = malloc(sizeof(flush_callback_t)); + cb = malloc(sizeof (*cb)); if (cb == NULL) { ERROR ("plugin_register_flush: malloc failed."); @@ -1477,12 +1474,12 @@ int plugin_register_data_set (const data_set_t *ds) return (-1); } - ds_copy = (data_set_t *) malloc (sizeof (data_set_t)); + ds_copy = malloc (sizeof (*ds_copy)); if (ds_copy == NULL) return (-1); memcpy(ds_copy, ds, sizeof (data_set_t)); - ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t) + ds_copy->ds = malloc (sizeof (*ds_copy->ds) * ds->ds_num); if (ds_copy->ds == NULL) { @@ -2578,13 +2575,12 @@ static int plugin_notification_meta_add (notification_t *n, return (-1); } - meta = (notification_meta_t *) malloc (sizeof (notification_meta_t)); + 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 eb50bb8a..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 = (data_set_t *) malloc (sizeof (data_set_t)); + 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 fcfbb945..1680c41f 100644 --- a/src/daemon/utils_avltree.c +++ b/src/daemon/utils_avltree.c @@ -485,7 +485,7 @@ c_avl_tree_t *c_avl_create (int (*compare) (const void *, const void *)) if (compare == NULL) return (NULL); - if ((t = (c_avl_tree_t *) malloc (sizeof (c_avl_tree_t))) == NULL) + if ((t = malloc (sizeof (*t))) == NULL) return (NULL); t->root = NULL; @@ -509,7 +509,7 @@ int c_avl_insert (c_avl_tree_t *t, void *key, void *value) c_avl_node_t *nptr; int cmp; - if ((new = (c_avl_node_t *) malloc (sizeof (c_avl_node_t))) == NULL) + if ((new = malloc (sizeof (*new))) == NULL) return (-1); new->key = key; @@ -665,10 +665,9 @@ c_avl_iterator_t *c_avl_get_iterator (c_avl_tree_t *t) if (t == NULL) return (NULL); - iter = (c_avl_iterator_t *) malloc (sizeof (c_avl_iterator_t)); + 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 07568073..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 = (cache_entry_t *) malloc (sizeof (cache_entry_t)); + 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)); @@ -499,7 +498,7 @@ int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_val else { ret_num = ce->values_num; - ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t)); + ret = malloc (ret_num * sizeof (*ret)); if (ret == NULL) { ERROR ("utils_cache: uc_get_rate_by_name: malloc failed."); 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 12688380..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(ignorelist_item_t))) == 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 4265286b..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 = (llist_t *) malloc (sizeof (llist_t)); + ret = calloc (1, sizeof (*ret)); if (ret == NULL) return (NULL); - memset (ret, '\0', sizeof (llist_t)); - return (ret); } @@ -78,7 +76,7 @@ llentry_t *llentry_create (char *key, void *value) { llentry_t *e; - e = (llentry_t *) malloc (sizeof (llentry_t)); + e = malloc (sizeof (*e)); if (e) { e->key = key; diff --git a/src/daemon/utils_match.c b/src/daemon/utils_match.c index 5083b05a..cf87b6b7 100644 --- a/src/daemon/utils_match.c +++ b/src/daemon/utils_match.c @@ -63,7 +63,7 @@ static char *match_substr (const char *str, int begin, int end) } ret_len = end - begin; - ret = (char *) malloc (sizeof (char) * (ret_len + 1)); + ret = malloc (ret_len + 1); if (ret == NULL) { ERROR ("utils_match: match_substr: malloc failed."); @@ -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 = (cu_match_t *) malloc (sizeof (cu_match_t)); + 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 = (cu_match_value_t *) malloc (sizeof (cu_match_value_t)); + 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_subst.c b/src/daemon/utils_subst.c index cfa1c764..9284f733 100644 --- a/src/daemon/utils_subst.c +++ b/src/daemon/utils_subst.c @@ -102,7 +102,7 @@ char *asubst (const char *string, int off1, int off2, const char *replacement) len = off1 + strlen (replacement) + strlen (string) - off2 + 1; - buf = (char *)malloc (len); + buf = malloc (len); if (NULL == buf) return NULL; diff --git a/src/daemon/utils_tail.c b/src/daemon/utils_tail.c index fe5dca89..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 = (cu_tail_t *) malloc (sizeof (cu_tail_t)); + 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 8776ad11..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 = (cu_tail_match_t *) malloc (sizeof (cu_tail_match_t)); + 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 = (cu_tail_match_simple_t *) malloc (sizeof (cu_tail_match_simple_t)); + 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) diff --git a/src/dbi.c b/src/dbi.c index 94d0762d..7dec51ad 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -294,13 +294,12 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ return (-1); } - db = (cdbi_database_t *) malloc (sizeof (*db)); + db = calloc (1, sizeof (*db)); if (db == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); return (-1); } - memset (db, 0, sizeof (*db)); status = cf_util_get_string (ci, &db->name); if (status != 0) @@ -357,12 +356,11 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ while ((status == 0) && (db->queries_num > 0)) { size_t j; - db->q_prep_areas = (udb_query_preparation_area_t **) calloc ( - db->queries_num, sizeof (*db->q_prep_areas)); + db->q_prep_areas = calloc (db->queries_num, sizeof (*db->q_prep_areas)); if (db->q_prep_areas == NULL) { - WARNING ("dbi plugin: malloc failed"); + WARNING ("dbi plugin: calloc failed"); status = -1; break; } @@ -548,35 +546,33 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ } /* Allocate `column_names' and `column_values'. {{{ */ - column_names = (char **) calloc (column_num, sizeof (char *)); + column_names = calloc (column_num, sizeof (*column_names)); if (column_names == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); BAIL_OUT (-1); } - column_names[0] = (char *) calloc (column_num, - DATA_MAX_NAME_LEN * sizeof (char)); + column_names[0] = calloc (column_num, DATA_MAX_NAME_LEN); if (column_names[0] == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); BAIL_OUT (-1); } for (i = 1; i < column_num; i++) column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN; - column_values = (char **) calloc (column_num, sizeof (char *)); + column_values = calloc (column_num, sizeof (*column_values)); if (column_values == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); BAIL_OUT (-1); } - column_values[0] = (char *) calloc (column_num, - DATA_MAX_NAME_LEN * sizeof (char)); + column_values[0] = calloc (column_num, DATA_MAX_NAME_LEN); if (column_values[0] == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); BAIL_OUT (-1); } for (i = 1; i < column_num; i++) diff --git a/src/dns.c b/src/dns.c index 3421c475..53fa725e 100644 --- a/src/dns.c +++ b/src/dns.c @@ -94,11 +94,10 @@ static counter_list_t *counter_list_create (counter_list_t **list, { counter_list_t *entry; - entry = (counter_list_t *) malloc (sizeof (counter_list_t)); + entry = calloc (1, sizeof (*entry)); if (entry == NULL) return (NULL); - memset (entry, 0, sizeof (counter_list_t)); entry->key = key; entry->value = value; diff --git a/src/email.c b/src/email.c index 81ae29c3..ea4e7850 100644 --- a/src/email.c +++ b/src/email.c @@ -219,7 +219,7 @@ static int email_config (const char *key, const char *value) static void type_list_incr (type_list_t *list, char *name, int incr) { if (NULL == list->head) { - list->head = (type_t *)smalloc (sizeof (type_t)); + list->head = smalloc (sizeof (*list->head)); list->head->name = sstrdup (name); list->head->value = incr; @@ -236,7 +236,7 @@ static void type_list_incr (type_list_t *list, char *name, int incr) } if (NULL == ptr) { - list->tail->next = (type_t *)smalloc (sizeof (type_t)); + list->tail->next = smalloc (sizeof (*list->tail->next)); list->tail = list->tail->next; list->tail->name = sstrdup (name); @@ -482,10 +482,10 @@ static void *open_connection (void __attribute__((unused)) *arg) available_collectors = max_conns; collectors = - (collector_t **)smalloc (max_conns * sizeof (collector_t *)); + smalloc (max_conns * sizeof (*collectors)); for (i = 0; i < max_conns; ++i) { - collectors[i] = (collector_t *)smalloc (sizeof (collector_t)); + collectors[i] = smalloc (sizeof (*collectors[i])); collectors[i]->socket = NULL; if (0 != (err = plugin_thread_create (&collectors[i]->thread, @@ -537,13 +537,12 @@ static void *open_connection (void __attribute__((unused)) *arg) break; } - connection = malloc (sizeof (*connection)); + connection = calloc (1, sizeof (*connection)); if (connection == NULL) { close (remote); continue; } - memset (connection, 0, sizeof (*connection)); connection->socket = fdopen (remote, "r"); connection->next = NULL; @@ -695,7 +694,7 @@ static void copy_type_list (type_list_t *l1, type_list_t *l2) for (ptr1 = l1->head, ptr2 = l2->head; NULL != ptr1; ptr1 = ptr1->next, last = ptr2, ptr2 = ptr2->next) { if (NULL == ptr2) { - ptr2 = (type_t *)smalloc (sizeof (type_t)); + ptr2 = smalloc (sizeof (*ptr2)); ptr2->name = NULL; ptr2->next = NULL; diff --git a/src/ethstat.c b/src/ethstat.c index dec14f3d..8971f4df 100644 --- a/src/ethstat.c +++ b/src/ethstat.c @@ -104,14 +104,13 @@ static int ethstat_add_map (const oconfig_item_t *ci) /* {{{ */ return (ENOMEM); } - map = malloc (sizeof (*map)); + map = calloc (1, sizeof (*map)); if (map == NULL) { sfree (key); - ERROR ("ethstat plugin: malloc(3) failed."); + ERROR ("ethstat plugin: calloc failed."); return (ENOMEM); } - memset (map, 0, sizeof (*map)); sstrncpy (map->type, ci->values[1].value.string, sizeof (map->type)); if (ci->values_num == 3) @@ -273,7 +272,7 @@ static int ethstat_read_interface (char *device) close (fd); sfree (strings); sfree (stats); - ERROR("ethstat plugin: malloc(3) failed."); + ERROR("ethstat plugin: malloc failed."); return (-1); } diff --git a/src/exec.c b/src/exec.c index f080ad67..85f6cb80 100644 --- a/src/exec.c +++ b/src/exec.c @@ -126,13 +126,12 @@ static int exec_config_exec (oconfig_item_t *ci) /* {{{ */ return (-1); } - pl = (program_list_t *) malloc (sizeof (program_list_t)); + pl = calloc (1, sizeof (*pl)); if (pl == NULL) { - ERROR ("exec plugin: malloc failed."); + ERROR ("exec plugin: calloc failed."); return (-1); } - memset (pl, '\0', sizeof (program_list_t)); if (strcasecmp ("NotificationExec", ci->key) == 0) pl->flags |= PL_NOTIF_ACTION; @@ -163,16 +162,15 @@ static int exec_config_exec (oconfig_item_t *ci) /* {{{ */ return (-1); } - pl->argv = (char **) malloc (ci->values_num * sizeof (char *)); + pl->argv = calloc (ci->values_num, sizeof (*pl->argv)); if (pl->argv == NULL) { - ERROR ("exec plugin: malloc failed."); + ERROR ("exec plugin: calloc failed."); sfree (pl->exec); sfree (pl->user); sfree (pl); return (-1); } - memset (pl->argv, '\0', ci->values_num * sizeof (char *)); { char *tmp = strrchr (ci->values[1].value.string, '/'); @@ -184,7 +182,7 @@ static int exec_config_exec (oconfig_item_t *ci) /* {{{ */ pl->argv[0] = strdup (buffer); if (pl->argv[0] == NULL) { - ERROR ("exec plugin: malloc failed."); + ERROR ("exec plugin: strdup failed."); sfree (pl->argv); sfree (pl->exec); sfree (pl->user); @@ -869,8 +867,7 @@ static int exec_notification (const notification_t *n, /* {{{ */ if (pl->pid != 0) continue; - pln = (program_list_and_notification_t *) malloc (sizeof - (program_list_and_notification_t)); + pln = malloc (sizeof (*pln)); if (pln == NULL) { ERROR ("exec plugin: malloc failed."); diff --git a/src/filecount.c b/src/filecount.c index 9ea8af7a..3b5038cd 100644 --- a/src/filecount.c +++ b/src/filecount.c @@ -344,13 +344,12 @@ static int fc_config_add_dir (oconfig_item_t *ci) } /* Initialize `dir' */ - dir = (fc_directory_conf_t *) malloc (sizeof (*dir)); + dir = calloc (1, sizeof (*dir)); if (dir == NULL) { - ERROR ("filecount plugin: malloc failed."); + ERROR ("filecount plugin: calloc failed."); return (-1); } - memset (dir, 0, sizeof (*dir)); dir->path = strdup (ci->values[0].value.string); if (dir->path == NULL) diff --git a/src/gmond.c b/src/gmond.c index f86d8f7a..c763cd96 100644 --- a/src/gmond.c +++ b/src/gmond.c @@ -454,10 +454,9 @@ static staging_entry_t *staging_entry_get (const char *host, /* {{{ */ return (se); /* insert new entry */ - se = (staging_entry_t *) malloc (sizeof (*se)); + se = calloc (1, sizeof (*se)); if (se == NULL) return (NULL); - memset (se, 0, sizeof (*se)); sstrncpy (se->key, key, sizeof (se->key)); se->flags = 0; diff --git a/src/interface.c b/src/interface.c index 90dc139d..d05dd723 100644 --- a/src/interface.c +++ b/src/interface.c @@ -368,9 +368,8 @@ static int interface_read (void) if (pnif != nif || ifstat == NULL) { - if (ifstat != NULL) - free(ifstat); - ifstat = malloc(nif * sizeof(perfstat_netinterface_t)); + free(ifstat); + ifstat = malloc(nif * sizeof (*ifstat)); } pnif = nif; diff --git a/src/ipc.c b/src/ipc.c index 645d4b08..00b3a5f8 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -213,7 +213,7 @@ static caddr_t ipc_get_info (cid_t cid, int cmd, int version, int stsize, int *n *nmemb = size / stsize; - buff = (caddr_t)malloc (size); + buff = malloc (size); if (buff == NULL) { ERROR ("ipc plugin: ipc_get_info malloc failed."); return (NULL); diff --git a/src/iptables.c b/src/iptables.c index 05e3e24b..1e35851a 100644 --- a/src/iptables.c +++ b/src/iptables.c @@ -215,7 +215,7 @@ static int iptables_config (const char *key, const char *value) } chain_list = list; - final = (ip_chain_t *) malloc( sizeof(temp) ); + final = malloc(sizeof (*final)); if (final == NULL) { char errbuf[1024]; diff --git a/src/java.c b/src/java.c index 6b92f54e..50cd6965 100644 --- a/src/java.c +++ b/src/java.c @@ -1801,14 +1801,13 @@ static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ return (NULL); } - cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi)); + cbi = calloc (1, sizeof (*cbi)); if (cbi == NULL) { - ERROR ("java plugin: cjni_callback_info_create: malloc failed."); + ERROR ("java plugin: cjni_callback_info_create: calloc failed."); (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name); return (NULL); } - memset (cbi, 0, sizeof (*cbi)); cbi->type = type; cbi->name = strdup (c_name); @@ -2067,13 +2066,12 @@ static JNIEnv *cjni_thread_attach (void) /* {{{ */ if (cjni_env == NULL) { /* This pointer is free'd in `cjni_jvm_env_destroy'. */ - cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env)); + cjni_env = calloc (1, sizeof (*cjni_env)); if (cjni_env == NULL) { - ERROR ("java plugin: cjni_thread_attach: malloc failed."); + ERROR ("java plugin: cjni_thread_attach: calloc failed."); return (NULL); } - memset (cjni_env, 0, sizeof (*cjni_env)); cjni_env->reference_counter = 0; cjni_env->jvm_env = NULL; @@ -2801,13 +2799,13 @@ static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */ /* Allocate a new callback info structure. This is going to be our user_data * pointer. */ - cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret)); + cbi_ret = calloc (1, sizeof (*cbi_ret)); if (cbi_ret == NULL) { - ERROR ("java plugin: cjni_match_target_create: malloc failed."); + ERROR ("java plugin: cjni_match_target_create: calloc failed."); BAIL_OUT (-1); } - memset (cbi_ret, 0, sizeof (*cbi_ret)); + cbi_ret->object = NULL; cbi_ret->type = type; diff --git a/src/libcollectdclient/client.c b/src/libcollectdclient/client.c index 77819c21..8b5ac0bd 100644 --- a/src/libcollectdclient/client.c +++ b/src/libcollectdclient/client.c @@ -312,7 +312,7 @@ static int lcc_receive (lcc_connection_t *c, /* {{{ */ /* Allocate space for the char-pointers */ res.lines_num = (size_t) res.status; res.status = 0; - res.lines = (char **) malloc (res.lines_num * sizeof (char *)); + res.lines = malloc (res.lines_num * sizeof (*res.lines)); if (res.lines == NULL) { lcc_set_errno (c, ENOMEM); @@ -584,10 +584,9 @@ int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */ if (ret_con == NULL) return (-1); - c = (lcc_connection_t *) malloc (sizeof (*c)); + c = calloc (1, sizeof (*c)); if (c == NULL) return (-1); - memset (c, 0, sizeof (*c)); status = lcc_open_socket (c, address); if (status != 0) @@ -687,7 +686,7 @@ int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */ /* Allocate space for the values */ if (ret_values != NULL) { - values = (gauge_t *) malloc (values_num * sizeof (*values)); + values = malloc (values_num * sizeof (*values)); if (values == NULL) BAIL_OUT (ENOMEM); } @@ -894,7 +893,7 @@ int lcc_listval (lcc_connection_t *c, /* {{{ */ } ident_num = res.lines_num; - ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident)); + ident = malloc (ident_num * sizeof (*ident)); if (ident == NULL) { lcc_response_free (&res); diff --git a/src/libcollectdclient/network.c b/src/libcollectdclient/network.c index 7a866ee7..4564fbbe 100644 --- a/src/libcollectdclient/network.c +++ b/src/libcollectdclient/network.c @@ -271,10 +271,9 @@ lcc_network_t *lcc_network_create (void) /* {{{ */ { lcc_network_t *net; - net = malloc (sizeof (*net)); + net = calloc (1, sizeof (*net)); if (net == NULL) return (NULL); - memset (net, 0, sizeof (*net)); net->servers = NULL; @@ -299,10 +298,9 @@ lcc_server_t *lcc_server_create (lcc_network_t *net, /* {{{ */ if (service == NULL) service = NET_DEFAULT_PORT; - srv = malloc (sizeof (*srv)); + srv = calloc (1, sizeof (*srv)); if (srv == NULL) return (NULL); - memset (srv, 0, sizeof (*srv)); srv->fd = -1; srv->security_level = NONE; diff --git a/src/libcollectdclient/network_buffer.c b/src/libcollectdclient/network_buffer.c index 343c285a..130da235 100644 --- a/src/libcollectdclient/network_buffer.c +++ b/src/libcollectdclient/network_buffer.c @@ -651,19 +651,17 @@ lcc_network_buffer_t *lcc_network_buffer_create (size_t size) /* {{{ */ return (NULL); } - nb = malloc (sizeof (*nb)); + nb = calloc (1, sizeof (*nb)); if (nb == NULL) return (NULL); - memset (nb, 0, sizeof (*nb)); nb->size = size; - nb->buffer = malloc (nb->size); + nb->buffer = calloc (1, nb->size); if (nb->buffer == NULL) { free (nb); return (NULL); } - memset (nb->buffer, 0, nb->size); nb->ptr = nb->buffer; nb->free = nb->size; diff --git a/src/liboconfig/oconfig.c b/src/liboconfig/oconfig.c index 89ccdecb..b4b82442 100644 --- a/src/liboconfig/oconfig.c +++ b/src/liboconfig/oconfig.c @@ -106,13 +106,12 @@ oconfig_item_t *oconfig_clone (const oconfig_item_t *ci_orig) { oconfig_item_t *ci_copy; - ci_copy = (oconfig_item_t *) malloc (sizeof (*ci_copy)); + ci_copy = calloc (1, sizeof (*ci_copy)); if (ci_copy == NULL) { - fprintf (stderr, "malloc failed.\n"); + fprintf (stderr, "calloc failed.\n"); return (NULL); } - memset (ci_copy, 0, sizeof (*ci_copy)); ci_copy->values = NULL; ci_copy->parent = NULL; ci_copy->children = NULL; diff --git a/src/liboconfig/parser.y b/src/liboconfig/parser.y index 803eec26..1eb7aece 100644 --- a/src/liboconfig/parser.y +++ b/src/liboconfig/parser.y @@ -208,15 +208,13 @@ statement_list: entire_file: statement_list { - ci_root = malloc (sizeof (oconfig_item_t)); - memset (ci_root, '\0', sizeof (oconfig_item_t)); + ci_root = calloc (1, sizeof (*ci_root)); ci_root->children = $1.statement; ci_root->children_num = $1.statement_num; } | /* epsilon */ { - ci_root = malloc (sizeof (oconfig_item_t)); - memset (ci_root, '\0', sizeof (oconfig_item_t)); + ci_root = calloc (1, sizeof (*ci_root)); ci_root->children = NULL; ci_root->children_num = 0; } diff --git a/src/match_empty_counter.c b/src/match_empty_counter.c index e30ff91b..8f99a6d1 100644 --- a/src/match_empty_counter.c +++ b/src/match_empty_counter.c @@ -46,13 +46,12 @@ static int mec_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ { mec_match_t *m; - m = (mec_match_t *) malloc (sizeof (*m)); + m = calloc (1, sizeof (*m)); if (m == NULL) { - ERROR ("mec_create: malloc failed."); + ERROR ("mec_create: calloc failed."); return (-ENOMEM); } - memset (m, 0, sizeof (*m)); if (ci->children_num != 0) { diff --git a/src/match_hashed.c b/src/match_hashed.c index ba0c47c3..4178d757 100644 --- a/src/match_hashed.c +++ b/src/match_hashed.c @@ -101,13 +101,12 @@ static int mh_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ mh_match_t *m; int i; - m = (mh_match_t *) malloc (sizeof (*m)); + m = calloc (1, sizeof (*m)); if (m == NULL) { - ERROR ("mh_create: malloc failed."); + ERROR ("mh_create: calloc failed."); return (-ENOMEM); } - memset (m, 0, sizeof (*m)); for (i = 0; i < ci->children_num; i++) { diff --git a/src/match_regex.c b/src/match_regex.c index 4fa6ce78..6e6bda2a 100644 --- a/src/match_regex.c +++ b/src/match_regex.c @@ -140,13 +140,12 @@ static int mr_config_add_regex (mr_regex_t **re_head, /* {{{ */ return (-1); } - re = (mr_regex_t *) malloc (sizeof (*re)); + re = calloc (1, sizeof (*re)); if (re == NULL) { - log_err ("mr_config_add_regex: malloc failed."); + log_err ("mr_config_add_regex: calloc failed."); return (-1); } - memset (re, 0, sizeof (*re)); re->next = NULL; re->re_str = strdup (ci->values[0].value.string); @@ -194,14 +193,13 @@ static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ int status; int i; - m = (mr_match_t *) malloc (sizeof (*m)); + m = calloc (1, sizeof (*m)); if (m == NULL) { - log_err ("mr_create: malloc failed."); + log_err ("mr_create: calloc failed."); return (-ENOMEM); } - memset (m, 0, sizeof (*m)); - + m->invert = 0; status = 0; diff --git a/src/match_timediff.c b/src/match_timediff.c index 996201ad..77c8f519 100644 --- a/src/match_timediff.c +++ b/src/match_timediff.c @@ -52,13 +52,12 @@ static int mt_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ int status; int i; - m = (mt_match_t *) malloc (sizeof (*m)); + m = calloc (1, sizeof (*m)); if (m == NULL) { - ERROR ("mt_create: malloc failed."); + ERROR ("mt_create: calloc failed."); return (-ENOMEM); } - memset (m, 0, sizeof (*m)); m->future = 0; m->past = 0; diff --git a/src/match_value.c b/src/match_value.c index 9ffcecaa..8c6fd36c 100644 --- a/src/match_value.c +++ b/src/match_value.c @@ -200,13 +200,12 @@ static int mv_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ int status; int i; - m = (mv_match_t *) malloc (sizeof (*m)); + m = calloc (1, sizeof (*m)); if (m == NULL) { - ERROR ("mv_create: malloc failed."); + ERROR ("mv_create: calloc failed."); return (-ENOMEM); } - memset (m, 0, sizeof (*m)); m->min = NAN; m->max = NAN; diff --git a/src/memcachec.c b/src/memcachec.c index 013b6c4e..1e1d42dc 100644 --- a/src/memcachec.c +++ b/src/memcachec.c @@ -206,13 +206,12 @@ static int cmc_config_add_match (web_page_t *page, /* {{{ */ WARNING ("memcachec plugin: Ignoring arguments for the `Match' block."); } - match = (web_match_t *) malloc (sizeof (*match)); + match = calloc (1, sizeof (*match)); if (match == NULL) { - ERROR ("memcachec plugin: malloc failed."); + ERROR ("memcachec plugin: calloc failed."); return (-1); } - memset (match, 0, sizeof (*match)); status = 0; for (i = 0; i < ci->children_num; i++) @@ -305,13 +304,12 @@ static int cmc_config_add_page (oconfig_item_t *ci) /* {{{ */ return (-1); } - page = (web_page_t *) malloc (sizeof (*page)); + page = calloc (1, sizeof (*page)); if (page == NULL) { - ERROR ("memcachec plugin: malloc failed."); + ERROR ("memcachec plugin: calloc failed."); return (-1); } - memset (page, 0, sizeof (*page)); page->server = NULL; page->key = NULL; diff --git a/src/memcached.c b/src/memcached.c index 2c01a576..4e7b1431 100644 --- a/src/memcached.c +++ b/src/memcached.c @@ -587,14 +587,13 @@ static int config_add_instance(oconfig_item_t *ci) /* Disable automatic generation of default instance in the init callback. */ memcached_have_instances = 1; - st = malloc (sizeof (*st)); + st = calloc (1, sizeof (*st)); if (st == NULL) { - ERROR ("memcached plugin: malloc failed."); + ERROR ("memcached plugin: calloc failed."); return (-1); } - memset (st, 0, sizeof (*st)); st->name = NULL; st->socket = NULL; st->host = NULL; @@ -685,10 +684,9 @@ static int memcached_init (void) return (0); /* No instances were configured, lets start a default instance. */ - st = malloc (sizeof (*st)); + st = calloc (1, sizeof (*st)); if (st == NULL) return (ENOMEM); - memset (st, 0, sizeof (*st)); st->name = sstrdup ("__legacy__"); st->socket = NULL; st->host = NULL; diff --git a/src/modbus.c b/src/modbus.c index d538fb3f..2605ec23 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -938,10 +938,9 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */ int status; int i; - host = malloc (sizeof (*host)); + host = calloc (1, sizeof (*host)); if (host == NULL) return (ENOMEM); - memset (host, 0, sizeof (*host)); host->slaves = NULL; status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host)); diff --git a/src/mqtt.c b/src/mqtt.c index 4977f684..0b00bab8 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -559,7 +559,7 @@ static int mqtt_config_publisher (oconfig_item_t *ci) conf = calloc (1, sizeof (*conf)); if (conf == NULL) { - ERROR ("mqtt plugin: malloc failed."); + ERROR ("mqtt plugin: calloc failed."); return (-1); } conf->publish = 1; @@ -664,7 +664,7 @@ static int mqtt_config_subscriber (oconfig_item_t *ci) conf = calloc (1, sizeof (*conf)); if (conf == NULL) { - ERROR ("mqtt plugin: malloc failed."); + ERROR ("mqtt plugin: calloc failed."); return (-1); } conf->publish = 0; diff --git a/src/mysql.c b/src/mysql.c index 38507456..4c94732b 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -113,13 +113,12 @@ static int mysql_config_database (oconfig_item_t *ci) /* {{{ */ return (-1); } - db = (mysql_database_t *) malloc (sizeof (*db)); + db = calloc (1, sizeof (*db)); if (db == NULL) { - ERROR ("mysql plugin: malloc failed."); + ERROR ("mysql plugin: calloc failed."); return (-1); } - memset (db, 0, sizeof (*db)); /* initialize all the pointers */ db->alias = NULL; diff --git a/src/netapp.c b/src/netapp.c index 8a8a7fe4..51a63318 100644 --- a/src/netapp.c +++ b/src/netapp.c @@ -464,10 +464,9 @@ static disk_t *get_disk(cfg_disk_t *cd, const char *name) /* {{{ */ return d; } - d = malloc(sizeof(*d)); + d = calloc (1, sizeof (*d)); if (d == NULL) return (NULL); - memset (d, 0, sizeof (*d)); d->next = NULL; d->name = strdup(name); @@ -513,10 +512,9 @@ static data_volume_usage_t *get_volume_usage (cfg_volume_usage_t *cvu, /* {{{ */ return (NULL); /* Not found: allocate. */ - new = malloc (sizeof (*new)); + new = calloc (1, sizeof (*new)); if (new == NULL) return (NULL); - memset (new, 0, sizeof (*new)); new->next = NULL; new->name = strdup (name); @@ -581,10 +579,9 @@ static data_volume_perf_t *get_volume_perf (cfg_volume_perf_t *cvp, /* {{{ */ return (NULL); /* Not found: allocate. */ - new = malloc (sizeof (*new)); + new = calloc (1, sizeof (*new)); if (new == NULL) return (NULL); - memset (new, 0, sizeof (*new)); new->next = NULL; new->name = strdup (name); @@ -2405,10 +2402,9 @@ static int cna_config_volume_performance (host_config_t *host, /* {{{ */ if (host->cfg_volume_perf == NULL) { - cfg_volume_perf = malloc (sizeof (*cfg_volume_perf)); + cfg_volume_perf = calloc (1, sizeof (*cfg_volume_perf)); if (cfg_volume_perf == NULL) return (ENOMEM); - memset (cfg_volume_perf, 0, sizeof (*cfg_volume_perf)); /* Set default flags */ cfg_volume_perf->query = NULL; @@ -2533,10 +2529,9 @@ static int cna_config_quota (host_config_t *host, oconfig_item_t *ci) /* {{{ */ if (host->cfg_quota == NULL) { - cfg_quota = malloc (sizeof (*cfg_quota)); + cfg_quota = calloc (1, sizeof (*cfg_quota)); if (cfg_quota == NULL) return (ENOMEM); - memset (cfg_quota, 0, sizeof (*cfg_quota)); cfg_quota->query = NULL; host->cfg_quota = cfg_quota; @@ -2566,10 +2561,9 @@ static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */ if (host->cfg_disk == NULL) { - cfg_disk = malloc (sizeof (*cfg_disk)); + cfg_disk = calloc (1, sizeof (*cfg_disk)); if (cfg_disk == NULL) return (ENOMEM); - memset (cfg_disk, 0, sizeof (*cfg_disk)); /* Set default flags */ cfg_disk->flags = CFG_DISK_ALL; @@ -2612,10 +2606,9 @@ static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */ if (host->cfg_wafl == NULL) { - cfg_wafl = malloc (sizeof (*cfg_wafl)); + cfg_wafl = calloc (1, sizeof (*cfg_wafl)); if (cfg_wafl == NULL) return (ENOMEM); - memset (cfg_wafl, 0, sizeof (*cfg_wafl)); /* Set default flags */ cfg_wafl->flags = CFG_WAFL_ALL; @@ -2681,10 +2674,9 @@ static int cna_config_volume_usage(host_config_t *host, /* {{{ */ if (host->cfg_volume_usage == NULL) { - cfg_volume_usage = malloc (sizeof (*cfg_volume_usage)); + cfg_volume_usage = calloc (1, sizeof (*cfg_volume_usage)); if (cfg_volume_usage == NULL) return (ENOMEM); - memset (cfg_volume_usage, 0, sizeof (*cfg_volume_usage)); /* Set default flags */ cfg_volume_usage->query = NULL; @@ -2743,10 +2735,9 @@ static int cna_config_snapvault (host_config_t *host, /* {{{ */ if (host->cfg_snapvault == NULL) { - cfg_snapvault = malloc (sizeof (*cfg_snapvault)); + cfg_snapvault = calloc (1, sizeof (*cfg_snapvault)); if (cfg_snapvault == NULL) return ENOMEM; - memset (cfg_snapvault, 0, sizeof (*cfg_snapvault)); cfg_snapvault->query = NULL; host->cfg_snapvault = cfg_snapvault; @@ -2773,16 +2764,15 @@ static int cna_config_system (host_config_t *host, /* {{{ */ { cfg_system_t *cfg_system; int i; - + if ((host == NULL) || (ci == NULL)) return (EINVAL); if (host->cfg_system == NULL) { - cfg_system = malloc (sizeof (*cfg_system)); + cfg_system = calloc (1, sizeof (*cfg_system)); if (cfg_system == NULL) return (ENOMEM); - memset (cfg_system, 0, sizeof (*cfg_system)); /* Set default flags */ cfg_system->flags = CFG_SYSTEM_ALL; @@ -2827,10 +2817,9 @@ static host_config_t *cna_alloc_host (void) /* {{{ */ { host_config_t *host; - host = malloc(sizeof(*host)); - if (! host) + host = calloc (1, sizeof (*host)); + if (host == NULL) return (NULL); - memset (host, 0, sizeof (*host)); host->name = NULL; host->protocol = NA_SERVER_TRANSPORT_HTTPS; diff --git a/src/netlink.c b/src/netlink.c index d4661ce8..c8905d5c 100644 --- a/src/netlink.c +++ b/src/netlink.c @@ -109,12 +109,10 @@ static int add_ignorelist (const char *dev, const char *type, { ir_ignorelist_t *entry; - entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t)); + entry = calloc (1, sizeof (*entry)); if (entry == NULL) return (-1); - memset (entry, '\0', sizeof (ir_ignorelist_t)); - if (strcasecmp (dev, "All") != 0) { entry->device = strdup (dev); diff --git a/src/network.c b/src/network.c index 086f04ec..6be4ed59 100644 --- a/src/network.c +++ b/src/network.c @@ -618,14 +618,14 @@ static int write_part_values (char **ret_buffer, int *ret_buffer_len, if (*ret_buffer_len < packet_len) return (-1); - pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t)); + pkg_values_types = malloc (num_values * sizeof (*pkg_values_types)); if (pkg_values_types == NULL) { ERROR ("network plugin: write_part_values: malloc failed."); return (-1); } - pkg_values = (value_t *) malloc (num_values * sizeof (value_t)); + pkg_values = malloc (num_values * sizeof (*pkg_values)); if (pkg_values == NULL) { free (pkg_values_types); @@ -2012,10 +2012,9 @@ static sockent_t *sockent_create (int type) /* {{{ */ if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER)) return (NULL); - se = malloc (sizeof (*se)); + se = calloc (1, sizeof (*se)); if (se == NULL) return (NULL); - memset (se, 0, sizeof (*se)); se->type = type; se->node = NULL; @@ -2196,16 +2195,15 @@ static int sockent_client_connect (sockent_t *se) /* {{{ */ continue; } - client->addr = malloc (sizeof (*client->addr)); + client->addr = calloc (1, sizeof (*client->addr)); if (client->addr == NULL) { - ERROR ("network plugin: malloc failed."); + ERROR ("network plugin: calloc failed."); close (client->fd); client->fd = -1; continue; } - memset (client->addr, 0, sizeof (*client->addr)); assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen); memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen); client->addrlen = ai_ptr->ai_addrlen; @@ -2495,15 +2493,15 @@ static int network_receive (void) /* {{{ */ * these entries in the dispatch thread but put them in * another list, so we don't have to allocate more and * more of these structures. */ - ent = malloc (sizeof (receive_list_entry_t)); + ent = calloc (1, sizeof (*ent)); if (ent == NULL) { - ERROR ("network plugin: malloc failed."); + ERROR ("network plugin: calloc failed."); status = ENOMEM; break; } - memset (ent, 0, sizeof (receive_list_entry_t)); - ent->data = malloc (network_config_packet_size); + + ent->data = malloc (*ent->data); if (ent->data == NULL) { sfree (ent); diff --git a/src/nut.c b/src/nut.c index d5ecc98d..17eb1b9a 100644 --- a/src/nut.c +++ b/src/nut.c @@ -80,13 +80,12 @@ static int nut_add_ups (const char *name) DEBUG ("nut plugin: nut_add_ups (name = %s);", name); - ups = (nut_ups_t *) malloc (sizeof (nut_ups_t)); + ups = calloc (1, sizeof (*ups)); if (ups == NULL) { - ERROR ("nut plugin: nut_add_ups: malloc failed."); + ERROR ("nut plugin: nut_add_ups: calloc failed."); return (1); } - memset (ups, '\0', sizeof (nut_ups_t)); status = upscli_splitname (name, &ups->upsname, &ups->hostname, &ups->port); @@ -152,7 +151,7 @@ static int nut_read_one (nut_ups_t *ups) /* (Re-)Connect if we have no connection */ if (ups->conn == NULL) { - ups->conn = (collectd_upsconn_t *) malloc (sizeof (collectd_upsconn_t)); + ups->conn = malloc (sizeof (*ups->conn)); if (ups->conn == NULL) { ERROR ("nut plugin: malloc failed."); diff --git a/src/onewire.c b/src/onewire.c index 3c441ecb..42bd1eb8 100644 --- a/src/onewire.c +++ b/src/onewire.c @@ -176,11 +176,11 @@ static int direct_list_insert(const char * config) { regmatch_t pmatch[3]; size_t nmatch = 3; - direct_access_element_t *element = NULL; + direct_access_element_t *element; DEBUG ("onewire plugin: direct_list_insert <%s>", config); - element = (direct_access_element_t *) malloc (sizeof(*element)); + element = malloc (sizeof (*element)); if (element == NULL) { ERROR ("onewire plugin: direct_list_insert - cannot allocate element"); diff --git a/src/openldap.c b/src/openldap.c index 0a86d521..055f2d16 100644 --- a/src/openldap.c +++ b/src/openldap.c @@ -553,13 +553,12 @@ static int cldap_config_add (oconfig_item_t *ci) /* {{{ */ int i; int status; - st = malloc (sizeof (*st)); + st = calloc (1, sizeof (*st)); if (st == NULL) { - ERROR ("openldap plugin: malloc failed."); + ERROR ("openldap plugin: calloc failed."); return (-1); } - memset (st, 0, sizeof (*st)); status = cf_util_get_string (ci, &st->name); if (status != 0) diff --git a/src/openvpn.c b/src/openvpn.c index 8bccce38..ec76a7aa 100644 --- a/src/openvpn.c +++ b/src/openvpn.c @@ -699,7 +699,7 @@ static int openvpn_config (const char *key, const char *value) } /* create a new vpn element since file, version and name are ok */ - temp = (vpn_status_t *) malloc (sizeof (vpn_status_t)); + temp = malloc (sizeof (*temp)); if (temp == NULL) { char errbuf[1024]; diff --git a/src/oracle.c b/src/oracle.c index ab0812b7..3e4653d5 100644 --- a/src/oracle.c +++ b/src/oracle.c @@ -198,13 +198,12 @@ static int o_config_add_database (oconfig_item_t *ci) /* {{{ */ return (-1); } - db = (o_database_t *) malloc (sizeof (*db)); + db = calloc (1, sizeof (*db)); if (db == NULL) { - ERROR ("oracle plugin: malloc failed."); + ERROR ("oracle plugin: calloc failed."); return (-1); } - memset (db, 0, sizeof (*db)); db->name = NULL; db->host = NULL; db->connect_id = NULL; @@ -273,7 +272,7 @@ static int o_config_add_database (oconfig_item_t *ci) /* {{{ */ if (db->q_prep_areas == NULL) { - WARNING ("oracle plugin: malloc failed"); + WARNING ("oracle plugin: calloc failed"); status = -1; break; } @@ -502,13 +501,12 @@ static int o_read_database_query (o_database_t *db, /* {{{ */ #define ALLOC_OR_FAIL(ptr, ptr_size) \ do { \ size_t alloc_size = (size_t) ((ptr_size)); \ - (ptr) = malloc (alloc_size); \ + (ptr) = calloc (1, alloc_size); \ if ((ptr) == NULL) { \ FREE_ALL; \ - ERROR ("oracle plugin: o_read_database_query: malloc failed."); \ + ERROR ("oracle plugin: o_read_database_query: calloc failed."); \ return (-1); \ } \ - memset ((ptr), 0, alloc_size); \ } while (0) /* Initialize everything to NULL so the above works. */ diff --git a/src/perl.c b/src/perl.c index c25bfe89..920e7b3f 100644 --- a/src/perl.c +++ b/src/perl.c @@ -437,7 +437,7 @@ static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds) return -1; } - ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t)); + ds->ds = smalloc ((len + 1) * sizeof (*ds->ds)); ds->ds_num = len + 1; for (i = 0; i <= len; ++i) { @@ -501,7 +501,7 @@ static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta) hash = (HV *)SvRV (*tmp); - *m = (notification_meta_t *)smalloc (sizeof (**m)); + *m = smalloc (sizeof (**m)); if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) { log_warn ("av2notification_meta: Skipping invalid " @@ -1218,7 +1218,7 @@ static c_ithread_t *c_ithread_create (PerlInterpreter *base) assert (NULL != perl_threads); - t = (c_ithread_t *)smalloc (sizeof (c_ithread_t)); + t = smalloc (sizeof (*t)); memset (t, 0, sizeof (c_ithread_t)); t->interp = (NULL == base) @@ -1428,7 +1428,7 @@ static int fc_create (int type, const oconfig_item_t *ci, void **user_data) return -1; } - data = (pfc_user_data_t *)smalloc (sizeof (*data)); + data = smalloc (sizeof (*data)); data->name = sstrdup (ci->values[0].value.string); data->user_data = newSV (0); @@ -2252,7 +2252,7 @@ static int init_pi (int argc, char **argv) #endif PERL_SYS_INIT3 (&argc, &argv, &environ); - perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t)); + perl_threads = smalloc (sizeof (*perl_threads)); memset (perl_threads, 0, sizeof (c_ithread_list_t)); pthread_mutex_init (&perl_threads->mutex, NULL); @@ -2394,7 +2394,7 @@ static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci) perl_argv[perl_argc - 1] = "-d"; } else { - perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4); + perl_argv[perl_argc - 1] = smalloc (strlen (value) + 4); sstrncpy (perl_argv[perl_argc - 1], "-d:", 4); sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1); } @@ -2427,7 +2427,7 @@ static int perl_config_includedir (pTHX_ oconfig_item_t *ci) exit (3); } - perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3); + perl_argv[perl_argc - 1] = smalloc (strlen (value) + 3); sstrncpy(perl_argv[perl_argc - 1], "-I", 3); sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1); @@ -2545,7 +2545,7 @@ static int perl_config (oconfig_item_t *ci) void module_register (void) { perl_argc = 4; - perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *)); + perl_argv = smalloc ((perl_argc + 1) * sizeof (*perl_argv)); /* default options for the Perl interpreter */ perl_argv[0] = ""; diff --git a/src/pinba.c b/src/pinba.c index d13d047b..f6ddcdaf 100644 --- a/src/pinba.c +++ b/src/pinba.c @@ -381,14 +381,13 @@ static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */ } assert (ai_list != NULL); - s = malloc (sizeof (*s)); + s = calloc (1, sizeof (*s)); if (s == NULL) { freeaddrinfo (ai_list); - ERROR ("pinba plugin: malloc failed."); + ERROR ("pinba plugin: calloc failed."); return (NULL); } - memset (s, 0, sizeof (*s)); for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { diff --git a/src/ping.c b/src/ping.c index da63d3a5..b6b2b688 100644 --- a/src/ping.c +++ b/src/ping.c @@ -483,7 +483,7 @@ static int ping_config (const char *key, const char *value) /* {{{ */ hostlist_t *hl; char *host; - hl = (hostlist_t *) malloc (sizeof (hostlist_t)); + hl = malloc (sizeof (*hl)); if (hl == NULL) { char errbuf[1024]; diff --git a/src/postgresql.c b/src/postgresql.c index 79226830..6cf56318 100644 --- a/src/postgresql.c +++ b/src/postgresql.c @@ -218,7 +218,7 @@ static c_psql_database_t *c_psql_database_new (const char *name) c_psql_database_t **tmp; c_psql_database_t *db; - db = (c_psql_database_t *)malloc (sizeof(*db)); + db = malloc (sizeof(*db)); if (NULL == db) { log_err ("Out of memory."); return NULL; @@ -1042,12 +1042,11 @@ static int config_query_param_add (udb_query_t *q, oconfig_item_t *ci) data = udb_query_get_user_data (q); if (NULL == data) { - data = malloc (sizeof (*data)); + data = calloc (1, sizeof (*data)); if (NULL == data) { log_err ("Out of memory."); return -1; } - memset (data, 0, sizeof (*data)); data->params = NULL; data->params_num = 0; diff --git a/src/powerdns.c b/src/powerdns.c index ab62dcfd..1c41ff8d 100644 --- a/src/powerdns.c +++ b/src/powerdns.c @@ -465,7 +465,7 @@ static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */ return (-1); assert (buffer_size > 0); - buffer = (char *) malloc (buffer_size); + buffer = malloc (buffer_size); if (buffer == NULL) { FUNC_ERROR ("malloc"); @@ -840,13 +840,12 @@ static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */ return (-1); } - item = (list_item_t *) malloc (sizeof (list_item_t)); + item = calloc (1, sizeof (*item)); if (item == NULL) { - ERROR ("powerdns plugin: malloc failed."); + ERROR ("powerdns plugin: calloc failed."); return (-1); } - memset (item, '\0', sizeof (list_item_t)); item->instance = strdup (ci->values[0].value.string); if (item->instance == NULL) diff --git a/src/processes.c b/src/processes.c index d0d73615..d759cb7f 100644 --- a/src/processes.c +++ b/src/processes.c @@ -262,20 +262,19 @@ static void ps_list_register (const char *name, const char *regexp) procstat_t *ptr; int status; - new = (procstat_t *) malloc (sizeof (procstat_t)); + new = calloc (1, sizeof (*new)); if (new == NULL) { - ERROR ("processes plugin: ps_list_register: malloc failed."); + ERROR ("processes plugin: ps_list_register: calloc failed."); return; } - memset (new, 0, sizeof (procstat_t)); sstrncpy (new->name, name, sizeof (new->name)); #if HAVE_REGEX_H if (regexp != NULL) { DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name); - new->re = (regex_t *) malloc (sizeof (regex_t)); + new->re = malloc (sizeof (*new->re)); if (new->re == NULL) { ERROR ("processes plugin: ps_list_register: malloc failed."); @@ -410,10 +409,9 @@ static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t { procstat_entry_t *new; - new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t)); + new = calloc (1, sizeof (*new)); if (new == NULL) return; - memset (new, 0, sizeof (procstat_entry_t)); new->id = entry->id; if (pse == NULL) @@ -1348,18 +1346,15 @@ static int ps_read_process(long pid, procstat_t *ps, char *state) snprintf(f_usage, sizeof (f_usage), "/proc/%li/usage", pid); - buffer = malloc(sizeof (pstatus_t)); - memset(buffer, 0, sizeof (pstatus_t)); + buffer = calloc(1, sizeof (pstatus_t)); read_file_contents(filename, buffer, sizeof (pstatus_t)); myStatus = (pstatus_t *) buffer; - buffer = malloc(sizeof (psinfo_t)); - memset(buffer, 0, sizeof(psinfo_t)); + buffer = calloc(1, sizeof (psinfo_t)); read_file_contents(f_psinfo, buffer, sizeof (psinfo_t)); myInfo = (psinfo_t *) buffer; - buffer = malloc(sizeof (prusage_t)); - memset(buffer, 0, sizeof(prusage_t)); + buffer = calloc(1, sizeof (prusage_t)); read_file_contents(f_usage, buffer, sizeof (prusage_t)); myUsage = (prusage_t *) buffer; diff --git a/src/python.c b/src/python.c index b9090249..1f808496 100644 --- a/src/python.c +++ b/src/python.c @@ -552,10 +552,9 @@ static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args Py_INCREF(callback); Py_XINCREF(data); - c = malloc(sizeof(*c)); + c = calloc(1, sizeof(*c)); if (c == NULL) return NULL; - memset (c, 0, sizeof (*c)); c->name = strdup(buf); c->callback = callback; @@ -644,10 +643,9 @@ static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObjec Py_INCREF(callback); Py_XINCREF(data); - c = malloc(sizeof(*c)); + c = calloc(1, sizeof(*c)); if (c == NULL) return NULL; - memset (c, 0, sizeof (*c)); c->name = strdup(buf); c->callback = callback; @@ -683,10 +681,9 @@ static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwd Py_INCREF(callback); Py_XINCREF(data); - c = malloc(sizeof(*c)); + c = calloc(1, sizeof(*c)); if (c == NULL) return NULL; - memset (c, 0, sizeof (*c)); c->name = strdup(buf); c->callback = callback; diff --git a/src/redis.c b/src/redis.c index 4e3de56c..919a8312 100644 --- a/src/redis.c +++ b/src/redis.c @@ -130,7 +130,7 @@ static redis_query_t *redis_config_query (oconfig_item_t *ci) /* {{{ */ rq = calloc(1, sizeof(*rq)); if (rq == NULL) { - ERROR("redis plugin: calloca failed adding redis_query."); + ERROR("redis plugin: calloc failed adding redis_query."); return NULL; } status = cf_util_get_string_buffer(ci, rq->query, sizeof(rq->query)); diff --git a/src/routeros.c b/src/routeros.c index 667c2fa5..7ee30248 100644 --- a/src/routeros.c +++ b/src/routeros.c @@ -328,10 +328,9 @@ static int cr_config_router (oconfig_item_t *ci) /* {{{ */ int status; int i; - router_data = malloc (sizeof (*router_data)); + router_data = calloc (1, sizeof (*router_data)); if (router_data == NULL) return (-1); - memset (router_data, 0, sizeof (*router_data)); router_data->connection = NULL; router_data->node = NULL; router_data->service = NULL; diff --git a/src/rrdtool.c b/src/rrdtool.c index fd5fb563..6b44f67e 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -166,7 +166,7 @@ static int srrd_update (char *filename, char *template, assert (template == NULL); new_argc = 2 + argc; - new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *)); + new_argv = malloc ((new_argc + 1) * sizeof (*new_argv)); if (new_argv == NULL) { ERROR ("rrdtool plugin: malloc failed."); @@ -480,7 +480,7 @@ static int rrd_queue_enqueue (const char *filename, { rrd_queue_t *queue_entry; - queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t)); + queue_entry = malloc (sizeof (*queue_entry)); if (queue_entry == NULL) return (-1); diff --git a/src/sensors.c b/src/sensors.c index c678dec9..6125d0bc 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -378,13 +378,12 @@ static int sensors_load_conf (void) continue; } - fl = (featurelist_t *) malloc (sizeof (featurelist_t)); + fl = calloc (1, sizeof (*fl)); if (fl == NULL) { - ERROR ("sensors plugin: malloc failed."); + ERROR ("sensors plugin: calloc failed."); continue; } - memset (fl, '\0', sizeof (featurelist_t)); fl->chip = chip; fl->data = feature; @@ -435,13 +434,12 @@ static int sensors_load_conf (void) && (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT)) continue; - fl = (featurelist_t *) malloc (sizeof (featurelist_t)); + fl = calloc (1, sizeof (*fl)); if (fl == NULL) { - ERROR ("sensors plugin: malloc failed."); + ERROR ("sensors plugin: calloc failed."); continue; } - memset (fl, '\0', sizeof (featurelist_t)); fl->chip = chip; fl->feature = feature; diff --git a/src/sigrok.c b/src/sigrok.c index ad5c70c0..6c781f81 100644 --- a/src/sigrok.c +++ b/src/sigrok.c @@ -72,11 +72,10 @@ static int sigrok_config_device(oconfig_item_t *ci) struct config_device *cfdev; int i; - if (!(cfdev = malloc(sizeof(struct config_device)))) { - ERROR("sigrok plugin: malloc() failed."); + if (!(cfdev = calloc(1, sizeof(*cfdev)))) { + ERROR("sigrok plugin: calloc failed."); return -1; } - memset(cfdev, 0, sizeof(*cfdev)); if (cf_util_get_string(ci, &cfdev->name)) { free(cfdev); WARNING("sigrok plugin: Invalid device name."); @@ -236,14 +235,14 @@ static int sigrok_init_driver(struct config_device *cfdev, drvopts = NULL; if (cfdev->conn) { - if (!(src = malloc(sizeof(struct sr_config)))) + if (!(src = malloc(sizeof(*src)))) return -1; src->key = SR_CONF_CONN; src->data = g_variant_new_string(cfdev->conn); drvopts = g_slist_append(drvopts, src); } if (cfdev->serialcomm) { - if (!(src = malloc(sizeof(struct sr_config)))) + if (!(src = malloc(sizeof(*src)))) return -1; src->key = SR_CONF_SERIALCOMM; src->data = g_variant_new_string(cfdev->serialcomm); diff --git a/src/snmp.c b/src/snmp.c index 3ccf60c3..8b21907a 100644 --- a/src/snmp.c +++ b/src/snmp.c @@ -310,7 +310,7 @@ static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t * sfree (dd->values); dd->values_len = 0; - dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num); + dd->values = malloc (sizeof (*dd->values) * ci->values_num); if (dd->values == NULL) return (-1); dd->values_len = (size_t) ci->values_num; @@ -384,10 +384,9 @@ static int csnmp_config_add_data (oconfig_item_t *ci) int status = 0; int i; - dd = (data_definition_t *) malloc (sizeof (data_definition_t)); + dd = calloc (1, sizeof (*dd)); if (dd == NULL) return (-1); - memset (dd, '\0', sizeof (data_definition_t)); status = cf_util_get_string(ci, &dd->name); if (status != 0) @@ -646,10 +645,9 @@ static int csnmp_config_add_host (oconfig_item_t *ci) char cb_name[DATA_MAX_NAME_LEN]; user_data_t cb_data; - hd = (host_definition_t *) malloc (sizeof (host_definition_t)); + hd = calloc (1, sizeof (*hd)); if (hd == NULL) return (-1); - memset (hd, '\0', sizeof (host_definition_t)); hd->version = 2; C_COMPLAIN_INIT (&hd->complaint); @@ -1162,13 +1160,12 @@ static int csnmp_instance_list_add (csnmp_list_instances_t **head, csnmp_oid_init (&vb_name, vb->name, vb->name_length); - il = malloc (sizeof (*il)); + il = calloc (1, sizeof (*il)); if (il == NULL) { - ERROR ("snmp plugin: malloc failed."); + ERROR ("snmp plugin: calloc failed."); return (-1); } - memset (il, 0, sizeof (*il)); il->next = NULL; status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid); @@ -1607,14 +1604,13 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data) continue; } - vt = malloc (sizeof (*vt)); + vt = calloc (1, sizeof (*vt)); if (vt == NULL) { - ERROR ("snmp plugin: malloc failed."); + ERROR ("snmp plugin: calloc failed."); status = -1; break; } - memset (vt, 0, sizeof (*vt)); vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type, data->scale, data->shift, host->name, data->name); @@ -1710,7 +1706,7 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data) } vl.values_len = ds->ds_num; - vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len); + vl.values = malloc (sizeof (*vl.values) * vl.values_len); if (vl.values == NULL) return (-1); for (i = 0; i < vl.values_len; i++) diff --git a/src/statsd.c b/src/statsd.c index d0a677e7..5b7a6f18 100644 --- a/src/statsd.c +++ b/src/statsd.c @@ -127,14 +127,13 @@ static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name, /* {{{ */ return (NULL); } - metric = malloc (sizeof (*metric)); + metric = calloc (1, sizeof (*metric)); if (metric == NULL) { - ERROR ("statsd plugin: malloc failed."); + ERROR ("statsd plugin: calloc failed."); sfree (key_copy); return (NULL); } - memset (metric, 0, sizeof (*metric)); metric->type = type; metric->latency = NULL; diff --git a/src/tail_csv.c b/src/tail_csv.c index bb9b58ac..2d794fc7 100644 --- a/src/tail_csv.c +++ b/src/tail_csv.c @@ -290,10 +290,9 @@ static int tcsv_config_add_metric(oconfig_item_t *ci){ int status; int i; - md = (metric_definition_t *)malloc(sizeof(*md)); + md = calloc(1, sizeof(*md)); if (md == NULL) return (-1); - memset(md, 0, sizeof(*md)); md->name = NULL; md->type = NULL; md->instance = NULL; @@ -426,10 +425,9 @@ static int tcsv_config_add_file(oconfig_item_t *ci) char cb_name[DATA_MAX_NAME_LEN]; user_data_t cb_data; - id = malloc(sizeof(*id)); + id = calloc(1, sizeof(*id)); if (id == NULL) return (-1); - memset(id, 0, sizeof(*id)); id->instance = NULL; id->path = NULL; id->metric_list = NULL; diff --git a/src/target_notification.c b/src/target_notification.c index 14fb5418..55b72ac1 100644 --- a/src/target_notification.c +++ b/src/target_notification.c @@ -126,13 +126,12 @@ static int tn_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ int status; int i; - data = (tn_data_t *) malloc (sizeof (*data)); + data = calloc (1, sizeof (*data)); if (data == NULL) { - ERROR ("tn_create: malloc failed."); + ERROR ("tn_create: calloc failed."); return (-ENOMEM); } - memset (data, 0, sizeof (*data)); data->message = NULL; data->severity = 0; diff --git a/src/target_replace.c b/src/target_replace.c index bd8f9e5c..545fa353 100644 --- a/src/target_replace.c +++ b/src/target_replace.c @@ -61,7 +61,7 @@ static char *tr_strdup (const char *orig) /* {{{ */ return (NULL); sz = strlen (orig) + 1; - dest = (char *) malloc (sz); + dest = malloc (sz); if (dest == NULL) return (NULL); @@ -102,13 +102,12 @@ static int tr_config_add_action (tr_action_t **dest, /* {{{ */ return (-1); } - act = (tr_action_t *) malloc (sizeof (*act)); + act = calloc (1, sizeof (*act)); if (act == NULL) { - ERROR ("tr_config_add_action: malloc failed."); + ERROR ("tr_config_add_action: calloc failed."); return (-ENOMEM); } - memset (act, 0, sizeof (*act)); act->replacement = NULL; act->may_be_empty = may_be_empty; @@ -244,13 +243,12 @@ static int tr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ int status; int i; - data = (tr_data_t *) malloc (sizeof (*data)); + data = calloc (1, sizeof (*data)); if (data == NULL) { - ERROR ("tr_create: malloc failed."); + ERROR ("tr_create: calloc failed."); return (-ENOMEM); } - memset (data, 0, sizeof (*data)); data->host = NULL; data->plugin = NULL; diff --git a/src/target_scale.c b/src/target_scale.c index 6169fa05..3ac399c9 100644 --- a/src/target_scale.c +++ b/src/target_scale.c @@ -387,13 +387,12 @@ static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ int status; int i; - data = (ts_data_t *) malloc (sizeof (*data)); + data = calloc (1, sizeof (*data)); if (data == NULL) { - ERROR ("ts_create: malloc failed."); + ERROR ("ts_create: calloc failed."); return (-ENOMEM); } - memset (data, 0, sizeof (*data)); data->factor = NAN; data->offset = NAN; diff --git a/src/target_set.c b/src/target_set.c index daeaf8be..faad0bf5 100644 --- a/src/target_set.c +++ b/src/target_set.c @@ -87,13 +87,12 @@ static int ts_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ int status; int i; - data = (ts_data_t *) malloc (sizeof (*data)); + data = calloc (1, sizeof (*data)); if (data == NULL) { - ERROR ("ts_create: malloc failed."); + ERROR ("ts_create: calloc failed."); return (-ENOMEM); } - memset (data, 0, sizeof (*data)); data->host = NULL; data->plugin = NULL; diff --git a/src/tcpconns.c b/src/tcpconns.c index 5b47ff53..6bc33842 100644 --- a/src/tcpconns.c +++ b/src/tcpconns.c @@ -385,10 +385,9 @@ static port_entry_t *conn_get_port_entry (uint16_t port, int create) if ((ret == NULL) && (create != 0)) { - ret = (port_entry_t *) malloc (sizeof (port_entry_t)); + ret = calloc (1, sizeof (*ret)); if (ret == NULL) return (NULL); - memset (ret, '\0', sizeof (port_entry_t)); ret->port = port; ret->next = port_list_head; @@ -832,7 +831,7 @@ static int conn_read (void) return (-1); } - buffer = (char *) malloc (buffer_len); + buffer = malloc (buffer_len); if (buffer == NULL) { ERROR ("tcpconns plugin: malloc failed."); diff --git a/src/teamspeak2.c b/src/teamspeak2.c index 058eb7c3..263863b9 100644 --- a/src/teamspeak2.c +++ b/src/teamspeak2.c @@ -83,13 +83,12 @@ static int tss2_add_vserver (int vserver_port) } /* Allocate memory */ - entry = (vserver_list_t *) malloc (sizeof (vserver_list_t)); + entry = calloc (1, sizeof (*entry)); if (entry == NULL) { - ERROR ("teamspeak2 plugin: malloc failed."); + ERROR ("teamspeak2 plugin: calloc failed."); return (-1); } - memset (entry, 0, sizeof (vserver_list_t)); /* Save data */ entry->port = vserver_port; diff --git a/src/threshold.c b/src/threshold.c index ed831299..624ab7a9 100644 --- a/src/threshold.c +++ b/src/threshold.c @@ -70,7 +70,7 @@ static int ut_threshold_add (const threshold_t *th) return (-1); } - th_copy = (threshold_t *) malloc (sizeof (threshold_t)); + th_copy = malloc (sizeof (*th_copy)); if (th_copy == NULL) { sfree (name_copy); diff --git a/src/unixsock.c b/src/unixsock.c index 795f7abe..1840e34c 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -368,7 +368,7 @@ static void *us_server_thread (void __attribute__((unused)) *arg) pthread_exit ((void *) 1); } - remote_fd = (int *) malloc (sizeof (int)); + remote_fd = malloc (sizeof (*remote_fd)); if (remote_fd == NULL) { char errbuf[1024]; diff --git a/src/utils_cmd_putval.c b/src/utils_cmd_putval.c index 43244f61..69af2e4a 100644 --- a/src/utils_cmd_putval.c +++ b/src/utils_cmd_putval.c @@ -163,7 +163,7 @@ int handle_putval (FILE *fh, char *buffer) sfree (identifier_copy); vl.values_len = ds->ds_num; - vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t)); + vl.values = malloc (vl.values_len * sizeof (*vl.values)); if (vl.values == NULL) { print_to_socket (fh, "-1 malloc failed.\n"); diff --git a/src/utils_db_query.c b/src/utils_db_query.c index c7be9f0d..74c3a81f 100644 --- a/src/utils_db_query.c +++ b/src/utils_db_query.c @@ -207,7 +207,7 @@ static int udb_result_submit (udb_result_t *r, /* {{{ */ vl.values = (value_t *) calloc (r->values_num, sizeof (value_t)); if (vl.values == NULL) { - ERROR ("db query utils: malloc failed."); + ERROR ("db query utils: calloc failed."); return (-1); } vl.values_len = r_area->ds->ds_num; @@ -390,7 +390,7 @@ static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */ = (size_t *) calloc (r->instances_num, sizeof (size_t)); if (prep_area->instances_pos == NULL) { - ERROR ("db query utils: udb_result_prepare_result: malloc failed."); + ERROR ("db query utils: udb_result_prepare_result: calloc failed."); BAIL_OUT (-ENOMEM); } @@ -398,7 +398,7 @@ static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */ = (char **) calloc (r->instances_num, sizeof (char *)); if (prep_area->instances_buffer == NULL) { - ERROR ("db query utils: udb_result_prepare_result: malloc failed."); + ERROR ("db query utils: udb_result_prepare_result: calloc failed."); BAIL_OUT (-ENOMEM); } } /* if (r->instances_num > 0) */ @@ -407,7 +407,7 @@ static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */ = (size_t *) calloc (r->values_num, sizeof (size_t)); if (prep_area->values_pos == NULL) { - ERROR ("db query utils: udb_result_prepare_result: malloc failed."); + ERROR ("db query utils: udb_result_prepare_result: calloc failed."); BAIL_OUT (-ENOMEM); } @@ -415,7 +415,7 @@ static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */ = (char **) calloc (r->values_num, sizeof (char *)); if (prep_area->values_buffer == NULL) { - ERROR ("db query utils: udb_result_prepare_result: malloc failed."); + ERROR ("db query utils: udb_result_prepare_result: calloc failed."); BAIL_OUT (-ENOMEM); } @@ -423,7 +423,7 @@ static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */ = (size_t *) calloc (r->metadata_num, sizeof (size_t)); if (prep_area->metadata_pos == NULL) { - ERROR ("db query utils: udb_result_prepare_result: malloc failed."); + ERROR ("db query utils: udb_result_prepare_result: calloc failed."); BAIL_OUT (-ENOMEM); } @@ -431,7 +431,7 @@ static int udb_result_prepare_result (udb_result_t const *r, /* {{{ */ = (char **) calloc (r->metadata_num, sizeof (char *)); if (prep_area->metadata_buffer == NULL) { - ERROR ("db query utils: udb_result_prepare_result: malloc failed."); + ERROR ("db query utils: udb_result_prepare_result: calloc failed."); BAIL_OUT (-ENOMEM); } @@ -550,13 +550,12 @@ static int udb_result_create (const char *query_name, /* {{{ */ ci->values_num, (ci->values_num == 1) ? "" : "s"); } - r = (udb_result_t *) malloc (sizeof (*r)); + r = calloc (1, sizeof (*r)); if (r == NULL) { - ERROR ("db query utils: malloc failed."); + ERROR ("db query utils: calloc failed."); return (-1); } - memset (r, 0, sizeof (*r)); r->type = NULL; r->instance_prefix = NULL; r->instances = NULL; @@ -678,13 +677,12 @@ int udb_query_create (udb_query_t ***ret_query_list, /* {{{ */ return (-1); } - q = (udb_query_t *) malloc (sizeof (*q)); + q = calloc (1, sizeof (*q)); if (q == NULL) { - ERROR ("db query utils: malloc failed."); + ERROR ("db query utils: calloc failed."); return (-1); } - memset (q, 0, sizeof (*q)); q->min_version = 0; q->max_version = UINT_MAX; @@ -1072,17 +1070,16 @@ udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */ udb_result_preparation_area_t **next_r_area; udb_result_t *r; - q_area = malloc (sizeof (*q_area)); + q_area = calloc (1, sizeof (*q_area)); if (q_area == NULL) return NULL; - memset (q_area, 0, sizeof (*q_area)); next_r_area = &q_area->result_prep_areas; for (r = q->results; r != NULL; r = r->next) { udb_result_preparation_area_t *r_area; - r_area = malloc (sizeof (*r_area)); + r_area = calloc (1, sizeof (*r_area)); if (r_area == NULL) { udb_result_preparation_area_t *a = q_area->result_prep_areas; @@ -1098,8 +1095,6 @@ udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */ return NULL; } - memset (r_area, 0, sizeof (*r_area)); - *next_r_area = r_area; next_r_area = &r_area->next; } diff --git a/src/utils_dns.c b/src/utils_dns.c index 2c7a59d3..991d346a 100644 --- a/src/utils_dns.c +++ b/src/utils_dns.c @@ -209,7 +209,7 @@ static void ignore_list_add (const struct in6_addr *addr) if (ignore_list_match (addr) != 0) return; - new = malloc (sizeof (ip_list_t)); + new = malloc (sizeof (*new)); if (new == NULL) { perror ("malloc"); diff --git a/src/utils_fbhash.c b/src/utils_fbhash.c index cbd15061..f27c0827 100644 --- a/src/utils_fbhash.c +++ b/src/utils_fbhash.c @@ -209,10 +209,9 @@ fbhash_t *fbh_create (const char *file) /* {{{ */ if (file == NULL) return (NULL); - h = malloc (sizeof (*h)); + h = calloc (1, sizeof (*h)); if (h == NULL) return (NULL); - memset (h, 0, sizeof (*h)); h->filename = strdup (file); if (h->filename == NULL) diff --git a/src/utils_latency.c b/src/utils_latency.c index ca7d5fab..41af00cb 100644 --- a/src/utils_latency.c +++ b/src/utils_latency.c @@ -121,10 +121,9 @@ latency_counter_t *latency_counter_create (void) /* {{{ */ { latency_counter_t *lc; - lc = malloc (sizeof (*lc)); + lc = calloc (1, sizeof (*lc)); if (lc == NULL) return (NULL); - memset (lc, 0, sizeof (*lc)); latency_counter_reset (lc); lc->bin_width = HISTOGRAM_DEFAULT_BIN_WIDTH; diff --git a/src/utils_mount.c b/src/utils_mount.c index 752d2e15..a36882c4 100644 --- a/src/utils_mount.c +++ b/src/utils_mount.c @@ -467,10 +467,8 @@ static cu_mount_t *cu_mount_getfsstat (void) return (NULL); } - if ((buf = (STRUCT_STATFS *) malloc (bufsize * sizeof (STRUCT_STATFS))) - == NULL) + if ((buf = calloc (bufsize, sizeof (*buf))) == NULL) return (NULL); - memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS)); /* The bufsize needs to be passed in bytes. Really. This is not in the * manpage.. -octo */ @@ -487,10 +485,9 @@ static cu_mount_t *cu_mount_getfsstat (void) for (i = 0; i < num; i++) { - if ((new = malloc (sizeof (cu_mount_t))) == NULL) + if ((new = calloc (1, sizeof (*new))) == NULL) break; - memset (new, '\0', sizeof (cu_mount_t)); - + /* Copy values from `struct mnttab' */ new->dir = sstrdup (buf[i].f_mntonname); new->spec_device = sstrdup (buf[i].f_mntfromname); @@ -541,10 +538,9 @@ static cu_mount_t *cu_mount_gen_getmntent (void) while (getmntent (fp, &mt) == 0) { - if ((new = malloc (sizeof (cu_mount_t))) == NULL) + if ((new = calloc (1, sizeof (*new))) == NULL) break; - memset (new, '\0', sizeof (cu_mount_t)); - + /* Copy values from `struct mnttab' */ new->dir = sstrdup (mt.mnt_mountp); new->spec_device = sstrdup (mt.mnt_special); @@ -599,9 +595,8 @@ static cu_mount_t *cu_mount_getmntent (void) while (getmntent_r (fp, &me, mntbuf, sizeof (mntbuf) )) { - if ((new = malloc (sizeof (cu_mount_t))) == NULL) + if ((new = calloc (1, sizeof (*new))) == NULL) break; - memset (new, '\0', sizeof (cu_mount_t)); /* Copy values from `struct mntent *' */ new->dir = sstrdup (me.mnt_dir); @@ -656,10 +651,9 @@ static cu_mount_t *cu_mount_getmntent (void) while ((me = getmntent (fp)) != NULL) { - if ((new = malloc (sizeof (cu_mount_t))) == NULL) + if ((new = calloc (1, sizeof (*new))) == NULL) break; - memset (new, '\0', sizeof (cu_mount_t)); - + /* Copy values from `struct mntent *' */ new->dir = sstrdup (me->mnt_dir); new->spec_device = sstrdup (me->mnt_fsname); @@ -820,7 +814,7 @@ cu_mount_getoptionvalue(char *line, const char *keyword) if((p-r) == 1) { return NULL; } - m = (char *)smalloc(p-r+1); + m = smalloc(p-r+1); sstrncpy(m, r, p-r+1); return m; } diff --git a/src/utils_rrdcreate.c b/src/utils_rrdcreate.c index 06e4f21b..fb04201c 100644 --- a/src/utils_rrdcreate.c +++ b/src/utils_rrdcreate.c @@ -113,13 +113,12 @@ static srrd_create_args_t *srrd_create_args_create (const char *filename, { srrd_create_args_t *args; - args = malloc (sizeof (*args)); + args = calloc (1, sizeof (*args)); if (args == NULL) { - ERROR ("srrd_create_args_create: malloc failed."); + ERROR ("srrd_create_args_create: calloc failed."); return (NULL); } - memset (args, 0, sizeof (*args)); args->filename = NULL; args->pdp_step = pdp_step; args->last_up = last_up; @@ -216,9 +215,8 @@ static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */ rra_max = rts_num * rra_types_num; assert (rra_max > 0); - if ((rra_def = malloc ((rra_max + 1) * sizeof (*rra_def))) == NULL) + if ((rra_def = calloc (rra_max + 1, sizeof (*rra_def))) == NULL) return (-1); - memset (rra_def, 0, (rra_max + 1) * sizeof (*rra_def)); rra_num = 0; cdp_len = 0; @@ -292,15 +290,14 @@ static int ds_get (char ***ret, /* {{{ */ assert (ds->ds_num > 0); - ds_def = malloc (ds->ds_num * sizeof (*ds_def)); + ds_def = calloc (ds->ds_num, sizeof (*ds_def)); if (ds_def == NULL) { char errbuf[1024]; - ERROR ("rrdtool plugin: malloc failed: %s", + ERROR ("rrdtool plugin: calloc failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } - memset (ds_def, 0, ds->ds_num * sizeof (*ds_def)); for (ds_num = 0; ds_num < ds->ds_num; ds_num++) { @@ -421,7 +418,7 @@ static int srrd_create (const char *filename, /* {{{ */ char last_up_str[16]; new_argc = 6 + argc; - new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *)); + new_argv = malloc ((new_argc + 1) * sizeof (*new_argv)); if (new_argv == NULL) { ERROR ("rrdtool plugin: malloc failed."); @@ -689,7 +686,7 @@ int cu_rrd_create_file (const char *filename, /* {{{ */ argc = ds_num + rra_num; - if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL) + if ((argv = malloc (sizeof (*argv) * (argc + 1))) == NULL) { char errbuf[1024]; ERROR ("cu_rrd_create_file failed: %s", diff --git a/src/utils_vl_lookup.c b/src/utils_vl_lookup.c index f85910e1..d3eeab3c 100644 --- a/src/utils_vl_lookup.c +++ b/src/utils_vl_lookup.c @@ -204,13 +204,12 @@ static void *lu_create_user_obj (lookup_t *obj, /* {{{ */ { user_obj_t *user_obj; - user_obj = malloc (sizeof (*user_obj)); + user_obj = calloc (1, sizeof (*user_obj)); if (user_obj == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); return (NULL); } - memset (user_obj, 0, sizeof (*user_obj)); user_obj->next = NULL; user_obj->user_obj = obj->cb_user_class (ds, vl, user_class->user_class); @@ -373,16 +372,15 @@ static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */ return (NULL); } - by_type = malloc (sizeof (*by_type)); + by_type = calloc (1, sizeof (*by_type)); if (by_type == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); sfree (type_copy); return (NULL); } - memset (by_type, 0, sizeof (*by_type)); by_type->wildcard_plugin_list = NULL; - + by_type->by_plugin_tree = c_avl_create ((void *) strcmp); if (by_type->by_plugin_tree == NULL) { @@ -544,13 +542,12 @@ lookup_t *lookup_create (lookup_class_callback_t cb_user_class, /* {{{ */ lookup_free_class_callback_t cb_free_class, lookup_free_obj_callback_t cb_free_obj) { - lookup_t *obj = malloc (sizeof (*obj)); + lookup_t *obj = calloc (1, sizeof (*obj)); if (obj == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); return (NULL); } - memset (obj, 0, sizeof (*obj)); obj->by_type_tree = c_avl_create ((void *) strcmp); if (obj->by_type_tree == NULL) @@ -605,13 +602,12 @@ int lookup_add (lookup_t *obj, /* {{{ */ if (by_type == NULL) return (-1); - user_class_obj = malloc (sizeof (*user_class_obj)); + user_class_obj = calloc (1, sizeof (*user_class_obj)); if (user_class_obj == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); return (ENOMEM); } - memset (user_class_obj, 0, sizeof (*user_class_obj)); pthread_mutex_init (&user_class_obj->entry.lock, /* attr = */ NULL); user_class_obj->entry.user_class = user_class; lu_copy_ident_to_match (&user_class_obj->entry.match, ident, group_by); diff --git a/src/varnish.c b/src/varnish.c index aee72471..d7da95a3 100644 --- a/src/varnish.c +++ b/src/varnish.c @@ -943,10 +943,9 @@ static int varnish_init (void) /* {{{ */ if (have_instance) return (0); - conf = malloc (sizeof (*conf)); + conf = calloc (1, sizeof (*conf)); if (conf == NULL) return (ENOMEM); - memset (conf, 0, sizeof (*conf)); /* Default settings: */ conf->instance = NULL; @@ -972,10 +971,9 @@ static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */ char callback_name[DATA_MAX_NAME_LEN]; int i; - conf = malloc (sizeof (*conf)); + conf = calloc (1, sizeof (*conf)); if (conf == NULL) return (ENOMEM); - memset (conf, 0, sizeof (*conf)); conf->instance = NULL; varnish_config_apply_default (conf); diff --git a/src/virt.c b/src/virt.c index 79d46826..bbf5afc7 100644 --- a/src/virt.c +++ b/src/virt.c @@ -686,7 +686,7 @@ refresh_lists (void) int *domids; /* Get list of domains. */ - domids = malloc (sizeof (int) * n); + domids = malloc (sizeof (*domids) * n); if (domids == 0) { ERROR (PLUGIN_NAME " plugin: malloc failed."); return -1; diff --git a/src/write_graphite.c b/src/write_graphite.c index 6baace14..0fa8bc32 100644 --- a/src/write_graphite.c +++ b/src/write_graphite.c @@ -486,13 +486,12 @@ static int wg_config_node (oconfig_item_t *ci) int i; int status = 0; - cb = malloc (sizeof (*cb)); + cb = calloc (1, sizeof (*cb)); if (cb == NULL) { - ERROR ("write_graphite plugin: malloc failed."); + ERROR ("write_graphite plugin: calloc failed."); return (-1); } - memset (cb, 0, sizeof (*cb)); cb->sock_fd = -1; cb->name = NULL; cb->node = strdup (WG_DEFAULT_NODE); diff --git a/src/write_http.c b/src/write_http.c index ec3a01ce..c817749f 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -183,7 +183,7 @@ static int wh_callback_init (wh_callback_t *cb) /* {{{ */ if (cb->pass != NULL) credentials_size += strlen (cb->pass); - cb->credentials = (char *) malloc (credentials_size); + cb->credentials = malloc (credentials_size); if (cb->credentials == NULL) { ERROR ("curl plugin: malloc failed."); @@ -549,13 +549,12 @@ static int wh_config_node (oconfig_item_t *ci) /* {{{ */ int status = 0; int i; - cb = malloc (sizeof (*cb)); + cb = calloc (1, sizeof (*cb)); if (cb == NULL) { - ERROR ("write_http plugin: malloc failed."); + ERROR ("write_http plugin: calloc failed."); return (-1); } - memset (cb, 0, sizeof (*cb)); cb->verify_peer = 1; cb->verify_host = 1; cb->format = WH_FORMAT_COMMAND; diff --git a/src/write_mongodb.c b/src/write_mongodb.c index 24151ceb..9a6fdf26 100644 --- a/src/write_mongodb.c +++ b/src/write_mongodb.c @@ -274,10 +274,9 @@ static int wm_config_node (oconfig_item_t *ci) /* {{{ */ int status; int i; - node = malloc (sizeof (*node)); + node = calloc (1, sizeof (*node)); if (node == NULL) return (ENOMEM); - memset (node, 0, sizeof (*node)); mongo_init (node->conn); node->host = NULL; node->store_rates = 1; diff --git a/src/write_redis.c b/src/write_redis.c index 4bfcc73d..a79eb36b 100644 --- a/src/write_redis.c +++ b/src/write_redis.c @@ -171,10 +171,9 @@ static int wr_config_node (oconfig_item_t *ci) /* {{{ */ int status; int i; - node = malloc (sizeof (*node)); + node = calloc (1, sizeof (*node)); if (node == NULL) return (ENOMEM); - memset (node, 0, sizeof (*node)); node->host = NULL; node->port = 0; node->timeout.tv_sec = 0; diff --git a/src/write_riemann.c b/src/write_riemann.c index 6a47ba32..8191ae25 100644 --- a/src/write_riemann.c +++ b/src/write_riemann.c @@ -205,12 +205,11 @@ static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ * if (host->use_tcp) buffer_len += 4; - buffer = malloc (buffer_len); + buffer = calloc (1, buffer_len); if (buffer == NULL) { - ERROR ("write_riemann plugin: malloc failed."); + ERROR ("write_riemann plugin: calloc failed."); return ENOMEM; } - memset (buffer, 0, buffer_len); if (host->use_tcp) { @@ -365,13 +364,12 @@ static Msg *riemann_notification_to_protobuf(struct riemann_host *host, /* {{{ * notification_meta_t *meta; size_t i; - msg = malloc (sizeof (*msg)); + msg = calloc (1, sizeof (*msg)); if (msg == NULL) { - ERROR ("write_riemann plugin: malloc failed."); + ERROR ("write_riemann plugin: calloc failed."); return (NULL); } - memset (msg, 0, sizeof (*msg)); msg__init (msg); msg->events = malloc (sizeof (*msg->events)); @@ -382,15 +380,14 @@ static Msg *riemann_notification_to_protobuf(struct riemann_host *host, /* {{{ * return (NULL); } - event = malloc (sizeof (*event)); + event = calloc (1, sizeof (*event)); if (event == NULL) { - ERROR ("write_riemann plugin: malloc failed."); + ERROR ("write_riemann plugin: calloc failed."); sfree (msg->events); sfree (msg); return (NULL); } - memset (event, 0, sizeof (*event)); event__init (event); msg->events[0] = event; @@ -474,13 +471,12 @@ static Event *riemann_value_to_protobuf(struct riemann_host const *host, /* {{{ double ttl; size_t i; - event = malloc (sizeof (*event)); + event = calloc (1, sizeof (*event)); if (event == NULL) { - ERROR ("write_riemann plugin: malloc failed."); + ERROR ("write_riemann plugin: calloc failed."); return (NULL); } - memset (event, 0, sizeof (*event)); event__init (event); event->host = strdup (vl->host); @@ -607,13 +603,12 @@ static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* gauge_t *rates = NULL; /* Initialize the Msg structure. */ - msg = malloc (sizeof (*msg)); + msg = calloc (1, sizeof (*msg)); if (msg == NULL) { - ERROR ("write_riemann plugin: malloc failed."); + ERROR ("write_riemann plugin: calloc failed."); return (NULL); } - memset (msg, 0, sizeof (*msg)); msg__init (msg); /* Set up events. First, the list of pointers. */ diff --git a/src/write_sensu.c b/src/write_sensu.c index aae5d8bb..5e231a58 100644 --- a/src/write_sensu.c +++ b/src/write_sensu.c @@ -524,11 +524,10 @@ static char *replace_str(const char *str, const char *old, /* {{{ */ } else retlen = strlen(str); - ret = malloc(retlen + 1); + ret = calloc(1, retlen + 1); if (ret == NULL) return NULL; // added to original: not optimized, but keeps valgrind happy. - memset(ret, 0, retlen + 1); r = ret; p = str; diff --git a/src/write_tsdb.c b/src/write_tsdb.c index c562596b..0fa62452 100644 --- a/src/write_tsdb.c +++ b/src/write_tsdb.c @@ -579,13 +579,12 @@ static int wt_config_tsd(oconfig_item_t *ci) char callback_name[DATA_MAX_NAME_LEN]; int i; - cb = malloc(sizeof(*cb)); + cb = calloc(1, sizeof(*cb)); if (cb == NULL) { - ERROR("write_tsdb plugin: malloc failed."); + ERROR("write_tsdb plugin: calloc failed."); return -1; } - memset(cb, 0, sizeof(*cb)); cb->sock_fd = -1; cb->node = NULL; cb->service = NULL; diff --git a/src/zone.c b/src/zone.c index 15eae6a2..52dc5539 100644 --- a/src/zone.c +++ b/src/zone.c @@ -115,11 +115,11 @@ zone_find_stats(c_avl_tree_t *tree, zoneid_t zoneid) zoneid_t *key = NULL; if (c_avl_get(tree, (void **)&zoneid, (void **)&ret)) { - if (!(ret = malloc(sizeof(zone_stats_t)))) { + if (!(ret = malloc(sizeof(*ret)))) { WARNING("zone plugin: no memory"); return(NULL); } - if (!(key = malloc(sizeof(zoneid_t)))) { + if (!(key = malloc(sizeof(*key)))) { WARNING("zone plugin: no memory"); return(NULL); }