aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-08-13 10:27:13 -0400
committerNick Mathewson <nickm@torproject.org>2014-08-13 10:39:56 -0400
commit19b137bc05d7414e478945c61f41409aa886462b (patch)
tree3d618f96bc4a256ef174276b9e94d3e4b668a440 /src
parentbb68c731b897a967a4b7eb138728fa077617646e (diff)
downloadtor-19b137bc05d7414e478945c61f41409aa886462b.tar.gz
tor-19b137bc05d7414e478945c61f41409aa886462b.zip
Add reallocarray clone so we can stop doing multiply-then-reallocate
Diffstat (limited to 'src')
-rw-r--r--src/common/util.c14
-rw-r--r--src/common/util.h3
2 files changed, 17 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 8589344dbe..947325108e 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -244,6 +244,20 @@ tor_realloc_(void *ptr, size_t size DMALLOC_PARAMS)
return result;
}
+/**
+ * Try to realloc <b>ptr</b> so that it takes up sz1 * sz2 bytes. Check for
+ * overflow. Unlike other allocation functions, return NULL on overflow.
+ */
+void *
+tor_reallocarray_(void *ptr, size_t sz1, size_t sz2 DMALLOC_PARAMS)
+{
+ /* XXXX we can make this return 0, but we would need to check all the
+ * reallocarray users. */
+ tor_assert(sz2 == 0 || sz1 < SIZE_T_CEILING / sz2);
+
+ return tor_realloc(ptr, (sz1 * sz2) DMALLOC_FN_ARGS);
+}
+
/** Return a newly allocated copy of the NUL-terminated string s. On
* error, log and terminate. (Like strdup(s), but never returns
* NULL.)
diff --git a/src/common/util.h b/src/common/util.h
index 97367a9a7b..61bb05f016 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -79,6 +79,7 @@ void *tor_malloc_(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
void *tor_malloc_zero_(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
void *tor_calloc_(size_t nmemb, size_t size DMALLOC_PARAMS) ATTR_MALLOC;
void *tor_realloc_(void *ptr, size_t size DMALLOC_PARAMS);
+void *tor_reallocarray_(void *ptr, size_t size1, size_t size2 DMALLOC_PARAMS);
char *tor_strdup_(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
char *tor_strndup_(const char *s, size_t n DMALLOC_PARAMS)
ATTR_MALLOC ATTR_NONNULL((1));
@@ -116,6 +117,8 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
#define tor_malloc_zero(size) tor_malloc_zero_(size DMALLOC_ARGS)
#define tor_calloc(nmemb,size) tor_calloc_(nmemb, size DMALLOC_ARGS)
#define tor_realloc(ptr, size) tor_realloc_(ptr, size DMALLOC_ARGS)
+#define tor_reallocarray(ptr, sz1, sz2) \
+ tor_reallocarray_((ptr), (sz1), (sz2) DMALLOC_ARGS)
#define tor_strdup(s) tor_strdup_(s DMALLOC_ARGS)
#define tor_strndup(s, n) tor_strndup_(s, n DMALLOC_ARGS)
#define tor_memdup(s, n) tor_memdup_(s, n DMALLOC_ARGS)