diff options
author | Scarlett <scarlett@xavin.net> | 2015-01-15 10:38:11 +0000 |
---|---|---|
committer | Tatsuya Kinoshita <tats@debian.org> | 2015-01-15 10:38:11 +0000 |
commit | cbec7032ee36346649fc5ebecc4b1c01f01178f1 (patch) | |
tree | 69907176f0aed9cef693dbbfd0398fa561c4a4cc /alloc.h | |
parent | Merge branch 'bug/printf' (diff) | |
download | w3m-cbec7032ee36346649fc5ebecc4b1c01f01178f1.tar.gz w3m-cbec7032ee36346649fc5ebecc4b1c01f01178f1.zip |
Add overflow detection
Origin: http://marc.info/?l=openbsd-ports&m=142090828929750&w=2
* main.c: Call exit(1) when out of memory to avoid dereferencing null
pointers when gc's malloc fails.
* alloc.h: Replacements for w3m's allocation macros which add
overflow detection and concentrate the macros in one file.
* indep.h, libwc/charset.c, libwc/status.c, matrix.c: Use the
overflow-detecting allocation macros from alloc.h.
Diffstat (limited to 'alloc.h')
-rw-r--r-- | alloc.h | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -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 > SIZE_MAX / size) { + fprintf(stderr, + "w3m: overflow in malloc, %zu*%zu\n", n, 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 */ |