diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-08-30 19:32:55 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-08-30 19:33:40 +0200 |
commit | 65460fc99d4257fed9bc437fad6ae190935c4e68 (patch) | |
tree | 9e66ca2718d8c4b944be2184c6ecd5c227f8fbba /src/slibc-alloc.c | |
parent | m (diff) | |
download | slibc-65460fc99d4257fed9bc437fad6ae190935c4e68.tar.gz slibc-65460fc99d4257fed9bc437fad6ae190935c4e68.tar.bz2 slibc-65460fc99d4257fed9bc437fad6ae190935c4e68.tar.xz |
malloc: add overflow checks and add aligned alloc-functions
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/slibc-alloc.c')
-rw-r--r-- | src/slibc-alloc.c | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/slibc-alloc.c b/src/slibc-alloc.c index 0cbf623..08e6209 100644 --- a/src/slibc-alloc.c +++ b/src/slibc-alloc.c @@ -23,8 +23,9 @@ -#define PURE_ALLOC(p) (((char*)(p)) - sizeof(size_t)) -#define PURE_SIZE(z) ((z) + sizeof(size_t)) +#define __ALIGN(p) (*(size_t*)(((char*)(p)) - sizeof(size_t))) +#define PURE_ALLOC(p) (((char*)(p)) - (__ALIGN(p) + 2 * sizeof(size_t))) +#define PURE_SIZE(p) (*(size_t*)PURE_ALLOC(p) + 2 * sizeof(size_t)) @@ -38,7 +39,7 @@ void fast_free(void* segment) { if (segument == NULL) return; - munmap(PURE_ALLOC(segment), PURE_SIZE(*(size_t*)segment)); + munmap(PURE_ALLOC(segment), PURE_SIZE(segment)); } @@ -52,7 +53,7 @@ void secure_free(void* segment) { if (segument == NULL) return; - explicit_bzero(PURE_ALLOC(segment), PURE_SIZE(allocsize(segment))); + explicit_bzero(PURE_ALLOC(segment), PURE_SIZE(segment)); fast_free(segment); } @@ -105,7 +106,7 @@ size_t allocsize(void* segment) if (new_ptr != ptr) \ { \ if (CLEAR_FREE) \ - explicit_bzero(PURE_ALLOC(ptr), PURE_SIZE(old_size)); \ + explicit_bzero(PURE_ALLOC(ptr), PURE_SIZE(ptr)); \ fast_free(new_ptr); \ } \ \ |