From f9badfd50240e419a84ac15fb84b1affa71557a6 Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Sun, 7 Jul 2019 11:37:48 +0000 Subject: [PATCH] Fix: Corrected double referencing of (x) in type checking macros that resulted in memory leaks --- include/igloo/ro.h | 11 +++++++---- src/ro.c | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/include/igloo/ro.h b/include/igloo/ro.h index ae5f5e8..4498990 100644 --- a/include/igloo/ro.h +++ b/include/igloo/ro.h @@ -89,22 +89,25 @@ struct igloo_ro_base_tag { int igloo_ro_new__return_zero(igloo_ro_t self, const igloo_ro_type_t *type, va_list ap); /* ---[ END PRIVATE ]--- */ +igloo_ro_t igloo_RO_TO_TYPE_raw(igloo_ro_t object, const igloo_ro_type_t *type); #ifdef IGLOO_CTC_HAVE_TYPE_ATTRIBUTE_TRANSPARENT_UNION #define igloo_RO__GETBASE(x) (((igloo_ro_t)(x)).subtype__igloo_ro_base_t) #define igloo_RO_NULL ((igloo_ro_t)(igloo_ro_base_t*)NULL) #define igloo_RO_IS_NULL(x) (igloo_RO__GETBASE((x)) == NULL) -#define igloo_RO_TO_TYPE(x,type) (igloo_RO_IS_VALID((x),type) ? ((igloo_ro_t)(x)).subtype__ ## type : NULL) +#define igloo_RO_TO_TYPE(x,type) (((igloo_ro_t)igloo_RO_TO_TYPE_raw((x), (igloo_ro__type__ ## type))).subtype__ ## type) #else #define igloo_RO__GETBASE(x) ((igloo_ro_base_t*)(x)) #define igloo_RO_NULL NULL #define igloo_RO_IS_NULL(x) ((x) == NULL) -#define igloo_RO_TO_TYPE(x,type) (igloo_RO_IS_VALID((x),type) ? (type*)(x) : (type*)NULL) +#define igloo_RO_TO_TYPE(x,type) ((type*)igloo_RO_TO_TYPE_raw((x), (igloo_ro__type__ ## type))) #endif #define igloo_RO_GET_TYPE(x) (igloo_RO__GETBASE((x)) == NULL ? NULL : igloo_RO__GETBASE((x))->type) #define igloo_RO_GET_TYPENAME(x) (igloo_RO_GET_TYPE((x)) == NULL ? NULL : igloo_RO_GET_TYPE((x))->type_name) -#define igloo_RO_IS_VALID(x,type) (!igloo_RO_IS_NULL((x)) && igloo_RO_GET_TYPE((x)) == (igloo_ro__type__ ## type) && igloo_RO__GETBASE((x))->refc) -#define igloo_RO_HAS_TYPE(x,type) (!igloo_RO_IS_NULL((x)) && igloo_RO_GET_TYPE((x)) == (type)) +int igloo_RO_IS_VALID_raw(igloo_ro_t object, const igloo_ro_type_t *type); +#define igloo_RO_IS_VALID(x,type) igloo_RO_IS_VALID_raw((x), (igloo_ro__type__ ## type)) +int igloo_RO_HAS_TYPE_raw(igloo_ro_t object, const igloo_ro_type_t *type); +#define igloo_RO_HAS_TYPE(x,type) igloo_RO_HAS_TYPE_raw((x), (type)) /* Create a new refobject * The type argument gives the type for the new object, diff --git a/src/ro.c b/src/ro.c index 1e985d0..7f3c0a4 100644 --- a/src/ro.c +++ b/src/ro.c @@ -43,6 +43,29 @@ static inline int check_type(const igloo_ro_type_t *type) type->type_length >= sizeof(igloo_ro_base_t); } + +static inline int igloo_RO_HAS_TYPE_raw_il(igloo_ro_t object, const igloo_ro_type_t *type) +{ + return !igloo_RO_IS_NULL(object) && igloo_RO_GET_TYPE(object) == type; +} +int igloo_RO_HAS_TYPE_raw(igloo_ro_t object, const igloo_ro_type_t *type) +{ + return igloo_RO_HAS_TYPE_raw_il(object, type); +} +static inline int igloo_RO_IS_VALID_raw_li(igloo_ro_t object, const igloo_ro_type_t *type) +{ + return igloo_RO_HAS_TYPE_raw_il(object, type) && igloo_RO__GETBASE(object)->refc; +} +int igloo_RO_IS_VALID_raw(igloo_ro_t object, const igloo_ro_type_t *type) +{ + return igloo_RO_IS_VALID_raw_li(object, type); +} +igloo_ro_t igloo_RO_TO_TYPE_raw(igloo_ro_t object, const igloo_ro_type_t *type) +{ + return igloo_RO_IS_VALID_raw_li(object, type) ? object : igloo_RO_NULL; +} + + igloo_ro_t igloo_ro_new__raw(const igloo_ro_type_t *type, const char *name, igloo_ro_t associated) { igloo_ro_base_t *base;