json: Only use calloc instead of malloc

* src/cJSON.c, src/gpgme-json.c (CALLOC_ONLY): New define
to change xmalloc / xtrymalloc to use calloc.

--
Some people consider malloc dangerous as it might allow an
information leak.
This commit is contained in:
Andre Heinecke 2018-08-08 14:25:28 +02:00
parent 974a95db04
commit fdc07b3ddc
No known key found for this signature in database
GPG Key ID: 2978E9D40CBABA5C
2 changed files with 39 additions and 8 deletions

View File

@ -45,20 +45,42 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <gpg-error.h>
#include "cJSON.h" #include "cJSON.h"
/* Only use calloc. */
#define CALLOC_ONLY 1
/* To avoid that a compiler optimizes certain memset calls away, these
macros may be used instead. */
#define wipememory2(_ptr,_set,_len) do { \
volatile char *_vptr=(volatile char *)(_ptr); \
size_t _vlen=(_len); \
while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \
} while(0)
#define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
/* We use malloc function wrappers from gpgrt (aka libgpg-error). */ /* We use malloc function wrappers from gpgrt (aka libgpg-error). */
#if GPGRT_VERSION_NUMBER >= 0x011c00 /* 1.28 */ #if GPGRT_VERSION_NUMBER >= 0x011c00 /* 1.28 */
# include <gpgrt.h> # include <gpgrt.h>
# define xtrymalloc(a) gpgrt_malloc ((a))
# define xtrycalloc(a,b) gpgrt_calloc ((a), (b)) # define xtrycalloc(a,b) gpgrt_calloc ((a), (b))
# define xtrystrdup(a) gpgrt_strdup ((a)) # define xtrystrdup(a) gpgrt_strdup ((a))
# define xfree(a) gpgrt_free ((a)) # define xfree(a) gpgrt_free ((a))
# if CALLOC_ONLY
# define xtrymalloc(a) gpgrt_calloc (1, (a))
# else
# define xtrymalloc(a) gpgrt_malloc ((a))
# endif
#else /* Without gpgrt (aka libgpg-error). */ #else /* Without gpgrt (aka libgpg-error). */
# define xtrymalloc(a) malloc ((a))
# define xtrycalloc(a,b) calloc ((a), (b)) # define xtrycalloc(a,b) calloc ((a), (b))
# define xtrystrdup(a) strdup ((a)) # define xtrystrdup(a) strdup ((a))
# define xfree(a) free ((a)) # define xfree(a) free ((a))
# if CALLOC_ONLY
# define xtrymalloc(a) calloc (1, (a))
# else
# define xtrymalloc(a) malloc ((a))
# endif
#endif #endif

View File

@ -87,13 +87,7 @@ static struct
* Helper functions and macros * Helper functions and macros
*/ */
#define xtrymalloc(a) gpgrt_malloc ((a))
#define xtrystrdup(a) gpgrt_strdup ((a)) #define xtrystrdup(a) gpgrt_strdup ((a))
#define xmalloc(a) ({ \
void *_r = gpgrt_malloc ((a)); \
if (!_r) \
xoutofcore ("malloc"); \
_r; })
#define xcalloc(a,b) ({ \ #define xcalloc(a,b) ({ \
void *_r = gpgrt_calloc ((a), (b)); \ void *_r = gpgrt_calloc ((a), (b)); \
if (!_r) \ if (!_r) \
@ -111,6 +105,21 @@ static struct
_r; }) _r; })
#define xfree(a) gpgrt_free ((a)) #define xfree(a) gpgrt_free ((a))
/* Only use calloc. */
#define CALLOC_ONLY 1
#if CALLOC_ONLY
#define xtrymalloc(a) gpgrt_calloc (1, (a))
#define xmalloc(a) xcalloc(1, (a))
#else
#define xtrymalloc(a) gpgrt_malloc ((a))
#define xmalloc(a) ({ \
void *_r = gpgrt_malloc ((a)); \
if (!_r) \
xoutofcore ("malloc"); \
_r; })
#endif
#define spacep(p) (*(p) == ' ' || *(p) == '\t') #define spacep(p) (*(p) == ' ' || *(p) == '\t')
#ifndef HAVE_STPCPY #ifndef HAVE_STPCPY