From a2a13280f517bc76607ee680720db4b024c85f52 Mon Sep 17 00:00:00 2001 From: Duncan MacGregor Date: Mon, 22 Aug 2022 19:48:03 +0100 Subject: [PATCH] Add some null checks to mark functions. (#812) --- ext/oj/fast.c | 28 +++++++++++++++------------- ext/oj/intern.c | 15 ++++++--------- ext/oj/saj2.c | 2 +- ext/oj/usual.c | 2 +- ext/oj/val_stack.c | 2 +- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/ext/oj/fast.c b/ext/oj/fast.c index 93fdcec7..10c2a23d 100644 --- a/ext/oj/fast.c +++ b/ext/oj/fast.c @@ -677,21 +677,23 @@ static void free_doc_cb(void *x) { } static void mark_leaf(Leaf leaf) { - switch (leaf->value_type) { - case COL_VAL: - if (NULL != leaf->elements) { - Leaf first = leaf->elements->next; - Leaf e = first; + if (NULL != leaf) { + switch (leaf->value_type) { + case COL_VAL: + if (NULL != leaf->elements) { + Leaf first = leaf->elements->next; + Leaf e = first; - do { - mark_leaf(e); - e = e->next; - } while (e != first); - } - break; - case RUBY_VAL: mark(leaf->value); break; + do { + mark_leaf(e); + e = e->next; + } while (e != first); + } + break; + case RUBY_VAL: mark(leaf->value); break; - default: break; + default: break; + } } } diff --git a/ext/oj/intern.c b/ext/oj/intern.c index f9831fcd..8bf8dd08 100644 --- a/ext/oj/intern.c +++ b/ext/oj/intern.c @@ -37,13 +37,10 @@ typedef struct _hash { struct _hash class_hash; struct _hash attr_hash; -static struct _cache *str_cache = NULL; static VALUE str_cache_obj; -static struct _cache *sym_cache = NULL; static VALUE sym_cache_obj; -static struct _cache *attr_cache = NULL; static VALUE attr_cache_obj; static VALUE form_str(const char *str, size_t len) { @@ -93,15 +90,15 @@ void oj_hash_init(void) { rb_gc_register_address(&cache_class); rb_undef_alloc_func(cache_class); - str_cache = cache_create(0, form_str, true, true); + struct _cache *str_cache = cache_create(0, form_str, true, true); str_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, str_cache); rb_gc_register_address(&str_cache_obj); - sym_cache = cache_create(0, form_sym, true, true); + struct _cache *sym_cache = cache_create(0, form_sym, true, true); sym_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, sym_cache); rb_gc_register_address(&sym_cache_obj); - attr_cache = cache_create(0, form_attr, false, true); + struct _cache *attr_cache = cache_create(0, form_attr, false, true); attr_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, attr_cache); rb_gc_register_address(&attr_cache_obj); @@ -122,18 +119,18 @@ oj_str_intern(const char *key, size_t len) { #if HAVE_RB_ENC_INTERNED_STR && 0 return rb_enc_interned_str(key, len, rb_utf8_encoding()); #else - return cache_intern(str_cache, key, len); + return cache_intern(DATA_PTR(str_cache_obj), key, len); #endif } VALUE oj_sym_intern(const char *key, size_t len) { - return cache_intern(sym_cache, key, len); + return cache_intern(DATA_PTR(sym_cache_obj), key, len); } ID oj_attr_intern(const char *key, size_t len) { - return cache_intern(attr_cache, key, len); + return cache_intern(DATA_PTR(attr_cache_obj), key, len); } static uint64_t hash_calc(const uint8_t *key, size_t len) { diff --git a/ext/oj/saj2.c b/ext/oj/saj2.c index c0b8ffa1..4e4a8a8c 100644 --- a/ext/oj/saj2.c +++ b/ext/oj/saj2.c @@ -563,7 +563,7 @@ static void dfree(ojParser p) { } static void mark(ojParser p) { - if (NULL == p->ctx) { + if (NULL == p || NULL == p->ctx) { return; } Delegate d = (Delegate)p->ctx; diff --git a/ext/oj/usual.c b/ext/oj/usual.c index e5da49ff..a71c4480 100644 --- a/ext/oj/usual.c +++ b/ext/oj/usual.c @@ -684,7 +684,7 @@ static void dfree(ojParser p) { } static void mark(ojParser p) { - if (NULL == p->ctx) { + if (NULL == p || NULL == p->ctx) { return; } Delegate d = (Delegate)p->ctx; diff --git a/ext/oj/val_stack.c b/ext/oj/val_stack.c index f6582600..eeaeaf94 100644 --- a/ext/oj/val_stack.c +++ b/ext/oj/val_stack.c @@ -12,7 +12,7 @@ static void mark(void *ptr) { ValStack stack = (ValStack)ptr; Val v; - if (0 == ptr) { + if (NULL == ptr) { return; } #ifdef HAVE_PTHREAD_MUTEX_INIT