about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhexcoder <hexcoder-@users.noreply.github.com>2020-01-18 17:50:56 +0100
committerGitHub <noreply@github.com>2020-01-18 17:50:56 +0100
commit99fe0becd45581042fab242add4a209d4a1ad887 (patch)
tree2736618ffac3aae6ac716512f29bc5d6b3d5e274
parent0eec6221554c260b2d93de73e88c2279c4479753 (diff)
parent858b5da24e3b060e2ebf6ab48ded22fbdd7d3ceb (diff)
downloadafl++-99fe0becd45581042fab242add4a209d4a1ad887.tar.gz
Merge pull request #174 from devnexen/reallocarray_API_bsd
libdislocator: reallocarray API introduction
-rw-r--r--libdislocator/libdislocator.so.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/libdislocator/libdislocator.so.c b/libdislocator/libdislocator.so.c
index 20649470..b9ba8967 100644
--- a/libdislocator/libdislocator.so.c
+++ b/libdislocator/libdislocator.so.c
@@ -397,6 +397,28 @@ void* aligned_alloc(size_t align, size_t len) {
 
 }
 
+/* specific BSD api mainly checking possible overflow for the size */
+
+void* reallocarray(void* ptr, size_t elem_len, size_t elem_cnt) {
+
+  const size_t elem_lim = 1UL << (sizeof(size_t) * 4);
+  const size_t elem_tot = elem_len * elem_cnt;
+  void*        ret = NULL;
+
+  if ((elem_len >= elem_lim || elem_cnt >= elem_lim) && elem_len > 0 &&
+      elem_cnt > (SIZE_MAX / elem_len)) {
+
+    DEBUGF("reallocarray size overflow (%zu)", elem_tot);
+
+  } else {
+
+    ret = realloc(ptr, elem_tot);
+
+  }
+
+  return ret;
+}
+
 __attribute__((constructor)) void __dislocator_init(void) {
 
   u8* tmp = (u8*)getenv("AFL_LD_LIMIT_MB");