Skip to content

Commit 2acba9d

Browse files
committed
Merge pull request #1920 from usta/possibleProblem
possible null problems
2 parents 676383a + 10c6ef3 commit 2acba9d

File tree

1 file changed

+65
-50
lines changed

1 file changed

+65
-50
lines changed

src/json.cpp

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -544,42 +544,50 @@ JsonNode *json_mkobject(void)
544544

545545
static void append_node(JsonNode *parent, JsonNode *child)
546546
{
547-
child->parent = parent;
548-
child->prev = parent->children.tail;
549-
child->next = NULL;
550-
551-
if (parent->children.tail != NULL)
552-
parent->children.tail->next = child;
553-
else
554-
parent->children.head = child;
555-
parent->children.tail = child;
547+
if (child != NULL && parent != NULL) {
548+
child->parent = parent;
549+
child->prev = parent->children.tail;
550+
child->next = NULL;
551+
552+
if (parent->children.tail != NULL)
553+
parent->children.tail->next = child;
554+
else
555+
parent->children.head = child;
556+
parent->children.tail = child;
557+
}
556558
}
557559

558560
static void prepend_node(JsonNode *parent, JsonNode *child)
559561
{
560-
child->parent = parent;
561-
child->prev = NULL;
562-
child->next = parent->children.head;
563-
564-
if (parent->children.head != NULL)
565-
parent->children.head->prev = child;
566-
else
567-
parent->children.tail = child;
568-
parent->children.head = child;
562+
if (child != NULL && parent != NULL) {
563+
child->parent = parent;
564+
child->prev = NULL;
565+
child->next = parent->children.head;
566+
567+
if (parent->children.head != NULL)
568+
parent->children.head->prev = child;
569+
else
570+
parent->children.tail = child;
571+
parent->children.head = child;
572+
}
569573
}
570574

571575
static void append_member(JsonNode *object, char *key, JsonNode *value)
572576
{
573-
value->key = key;
574-
append_node(object, value);
577+
if (value != NULL && object != NULL) {
578+
value->key = key;
579+
append_node(object, value);
580+
}
575581
}
576582

577583
void json_append_element(JsonNode *array, JsonNode *element)
578584
{
579-
assert(array->tag == JSON_ARRAY);
580-
assert(element->parent == NULL);
581-
582-
append_node(array, element);
585+
if (array != NULL && element !=NULL) {
586+
assert(array->tag == JSON_ARRAY);
587+
assert(element->parent == NULL);
588+
589+
append_node(array, element);
590+
}
583591
}
584592

585593
void json_prepend_element(JsonNode *array, JsonNode *element)
@@ -592,40 +600,47 @@ void json_prepend_element(JsonNode *array, JsonNode *element)
592600

593601
void json_append_member(JsonNode *object, const char *key, JsonNode *value)
594602
{
595-
assert(object->tag == JSON_OBJECT);
596-
assert(value->parent == NULL);
597-
598-
append_member(object, json_strdup(key), value);
603+
if (object != NULL && key != NULL && value != NULL) {
604+
assert(object->tag == JSON_OBJECT);
605+
assert(value->parent == NULL);
606+
607+
append_member(object, json_strdup(key), value);
608+
}
599609
}
600610

601611
void json_prepend_member(JsonNode *object, const char *key, JsonNode *value)
602612
{
603-
assert(object->tag == JSON_OBJECT);
604-
assert(value->parent == NULL);
605-
606-
value->key = json_strdup(key);
607-
prepend_node(object, value);
613+
if (object != NULL && key != NULL && value != NULL) {
614+
assert(object->tag == JSON_OBJECT);
615+
assert(value->parent == NULL);
616+
617+
value->key = json_strdup(key);
618+
prepend_node(object, value);
619+
}
608620
}
609621

610622
void json_remove_from_parent(JsonNode *node)
611623
{
612-
JsonNode *parent = node->parent;
613-
614-
if (parent != NULL) {
615-
if (node->prev != NULL)
616-
node->prev->next = node->next;
617-
else
618-
parent->children.head = node->next;
619-
if (node->next != NULL)
620-
node->next->prev = node->prev;
621-
else
622-
parent->children.tail = node->prev;
623-
624-
free(node->key);
625-
626-
node->parent = NULL;
627-
node->prev = node->next = NULL;
628-
node->key = NULL;
624+
if (node != NULL) {
625+
JsonNode *parent = node->parent;
626+
627+
if (parent != NULL) {
628+
if (node->prev != NULL)
629+
node->prev->next = node->next;
630+
else
631+
parent->children.head = node->next;
632+
633+
if (node->next != NULL)
634+
node->next->prev = node->prev;
635+
else
636+
parent->children.tail = node->prev;
637+
638+
free(node->key);
639+
640+
node->parent = NULL;
641+
node->prev = node->next = NULL;
642+
node->key = NULL;
643+
}
629644
}
630645
}
631646

0 commit comments

Comments
 (0)