Refine memory management (#579)

- Added one macro to avoid dangling pointers
- Added two functions to perform NULL pointer check
  since the allocation is not guaranteed by C library, although it
  is a rare case, just for sanity
- Add NULL pointer check to brealloc() and bfree() and for sanity as well

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
This commit is contained in:
Syrone Wong
2016-04-15 09:16:38 -07:00
committed by Max Lv
parent 446e570b7d
commit ebe79bbbd8
12 changed files with 209 additions and 152 deletions
+3 -2
View File
@@ -28,6 +28,7 @@
*/
#include "json.h"
#include "mm-wrapper.h"
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
@@ -90,12 +91,12 @@ typedef struct {
static void *default_alloc(size_t size, int zero, void *user_data)
{
return zero ? calloc(1, size) : malloc(size);
return zero ? calloc(1, size) : SS_SAFEMALLOC(size);
}
static void default_free(void *ptr, void *user_data)
{
free(ptr);
SS_SAFEFREE(ptr);
}
static void *json_alloc(json_state *state, unsigned long size, int zero)