Skip to content

Commit

Permalink
Add some null checks to mark functions. (#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
aardvark179 authored Aug 22, 2022
1 parent 64d87b9 commit a2a1328
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
28 changes: 15 additions & 13 deletions ext/oj/fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
15 changes: 6 additions & 9 deletions ext/oj/intern.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion ext/oj/saj2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ext/oj/usual.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ext/oj/val_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a2a1328

Please sign in to comment.