diff options
author | Tatsuya Kinoshita <tats@debian.org> | 2015-01-15 11:52:51 +0000 |
---|---|---|
committer | Tatsuya Kinoshita <tats@debian.org> | 2015-01-15 11:52:51 +0000 |
commit | ffc254ab3865f9a49e24a6af4e2802627383f9f3 (patch) | |
tree | dce7d671c0b681460d5428d0126b90792ea1214d /alloc.h | |
parent | Merge branch 'bug/printf' (diff) | |
parent | Drop C99 features (diff) | |
download | w3m-ffc254ab3865f9a49e24a6af4e2802627383f9f3.tar.gz w3m-ffc254ab3865f9a49e24a6af4e2802627383f9f3.zip |
Merge branch 'bug/alloc'
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 > 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 */ |