Browse Source

Apply coccinelle script to replace malloc(a*b)->calloc(a,b)

Nick Mathewson 9 years ago
parent
commit
2bfd92d0d1

+ 2 - 2
src/common/compat.c

@@ -1687,12 +1687,12 @@ log_credential_status(void)
 
   /* log supplementary groups */
   sup_gids_size = 64;
-  sup_gids = tor_malloc(sizeof(gid_t) * 64);
+  sup_gids = tor_calloc(sizeof(gid_t), 64);
   while ((ngids = getgroups(sup_gids_size, sup_gids)) < 0 &&
          errno == EINVAL &&
          sup_gids_size < NGROUPS_MAX) {
     sup_gids_size *= 2;
-    sup_gids = tor_realloc(sup_gids, sizeof(gid_t) * sup_gids_size);
+    sup_gids = tor_reallocarray(sup_gids, sizeof(gid_t), sup_gids_size);
   }
 
   if (ngids < 0) {

+ 3 - 2
src/common/container.c

@@ -34,7 +34,7 @@ smartlist_new(void)
   smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
   sl->num_used = 0;
   sl->capacity = SMARTLIST_DEFAULT_CAPACITY;
-  sl->list = tor_malloc(sizeof(void *) * sl->capacity);
+  sl->list = tor_calloc(sizeof(void *), sl->capacity);
   return sl;
 }
 
@@ -77,7 +77,8 @@ smartlist_ensure_capacity(smartlist_t *sl, int size)
         higher *= 2;
     }
     sl->capacity = higher;
-    sl->list = tor_realloc(sl->list, sizeof(void*)*((size_t)sl->capacity));
+    sl->list = tor_reallocarray(sl->list, sizeof(void *),
+				((size_t)sl->capacity));
   }
 }
 

+ 2 - 2
src/common/container.h

@@ -563,7 +563,7 @@ bitarray_init_zero(unsigned int n_bits)
 {
   /* round up to the next int. */
   size_t sz = (n_bits+BITARRAY_MASK) >> BITARRAY_SHIFT;
-  return tor_malloc_zero(sz*sizeof(unsigned int));
+  return tor_calloc(sz, sizeof(unsigned int));
 }
 /** Expand <b>ba</b> from holding <b>n_bits_old</b> to <b>n_bits_new</b>,
  * clearing all new bits.  Returns a possibly changed pointer to the
@@ -577,7 +577,7 @@ bitarray_expand(bitarray_t *ba,
   char *ptr;
   if (sz_new <= sz_old)
     return ba;
-  ptr = tor_realloc(ba, sz_new*sizeof(unsigned int));
+  ptr = tor_reallocarray(ba, sz_new, sizeof(unsigned int));
   /* This memset does nothing to the older excess bytes.  But they were
    * already set to 0 by bitarry_init_zero. */
   memset(ptr+sz_old*sizeof(unsigned int), 0,

+ 2 - 2
src/common/crypto.c

@@ -1838,7 +1838,7 @@ crypto_store_dynamic_dh_modulus(const char *fname)
     goto done;
   }
 
