Commit 9114346d authored by Nick Mathewson's avatar Nick Mathewson 🦀
Browse files

Merge remote-tracking branch 'public/use_calloc'

parents 938deecc 4570805e
Loading
Loading
Loading
Loading

changes/bug12855

0 → 100644
+5 −0
Original line number Diff line number Diff line
  o Code simplification and refactoring
    - Use calloc and reallocarray functions in preference to
      multiply-then-malloc.  This makes it less likely for us to fall
      victim to an integer overflow attack when allocating. Resolves
      ticket 12855.
+20 −0
Original line number Diff line number Diff line
// Use calloc or realloc as appropriate instead of multiply-and-alloc

@malloc_to_calloc@
expression a,b;
@@
- tor_malloc(a * b)
+ tor_calloc(a, b)

@malloc_zero_to_calloc@
expression a, b;
@@
- tor_malloc_zero(a * b)
+ tor_calloc(a, b)

@realloc_to_reallocarray@
expression a, b;
expression p;
@@
- tor_realloc(p, a * b)
+ tor_reallocarray(p, a, b)
+2 −2
Original line number Diff line number Diff line
@@ -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
Original line number Diff line number Diff line
@@ -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
Original line number Diff line number Diff line
@@ -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,
Loading