aboutsummaryrefslogtreecommitdiffstats
path: root/alloc.h
diff options
context:
space:
mode:
authorTatsuya Kinoshita <tats@debian.org>2021-01-02 00:20:37 +0000
committerTatsuya Kinoshita <tats@debian.org>2021-01-02 00:20:37 +0000
commit1d0ba25a660483da1272a31dd077ed94441e3d9f (patch)
tree1d8dee52cd1e3d340fe178a8193dc96c4496db84 /alloc.h
parentMerge branch 'cvstrunk' into upstream (diff)
downloadw3m-upstream.tar.gz
w3m-upstream.zip
New upstream version 0.5.3+git20210102upstream/0.5.3+git20210102upstream
Diffstat (limited to 'alloc.h')
-rw-r--r--alloc.h39
1 files changed, 39 insertions, 0 deletions
diff --git a/alloc.h b/alloc.h
new file mode 100644
index 0000000..fa0d391
--- /dev/null
+++ b/alloc.h
@@ -0,0 +1,39 @@
+/*
+ * by Scarlett. public domain.
+ * replacements for w3m's allocation macros which add overflow
+ * detection and concentrate the macros in one file
+ */
+#ifndef W3_ALLOC_H
+#define W3_ALLOC_H
+#include <gc.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <limits.h>
+
+static inline size_t
+z_mult_no_oflow_(size_t n, size_t size)
+{
+ if (size != 0 && n > ULONG_MAX / size) {
+ fprintf(stderr,
+ "w3m: overflow in malloc, %lu*%lu\n", (unsigned long)n, (unsigned long)size);
+ exit(1);
+ }
+ return n * size;
+}
+
+#define New(type) \
+ (GC_MALLOC(sizeof(type)))
+
+#define NewAtom(type) \
+ (GC_MALLOC_ATOMIC(sizeof(type)))
+
+#define New_N(type, n) \
+ (GC_MALLOC(z_mult_no_oflow_((n), sizeof(type))))
+
+#define NewAtom_N(type, n) \
+ (GC_MALLOC_ATOMIC(z_mult_no_oflow_((n), sizeof(type))))
+
+#define New_Reuse(type, ptr, n) \
+ (GC_REALLOC((ptr), z_mult_no_oflow_((n), sizeof(type))))
+
+#endif /* W3_ALLOC_H */