-  base64_encoded_dh = tor_malloc_zero(len * 2); /* should be enough */
+  base64_encoded_dh = tor_calloc(len, 2); /* should be enough */
   new_len = base64_encode(base64_encoded_dh, len * 2,
                           (char *)dh_string_repr, len);
   if (new_len < 0) {
@@ -3164,7 +3164,7 @@ setup_openssl_threading(void)
   int i;
   int n = CRYPTO_num_locks();
   n_openssl_mutexes_ = n;
-  openssl_mutexes_ = tor_malloc(n*sizeof(tor_mutex_t *));
+  openssl_mutexes_ = tor_calloc(n, sizeof(tor_mutex_t *));
   for (i=0; i < n; ++i)
     openssl_mutexes_[i] = tor_mutex_new();
   CRYPTO_set_locking_callback(openssl_locking_cb_);

+ 3 - 3
src/common/util.c

@@ -3392,8 +3392,8 @@ format_win_cmdline_argument(const char *arg)
     smartlist_add(arg_chars, (void*)&backslash);
 
   /* Allocate space for argument, quotes (if needed), and terminator */
-  formatted_arg = tor_malloc(sizeof(char) *
-      (smartlist_len(arg_chars) + (need_quotes?2:0) + 1));
+  formatted_arg = tor_calloc(sizeof(char),
+                             (smartlist_len(arg_chars) + (need_quotes ? 2 : 0) + 1));
 
   /* Add leading quote */
   i=0;
@@ -5022,7 +5022,7 @@ tor_check_port_forwarding(const char *filename,
        for each smartlist element (one for "-p" and one for the
        ports), and one for the final NULL. */
     args_n = 1 + 2*smartlist_len(ports_to_forward) + 1;
-    argv = tor_malloc_zero(sizeof(char*)*args_n);
+    argv = tor_calloc(sizeof(char *), args_n);
 
     argv[argv_index++] = filename;
     SMARTLIST_FOREACH_BEGIN(ports_to_forward, const char *, port) {

+ 1 - 1
src/or/circuitbuild.c

@@ -1560,7 +1560,7 @@ choose_good_exit_server_general(int need_uptime, int need_capacity)
    * -1 means "Don't use this router at all."
    */
   the_nodes = nodelist_get_list();
-  n_supported = tor_malloc(sizeof(int)*smartlist_len(the_nodes));
+  n_supported = tor_calloc(sizeof(int), smartlist_len(the_nodes));
   SMARTLIST_FOREACH_BEGIN(the_nodes, const node_t *, node) {
     const int i = node_sl_idx;
     if (router_digest_is_me(node->identity)) {

+ 5 - 5
src/or/circuitstats.c

@@ -404,7 +404,7 @@ circuit_build_times_new_consensus_params(circuit_build_times_t *cbt,
          * distress anyway, so memory correctness here is paramount over
          * doing acrobatics to preserve the array.
          */
-        recent_circs = tor_malloc_zero(sizeof(int8_t)*num);
+        recent_circs = tor_calloc(sizeof(int8_t), num);
         if (cbt->liveness.timeouts_after_firsthop &&
             cbt->liveness.num_recent_circs > 0) {
           memcpy(recent_circs, cbt->liveness.timeouts_after_firsthop,
@@ -508,7 +508,7 @@ circuit_build_times_init(circuit_build_times_t *cbt)
     cbt->liveness.num_recent_circs =
       circuit_build_times_recent_circuit_count(NULL);
     cbt->liveness.timeouts_after_firsthop =
-      tor_malloc_zero(sizeof(int8_t)*cbt->liveness.num_recent_circs);
+      tor_calloc(sizeof(int8_t), cbt->liveness.num_recent_circs);
   } else {
     cbt->liveness.num_recent_circs = 0;
     cbt->liveness.timeouts_after_firsthop = NULL;
@@ -649,7 +649,7 @@ circuit_build_times_create_histogram(const circuit_build_times_t *cbt,
   int i, c;
 
   *nbins = 1 + (max_build_time / CBT_BIN_WIDTH);
-  histogram = tor_malloc_zero(*nbins * sizeof(build_time_t));
+  histogram = tor_calloc(*nbins, sizeof(build_time_t));
 
   // calculate histogram
   for (i = 0; i < CBT_NCIRCUITS_TO_OBSERVE; i++) {
@@ -691,7 +691,7 @@ circuit_build_times_get_xm(circuit_build_times_t *cbt)
   if (cbt->total_build_times < CBT_NCIRCUITS_TO_OBSERVE)
     num_modes = 1;
 
-  nth_max_bin = (build_time_t*)tor_malloc_zero(num_modes*sizeof(build_time_t));
+  nth_max_bin = (build_time_t*)tor_calloc(num_modes, sizeof(build_time_t));
 
   /* Determine the N most common build times */
   for (i = 0; i < nbins; i++) {
@@ -873,7 +873,7 @@ circuit_build_times_parse_state(circuit_build_times_t *cbt,
   }
 
   /* build_time_t 0 means uninitialized */
-  loaded_times = tor_malloc_zero(sizeof(build_time_t)*state->TotalBuildTimes);
+  loaded_times = tor_calloc(sizeof(build_time_t), state->TotalBuildTimes);
 
   for (line = state->BuildtimeHistogram; line; line = line->next) {
     smartlist_t *args = smartlist_new();

+ 2 - 2
src/or/config.c

@@ -4847,7 +4847,7 @@ parse_client_transport_line(const or_options_t *options,
     if (!validate_only && !is_useless_proxy) {
       proxy_argc = line_length-2;
       tor_assert(proxy_argc > 0);
-      proxy_argv = tor_malloc_zero(sizeof(char*)*(proxy_argc+1));
+      proxy_argv = tor_calloc(sizeof(char *), (proxy_argc + 1));
       tmp = proxy_argv;
       for (i=0;i<proxy_argc;i++) { /* store arguments */
         *tmp++ = smartlist_get(items, 2);
@@ -5127,7 +5127,7 @@ parse_server_transport_line(const or_options_t *options,
     if (!validate_only) {
       proxy_argc = line_length-2;
       tor_assert(proxy_argc > 0);
-      proxy_argv = tor_malloc_zero(sizeof(char*)*(proxy_argc+1));
+      proxy_argv = tor_calloc(sizeof(char *), (proxy_argc + 1));
       tmp = proxy_argv;
 
       for (i=0;i<proxy_argc;i++) { /* store arguments */

+ 1 - 1
src/or/cpuworker.c

@@ -510,7 +510,7 @@ spawn_cpuworker(void)
   connection_t *conn;
   int err;
 
-  fdarray = tor_malloc(sizeof(tor_socket_t)*2);
+  fdarray = tor_calloc(sizeof(tor_socket_t), 2);
   if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
     log_warn(LD_NET, "Couldn't construct socketpair for cpuworker: %s",
              tor_socket_strerror(-err));

+ 6 - 6
src/or/dirserv.c

@@ -1537,18 +1537,18 @@ dirserv_compute_performance_thresholds(routerlist_t *rl,
    * sort them and use that to compute thresholds. */
   n_active = n_active_nonexit = 0;
   /* Uptime for every active router. */
-  uptimes = tor_malloc(sizeof(uint32_t)*smartlist_len(rl->routers));
+  uptimes = tor_calloc(sizeof(uint32_t), smartlist_len(rl->routers));
   /* Bandwidth for every active router. */
-  bandwidths_kb = tor_malloc(sizeof(uint32_t)*smartlist_len(rl->routers));
+  bandwidths_kb = tor_calloc(sizeof(uint32_t), smartlist_len(rl->routers));
   /* Bandwidth for every active non-exit router. */
   bandwidths_excluding_exits_kb =
-    tor_malloc(sizeof(uint32_t)*smartlist_len(rl->routers));
+    tor_calloc(sizeof(uint32_t), smartlist_len(rl->routers));
   /* Weighted mean time between failure for each active router. */
-  mtbfs = tor_malloc(sizeof(double)*smartlist_len(rl->routers));
+  mtbfs = tor_calloc(sizeof(double), smartlist_len(rl->routers));
   /* Time-known for each active router. */
-  tks = tor_malloc(sizeof(long)*smartlist_len(rl->routers));
+  tks = tor_calloc(sizeof(long), smartlist_len(rl->routers));
   /* Weighted fractional uptime for each active router. */
-  wfus = tor_malloc(sizeof(double)*smartlist_len(rl->routers));
+  wfus = tor_calloc(sizeof(double), smartlist_len(rl->routers));
 
   nodelist_assert_ok();
 

+ 18 - 18
src/or/dirvote.c

@@ -607,7 +607,7 @@ dirvote_compute_params(smartlist_t *votes, int method, int total_authorities)
      between INT32_MIN and INT32_MAX inclusive.  This should be guaranteed by
      the parsing code. */
 
-  vals = tor_malloc(sizeof(int)*n_votes);
+  vals = tor_calloc(sizeof(int), n_votes);
 
   SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
     if (!v->net_params)
@@ -1353,11 +1353,11 @@ networkstatus_compute_consensus(smartlist_t *votes,
    * routers we might need to talk about. */
   {
     int n_votes = smartlist_len(votes);
-    time_t *va_times = tor_malloc(n_votes * sizeof(time_t));
-    time_t *fu_times = tor_malloc(n_votes * sizeof(time_t));
-    time_t *vu_times = tor_malloc(n_votes * sizeof(time_t));
-    int *votesec_list = tor_malloc(n_votes * sizeof(int));
-    int *distsec_list = tor_malloc(n_votes * sizeof(int));
+    time_t *va_times = tor_calloc(n_votes, sizeof(time_t));
+    time_t *fu_times = tor_calloc(n_votes, sizeof(time_t));
+    time_t *vu_times = tor_calloc(n_votes, sizeof(time_t));
+    int *votesec_list = tor_calloc(n_votes, sizeof(int));
+    int *distsec_list = tor_calloc(n_votes, sizeof(int));
     int n_versioning_clients = 0, n_versioning_servers = 0;
     smartlist_t *combined_client_versions = smartlist_new();
     smartlist_t *combined_server_versions = smartlist_new();
@@ -1555,9 +1555,9 @@ networkstatus_compute_consensus(smartlist_t *votes,
     smartlist_t *chosen_flags = smartlist_new();
     smartlist_t *versions = smartlist_new();
     smartlist_t *exitsummaries = smartlist_new();
-    uint32_t *bandwidths_kb = tor_malloc(sizeof(uint32_t) *
+    uint32_t *bandwidths_kb = tor_calloc(sizeof(uint32_t),
                                          smartlist_len(votes));
-    uint32_t *measured_bws_kb = tor_malloc(sizeof(uint32_t) *
+    uint32_t *measured_bws_kb = tor_calloc(sizeof(uint32_t),
                                            smartlist_len(votes));
     int num_bandwidths;
     int num_mbws;
@@ -1579,13 +1579,13 @@ networkstatus_compute_consensus(smartlist_t *votes,
     memset(conflict, 0, sizeof(conflict));
     memset(unknown, 0xff, sizeof(conflict));
 
-    index = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
-    size = tor_malloc_zero(sizeof(int)*smartlist_len(votes));
-    n_voter_flags = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
-    n_flag_voters = tor_malloc_zero(sizeof(int) * smartlist_len(flags));
-    flag_map = tor_malloc_zero(sizeof(int*) * smartlist_len(votes));
-    named_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
-    unnamed_flag = tor_malloc_zero(sizeof(int) * smartlist_len(votes));
+    index = tor_calloc(sizeof(int), smartlist_len(votes));
+    size = tor_calloc(sizeof(int), smartlist_len(votes));
+    n_voter_flags = tor_calloc(sizeof(int), smartlist_len(votes));
+    n_flag_voters = tor_calloc(sizeof(int), smartlist_len(flags));
+    flag_map = tor_calloc(sizeof(int *), smartlist_len(votes));
+    named_flag = tor_calloc(sizeof(int), smartlist_len(votes));
+    unnamed_flag = tor_calloc(sizeof(int), smartlist_len(votes));
     for (i = 0; i < smartlist_len(votes); ++i)
       unnamed_flag[i] = named_flag[i] = -1;
     chosen_named_idx = smartlist_string_pos(flags, "Named");
@@ -1597,8 +1597,8 @@ networkstatus_compute_consensus(smartlist_t *votes,
      * that they're actually set before doing U64_LITERAL(1) << index with
      * them.*/
     SMARTLIST_FOREACH_BEGIN(votes, networkstatus_t *, v) {
-      flag_map[v_sl_idx] = tor_malloc_zero(
-                           sizeof(int)*smartlist_len(v->known_flags));
+      flag_map[v_sl_idx] = tor_calloc(sizeof(int),
+                                      smartlist_len(v->known_flags));
       if (smartlist_len(v->known_flags) > MAX_KNOWN_FLAGS_IN_VOTE) {
         log_warn(LD_BUG, "Somehow, a vote has %d entries in known_flags",
                  smartlist_len(v->known_flags));
@@ -1678,7 +1678,7 @@ networkstatus_compute_consensus(smartlist_t *votes,
     );
 
     /* Now go through all the votes */
-    flag_counts = tor_malloc(sizeof(int) * smartlist_len(flags));
+    flag_counts = tor_calloc(sizeof(int), smartlist_len(flags));
     while (1) {
       vote_routerstatus_t *rs;
       routerstatus_t rs_out;

+ 2 - 2
src/or/geoip.c

@@ -963,7 +963,7 @@ geoip_get_dirreq_history(dirreq_type_t type)
     /* We may have rounded 'completed' up.  Here we want to use the
      * real value. */
     complete = smartlist_len(dirreq_completed);
-    dltimes = tor_malloc_zero(sizeof(uint32_t) * complete);
+    dltimes = tor_calloc(sizeof(uint32_t), complete);
     SMARTLIST_FOREACH_BEGIN(dirreq_completed, dirreq_map_entry_t *, ent) {
       uint32_t bytes_per_second;
       uint32_t time_diff = (uint32_t) tv_mdiff(&ent->request_time,
@@ -1033,7 +1033,7 @@ geoip_get_client_history(geoip_client_action_t action,
   if (!geoip_is_loaded(AF_INET) && !geoip_is_loaded(AF_INET6))
     return -1;
 
-  counts = tor_malloc_zero(sizeof(unsigned)*n_countries);
+  counts = tor_calloc(sizeof(unsigned), n_countries);
   HT_FOREACH(ent, clientmap, &client_history) {
     int country;
     if ((*ent)->action != (int)action)

+ 1 - 1
src/or/rendcommon.c

@@ -528,7 +528,7 @@ rend_encode_v2_descriptors(smartlist_t *descs_out,
         return -1;
     }
     /* Base64-encode introduction points. */
-    ipos_base64 = tor_malloc_zero(ipos_len * 2);
+    ipos_base64 = tor_calloc(ipos_len, 2);
     if (base64_encode(ipos_base64, ipos_len * 2, ipos, ipos_len)<0) {
       log_warn(LD_REND, "Could not encode introduction point string to "
                "base64. length=%d", (int)ipos_len);

+ 4 - 7
src/or/rephist.c

@@ -1998,12 +1998,9 @@ void
 rep_hist_exit_stats_init(time_t now)
 {
   start_of_exit_stats_interval = now;
-  exit_bytes_read = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
-                                    sizeof(uint64_t));
-  exit_bytes_written = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
-                                       sizeof(uint64_t));
-  exit_streams = tor_malloc_zero(EXIT_STATS_NUM_PORTS *
-                                 sizeof(uint32_t));
+  exit_bytes_read = tor_calloc(EXIT_STATS_NUM_PORTS, sizeof(uint64_t));
+  exit_bytes_written = tor_calloc(EXIT_STATS_NUM_PORTS, sizeof(uint64_t));
+  exit_streams = tor_calloc(EXIT_STATS_NUM_PORTS, sizeof(uint32_t));
 }
 
 /** Reset counters for exit port statistics. */
@@ -2572,7 +2569,7 @@ rep_hist_format_desc_stats(time_t now)
 
   size = digestmap_size(served_descs);
   if (size > 0) {
-    vals = tor_malloc(size * sizeof(int));
+    vals = tor_calloc(size, sizeof(int));
     for (iter = digestmap_iter_init(served_descs);
          !digestmap_iter_done(iter);
          iter = digestmap_iter_next(served_descs, iter)) {

+ 6 - 6
src/or/routerlist.c

@@ -1534,7 +1534,7 @@ dirserver_choose_by_weight(const smartlist_t *servers, double authority_weight)
   u64_dbl_t *weights;
   const dir_server_t *ds;
 
-  weights = tor_malloc(sizeof(u64_dbl_t) * n);
+  weights = tor_calloc(sizeof(u64_dbl_t), n);
   for (i = 0; i < n; ++i) {
     ds = smartlist_get(servers, i);
     weights[i].dbl = ds->weight;
@@ -2042,7 +2042,7 @@ compute_weighted_bandwidths(const smartlist_t *sl,
   Web /= weight_scale;
   Wdb /= weight_scale;
 
-  bandwidths = tor_malloc_zero(sizeof(u64_dbl_t)*smartlist_len(sl));
+  bandwidths = tor_calloc(sizeof(u64_dbl_t), smartlist_len(sl));
 
   // Cycle through smartlist and total the bandwidth.
   SMARTLIST_FOREACH_BEGIN(sl, const node_t *, node) {
@@ -2189,7 +2189,7 @@ smartlist_choose_node_by_bandwidth(const smartlist_t *sl,
 
   /* First count the total bandwidth weight, and make a list
    * of each value.  We use UINT64_MAX to indicate "unknown". */
-  bandwidths = tor_malloc_zero(sizeof(u64_dbl_t)*smartlist_len(sl));
+  bandwidths = tor_calloc(sizeof(u64_dbl_t), smartlist_len(sl));
   fast_bits = bitarray_init_zero(smartlist_len(sl));
   exit_bits = bitarray_init_zero(smartlist_len(sl));
   guard_bits = bitarray_init_zero(smartlist_len(sl));
@@ -3581,9 +3581,9 @@ routerlist_remove_old_cached_routers_with_id(time_t now,
     n_extra = n - mdpr;
   }
 
-  lifespans = tor_malloc_zero(sizeof(struct duration_idx_t)*n);
-  rmv = tor_malloc_zero(sizeof(uint8_t)*n);
-  must_keep = tor_malloc_zero(sizeof(uint8_t)*n);
+  lifespans = tor_calloc(sizeof(struct duration_idx_t), n);
+  rmv = tor_calloc(sizeof(uint8_t), n);
+  must_keep = tor_calloc(sizeof(uint8_t), n);
   /* Set lifespans to contain the lifespan and index of each server. */
   /* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
   for (i = lo; i <= hi; ++i) {

+ 1 - 1
src/test/test_pt.c

@@ -194,7 +194,7 @@ test_pt_protocol(void)
   managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
   mp->conf_state = PT_PROTO_LAUNCHED;
   mp->transports = smartlist_new();
-  mp->argv = tor_malloc_zero(sizeof(char*)*2);
+  mp->argv = tor_calloc(sizeof(char *), 2);
   mp->argv[0] = tor_strdup("<testcase>");
 
   /* various wrong protocol runs: */

+ 1 - 1
src/test/test_util.c

@@ -1487,7 +1487,7 @@ test_util_gzip(void)
 
   /* Check whether we can uncompress concatenated, compressed strings. */
   tor_free(buf3);
-  buf2 = tor_realloc(buf2, len1*2);
+  buf2 = tor_reallocarray(buf2, len1, 2);
   memcpy(buf2+len1, buf2, len1);
   test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
                                    ZLIB_METHOD, 1, LOG_INFO));