From e9a49bbd8cc3880e41d5856a575b03d755cb7aec Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Tue, 29 May 2012 15:44:46 +0200 Subject: [PATCH] isl_*alloc*: return NULL if ctx argument is NULL Whenever an isl function needs to perform an allocation, it calls one of the isl_*alloc* functions on an isl_ctx obtained either from the user or from a call to one of the *_get_ctx functions. If this isl_ctx pointer is NULL, this means that either the original isl_ctx_alloc failed or that the pointer on which the *_get_ctx function was called was NULL. In both cases, we are in an error state and we should not allocate any more memory, but instead return as soon as possible. We therefore return NULL from the isl_*alloc* functions, an error condition which should already be handled by the caller. Signed-off-by: Sven Verdoolaege --- include/isl/ctx.h | 2 +- isl_ctx.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/isl/ctx.h b/include/isl/ctx.h index 3d886974..811798ef 100644 --- a/include/isl/ctx.h +++ b/include/isl/ctx.h @@ -102,7 +102,7 @@ typedef struct isl_ctx isl_ctx; * returns the value of 'expr'. It is used to ensure, that always an isl_ctx is * passed to the following macros, even if they currently do not use it. */ -#define isl_check_ctx(ctx, expr) ((ctx != (isl_ctx *) 0) ? expr : expr) +#define isl_check_ctx(ctx, expr) ((ctx != (isl_ctx *) 0) ? expr : NULL) #define isl_alloc(ctx,type,size) ((type *)isl_check_ctx(ctx,\ malloc(size))) diff --git a/isl_ctx.c b/isl_ctx.c index 6f02684d..7720be47 100644 --- a/isl_ctx.c +++ b/isl_ctx.c @@ -11,6 +11,9 @@ #include #include +#define __isl_calloc(type,size) ((type *)calloc(1, size)) +#define __isl_calloc_type(type) __isl_calloc(type,sizeof(type)) + void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg, const char *file, int line) { @@ -86,7 +89,7 @@ isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt) opt_allocated = 1; } - ctx = isl_calloc_type(NULL, struct isl_ctx); + ctx = __isl_calloc_type(struct isl_ctx); if (!ctx) goto error; -- 2.11.4.GIT