summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2026-02-12 01:20:05 +0100
committerGravatar Nikias Bassen2026-02-12 01:20:05 +0100
commit4e82bc85671cfe50763de2637b54cb8576d7976f (patch)
tree378d7d8c51e9a6a618e0f45aa6edf97e56bd3c1c
parent8c78d89041b713bffcb0b09fee4468304a3a54d5 (diff)
downloadlibplist-4e82bc85671cfe50763de2637b54cb8576d7976f.tar.gz
libplist-4e82bc85671cfe50763de2637b54cb8576d7976f.tar.bz2
Add NULL checks across codebase
-rw-r--r--src/base64.c1
-rw-r--r--src/bplist.c64
-rw-r--r--src/jplist.c18
-rw-r--r--src/plist.c116
-rw-r--r--src/xplist.c16
5 files changed, 197 insertions, 18 deletions
diff --git a/src/base64.c b/src/base64.c
index 76990b9..603ab6d 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -77,6 +77,7 @@ unsigned char *base64decode(const char *buf, size_t *size)
77 size_t len = (*size > 0) ? *size : strlen(buf); 77 size_t len = (*size > 0) ? *size : strlen(buf);
78 if (len <= 0) return NULL; 78 if (len <= 0) return NULL;
79 unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3); 79 unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3);
80 if (!outbuf) return NULL;
80 const char *ptr = buf; 81 const char *ptr = buf;
81 size_t p = 0; 82 size_t p = 0;
82 int wv, w1, w2, w3, w4; 83 int wv, w1, w2, w3, w4;
diff --git a/src/bplist.c b/src/bplist.c
index f0c44fc..308b787 100644
--- a/src/bplist.c
+++ b/src/bplist.c
@@ -27,7 +27,6 @@
27#include <stdlib.h> 27#include <stdlib.h>
28#include <stdio.h> 28#include <stdio.h>
29#include <string.h> 29#include <string.h>
30#include <assert.h>
31 30
32#include <ctype.h> 31#include <ctype.h>
33#include <inttypes.h> 32#include <inttypes.h>
@@ -279,6 +278,10 @@ static plist_t parse_bin_node_at_index(struct bplist_data *bplist, uint32_t node
279static plist_t parse_int_node(const char **bnode, uint8_t size) 278static plist_t parse_int_node(const char **bnode, uint8_t size)
280{ 279{
281 plist_data_t data = plist_new_plist_data(); 280 plist_data_t data = plist_new_plist_data();
281 if (!data) {
282 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
283 return NULL;
284 }
282 285
283 size = 1 << size; // make length less misleading 286 size = 1 << size; // make length less misleading
284 switch (size) 287 switch (size)
@@ -309,6 +312,10 @@ static plist_t parse_int_node(const char **bnode, uint8_t size)
309static plist_t parse_real_node(const char **bnode, uint8_t size) 312static plist_t parse_real_node(const char **bnode, uint8_t size)
310{ 313{
311 plist_data_t data = plist_new_plist_data(); 314 plist_data_t data = plist_new_plist_data();
315 if (!data) {
316 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
317 return NULL;
318 }
312 319
313 size = 1 << size; // make length less misleading 320 size = 1 << size; // make length less misleading
314 switch (size) 321 switch (size)
@@ -357,6 +364,10 @@ static plist_t parse_date_node(const char **bnode, uint8_t size)
357static plist_t parse_string_node(const char **bnode, uint64_t size) 364static plist_t parse_string_node(const char **bnode, uint64_t size)
358{ 365{
359 plist_data_t data = plist_new_plist_data(); 366 plist_data_t data = plist_new_plist_data();
367 if (!data) {
368 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
369 return NULL;
370 }
360 371
361 data->type = PLIST_STRING; 372 data->type = PLIST_STRING;
362 data->strval = (char *) malloc(sizeof(char) * (size + 1)); 373 data->strval = (char *) malloc(sizeof(char) * (size + 1));
@@ -446,6 +457,10 @@ static char *plist_utf16be_to_utf8(uint16_t *unistr, size_t len, size_t *items_r
446static plist_t parse_unicode_node(const char **bnode, uint64_t size) 457static plist_t parse_unicode_node(const char **bnode, uint64_t size)
447{ 458{
448 plist_data_t data = plist_new_plist_data(); 459 plist_data_t data = plist_new_plist_data();
460 if (!data) {
461 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
462 return NULL;
463 }
449 size_t items_read = 0; 464 size_t items_read = 0;
450 size_t items_written = 0; 465 size_t items_written = 0;
451 466
@@ -463,11 +478,14 @@ static plist_t parse_unicode_node(const char **bnode, uint64_t size)
463static plist_t parse_data_node(const char **bnode, uint64_t size) 478static plist_t parse_data_node(const char **bnode, uint64_t size)
464{ 479{
465 plist_data_t data = plist_new_plist_data(); 480 plist_data_t data = plist_new_plist_data();
466 481 if (!data) {
482 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
483 return NULL;
484 }
467 data->type = PLIST_DATA; 485 data->type = PLIST_DATA;
468 data->length = size; 486 data->length = size;
469 data->buff = (uint8_t *) malloc(sizeof(uint8_t) * size); 487 data->buff = (uint8_t *) malloc(sizeof(uint8_t) * size);
470 if (!data->strval) { 488 if (!data->buff) {
471 plist_free_data(data); 489 plist_free_data(data);
472 PLIST_BIN_ERR("%s: Could not allocate %" PRIu64 " bytes\n", __func__, sizeof(uint8_t) * size); 490 PLIST_BIN_ERR("%s: Could not allocate %" PRIu64 " bytes\n", __func__, sizeof(uint8_t) * size);
473 return NULL; 491 return NULL;
@@ -483,6 +501,10 @@ static plist_t parse_dict_node(struct bplist_data *bplist, const char** bnode, u
483 uint64_t str_i = 0, str_j = 0; 501 uint64_t str_i = 0, str_j = 0;
484 uint64_t index1, index2; 502 uint64_t index1, index2;
485 plist_data_t data = plist_new_plist_data(); 503 plist_data_t data = plist_new_plist_data();
504 if (!data) {
505 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
506 return NULL;
507 }
486 const char *index1_ptr = NULL; 508 const char *index1_ptr = NULL;
487 const char *index2_ptr = NULL; 509 const char *index2_ptr = NULL;
488 510
@@ -490,6 +512,11 @@ static plist_t parse_dict_node(struct bplist_data *bplist, const char** bnode, u
490 data->length = size; 512 data->length = size;
491 513
492 plist_t node = node_create(NULL, data); 514 plist_t node = node_create(NULL, data);
515 if (!node) {
516 plist_free_data(data);
517 PLIST_BIN_ERR("%s: failed to create node\n", __func__);
518 return NULL;
519 }
493 520
494 for (j = 0; j < data->length; j++) { 521 for (j = 0; j < data->length; j++) {
495 str_i = j * bplist->ref_size; 522 str_i = j * bplist->ref_size;
@@ -562,12 +589,21 @@ static plist_t parse_array_node(struct bplist_data *bplist, const char** bnode,
562 uint64_t str_j = 0; 589 uint64_t str_j = 0;
563 uint64_t index1; 590 uint64_t index1;
564 plist_data_t data = plist_new_plist_data(); 591 plist_data_t data = plist_new_plist_data();
592 if (!data) {
593 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
594 return NULL;
595 }
565 const char *index1_ptr = NULL; 596 const char *index1_ptr = NULL;
566 597
567 data->type = PLIST_ARRAY; 598 data->type = PLIST_ARRAY;
568 data->length = size; 599 data->length = size;
569 600
570 plist_t node = node_create(NULL, data); 601 plist_t node = node_create(NULL, data);
602 if (!node) {
603 plist_free_data(data);
604 PLIST_BIN_ERR("%s: failed to create node\n", __func__);
605 return NULL;
606 }
571 607
572 for (j = 0; j < data->length; j++) { 608 for (j = 0; j < data->length; j++) {
573 str_j = j * bplist->ref_size; 609 str_j = j * bplist->ref_size;
@@ -603,6 +639,10 @@ static plist_t parse_array_node(struct bplist_data *bplist, const char** bnode,
603static plist_t parse_uid_node(const char **bnode, uint8_t size) 639static plist_t parse_uid_node(const char **bnode, uint8_t size)
604{ 640{
605 plist_data_t data = plist_new_plist_data(); 641 plist_data_t data = plist_new_plist_data();
642 if (!data) {
643 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
644 return NULL;
645 }
606 size = size + 1; 646 size = size + 1;
607 data->intval = UINT_TO_HOST(*bnode, size); 647 data->intval = UINT_TO_HOST(*bnode, size);
608 if (data->intval > UINT32_MAX) { 648 if (data->intval > UINT32_MAX) {
@@ -673,6 +713,10 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object)
673 case BPLIST_TRUE: 713 case BPLIST_TRUE:
674 { 714 {
675 plist_data_t data = plist_new_plist_data(); 715 plist_data_t data = plist_new_plist_data();
716 if (!data) {
717 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
718 return NULL;
719 }
676 data->type = PLIST_BOOLEAN; 720 data->type = PLIST_BOOLEAN;
677 data->boolval = TRUE; 721 data->boolval = TRUE;
678 data->length = 1; 722 data->length = 1;
@@ -682,6 +726,10 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object)
682 case BPLIST_FALSE: 726 case BPLIST_FALSE:
683 { 727 {
684 plist_data_t data = plist_new_plist_data(); 728 plist_data_t data = plist_new_plist_data();
729 if (!data) {
730 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
731 return NULL;
732 }
685 data->type = PLIST_BOOLEAN; 733 data->type = PLIST_BOOLEAN;
686 data->boolval = FALSE; 734 data->boolval = FALSE;
687 data->length = 1; 735 data->length = 1;
@@ -691,6 +739,10 @@ static plist_t parse_bin_node(struct bplist_data *bplist, const char** object)
691 case BPLIST_NULL: 739 case BPLIST_NULL:
692 { 740 {
693 plist_data_t data = plist_new_plist_data(); 741 plist_data_t data = plist_new_plist_data();
742 if (!data) {
743 PLIST_BIN_ERR("%s: failed to allocate plist data\n", __func__);
744 return NULL;
745 }
694 data->type = PLIST_NULL; 746 data->type = PLIST_NULL;
695 data->length = 0; 747 data->length = 0;
696 return node_create(NULL, data); 748 return node_create(NULL, data);
@@ -1043,7 +1095,7 @@ static plist_err_t serialize_plist(node_t node, void* data, uint32_t depth)
1043 1095
1044 // insert new ref 1096 // insert new ref
1045 index_val = (uint64_t *) malloc(sizeof(uint64_t)); 1097 index_val = (uint64_t *) malloc(sizeof(uint64_t));
1046 assert(index_val != NULL); 1098 if (!index_val) return PLIST_ERR_NO_MEM;
1047 *index_val = ser->objects->len; 1099 *index_val = ser->objects->len;
1048 hash_table_insert(ser->ref_table, node, index_val); 1100 hash_table_insert(ser->ref_table, node, index_val);
1049 1101
@@ -1461,7 +1513,9 @@ plist_err_t plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
1461 1513
1462 //write objects and table 1514 //write objects and table
1463 offsets = (uint64_t *) malloc(num_objects * sizeof(uint64_t)); 1515 offsets = (uint64_t *) malloc(num_objects * sizeof(uint64_t));
1464 assert(offsets != NULL); 1516 if (!offsets) {
1517 return PLIST_ERR_NO_MEM;
1518 }
1465 for (i = 0; i < num_objects; i++) 1519 for (i = 0; i < num_objects; i++)
1466 { 1520 {
1467 1521
diff --git a/src/jplist.c b/src/jplist.c
index 2c88756..2bb526e 100644
--- a/src/jplist.c
+++ b/src/jplist.c
@@ -540,6 +540,10 @@ static plist_t parse_primitive(const char* js, jsmntok_info_t* ti, int* index)
540 val = plist_new_bool(1); 540 val = plist_new_bool(1);
541 } else if (!strncmp("null", str_val, str_len)) { 541 } else if (!strncmp("null", str_val, str_len)) {
542 plist_data_t data = plist_new_plist_data(); 542 plist_data_t data = plist_new_plist_data();
543 if (!data) {
544 PLIST_JSON_ERR("%s: failed to allocate plist data\n", __func__);
545 return NULL;
546 }
543 data->type = PLIST_NULL; 547 data->type = PLIST_NULL;
544 val = plist_new_node(data); 548 val = plist_new_node(data);
545 } else if (isdigit(str_val[0]) || (str_val[0] == '-' && str_val+1 < str_end && isdigit(str_val[1]))) { 549 } else if (isdigit(str_val[0]) || (str_val[0] == '-' && str_val+1 < str_end && isdigit(str_val[1]))) {
@@ -598,6 +602,10 @@ static plist_t parse_primitive(const char* js, jsmntok_info_t* ti, int* index)
598 } else { 602 } else {
599 PLIST_JSON_ERR("%s: invalid primitive value '%.*s' encountered\n", __func__, (int)str_len, str_val); 603 PLIST_JSON_ERR("%s: invalid primitive value '%.*s' encountered\n", __func__, (int)str_len, str_val);
600 } 604 }
605 if (!val) {
606 PLIST_JSON_ERR("%s: failed to create node\n", __func__);
607 return NULL;
608 }
601 (*index)++; 609 (*index)++;
602 return val; 610 return val;
603} 611}
@@ -695,10 +703,20 @@ static plist_t parse_string(const char* js, jsmntok_info_t* ti, int* index)
695 plist_t node; 703 plist_t node;
696 704
697 plist_data_t data = plist_new_plist_data(); 705 plist_data_t data = plist_new_plist_data();
706 if (!data) {
707 free(strval);
708 PLIST_JSON_ERR("%s: failed to allocate plist data\n", __func__);
709 return NULL;
710 }
698 data->type = PLIST_STRING; 711 data->type = PLIST_STRING;
699 data->strval = strval; 712 data->strval = strval;
700 data->length = str_len; 713 data->length = str_len;
701 node = plist_new_node(data); 714 node = plist_new_node(data);
715 if (!node) {
716 plist_free_data(data);
717 PLIST_JSON_ERR("%s: failed to create node\n", __func__);
718 return NULL;
719 }
702 720
703 (*index)++; 721 (*index)++;
704 return node; 722 return node;
diff --git a/src/plist.c b/src/plist.c
index ea285e0..22ef4d7 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -358,8 +358,7 @@ plist_data_t plist_get_data(plist_t node)
358 358
359plist_data_t plist_new_plist_data(void) 359plist_data_t plist_new_plist_data(void)
360{ 360{
361 plist_data_t data = (plist_data_t) calloc(1, sizeof(struct plist_data_s)); 361 return (plist_data_t) calloc(1, sizeof(struct plist_data_s));
362 return data;
363} 362}
364 363
365static unsigned int dict_key_hash(const void *data) 364static unsigned int dict_key_hash(const void *data)
@@ -471,6 +470,10 @@ static int plist_free_node(node_t root)
471plist_t plist_new_dict(void) 470plist_t plist_new_dict(void)
472{ 471{
473 plist_data_t data = plist_new_plist_data(); 472 plist_data_t data = plist_new_plist_data();
473 if (!data) {
474 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
475 return NULL;
476 }
474 data->type = PLIST_DICT; 477 data->type = PLIST_DICT;
475 return plist_new_node(data); 478 return plist_new_node(data);
476} 479}
@@ -478,6 +481,10 @@ plist_t plist_new_dict(void)
478plist_t plist_new_array(void) 481plist_t plist_new_array(void)
479{ 482{
480 plist_data_t data = plist_new_plist_data(); 483 plist_data_t data = plist_new_plist_data();
484 if (!data) {
485 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
486 return NULL;
487 }
481 data->type = PLIST_ARRAY; 488 data->type = PLIST_ARRAY;
482 return plist_new_node(data); 489 return plist_new_node(data);
483} 490}
@@ -486,24 +493,48 @@ plist_t plist_new_array(void)
486static plist_t plist_new_key(const char *val) 493static plist_t plist_new_key(const char *val)
487{ 494{
488 plist_data_t data = plist_new_plist_data(); 495 plist_data_t data = plist_new_plist_data();
496 if (!data) {
497 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
498 return NULL;
499 }
489 data->type = PLIST_KEY; 500 data->type = PLIST_KEY;
490 data->strval = strdup(val); 501 data->strval = strdup(val);
491 data->length = strlen(val); 502 if (!data->strval) {
503 plist_free_data(data);
504 PLIST_ERR("%s: strdup failed\n", __func__);
505 return NULL;
506 } else {
507 data->length = strlen(val);
508 }
492 return plist_new_node(data); 509 return plist_new_node(data);
493} 510}
494 511
495plist_t plist_new_string(const char *val) 512plist_t plist_new_string(const char *val)
496{ 513{
497 plist_data_t data = plist_new_plist_data(); 514 plist_data_t data = plist_new_plist_data();
515 if (!data) {
516 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
517 return NULL;
518 }
498 data->type = PLIST_STRING; 519 data->type = PLIST_STRING;
499 data->strval = strdup(val); 520 data->strval = strdup(val);
500 data->length = strlen(val); 521 if (!data->strval) {
522 plist_free_data(data);
523 PLIST_ERR("%s: strdup failed\n", __func__);
524 return NULL;
525 } else {
526 data->length = strlen(val);
527 }
501 return plist_new_node(data); 528 return plist_new_node(data);
502} 529}
503 530
504plist_t plist_new_bool(uint8_t val) 531plist_t plist_new_bool(uint8_t val)
505{ 532{
506 plist_data_t data = plist_new_plist_data(); 533 plist_data_t data = plist_new_plist_data();
534 if (!data) {
535 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
536 return NULL;
537 }
507 data->type = PLIST_BOOLEAN; 538 data->type = PLIST_BOOLEAN;
508 data->boolval = val; 539 data->boolval = val;
509 data->length = sizeof(uint8_t); 540 data->length = sizeof(uint8_t);
@@ -513,6 +544,10 @@ plist_t plist_new_bool(uint8_t val)
513plist_t plist_new_uint(uint64_t val) 544plist_t plist_new_uint(uint64_t val)
514{ 545{
515 plist_data_t data = plist_new_plist_data(); 546 plist_data_t data = plist_new_plist_data();
547 if (!data) {
548 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
549 return NULL;
550 }
516 data->type = PLIST_INT; 551 data->type = PLIST_INT;
517 data->intval = val; 552 data->intval = val;
518 data->length = (val > INT_MAX) ? sizeof(uint64_t)*2 : sizeof(uint64_t); 553 data->length = (val > INT_MAX) ? sizeof(uint64_t)*2 : sizeof(uint64_t);
@@ -522,6 +557,10 @@ plist_t plist_new_uint(uint64_t val)
522plist_t plist_new_int(int64_t val) 557plist_t plist_new_int(int64_t val)
523{ 558{
524 plist_data_t data = plist_new_plist_data(); 559 plist_data_t data = plist_new_plist_data();
560 if (!data) {
561 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
562 return NULL;
563 }
525 data->type = PLIST_INT; 564 data->type = PLIST_INT;
526 data->intval = val; 565 data->intval = val;
527 data->length = sizeof(uint64_t); 566 data->length = sizeof(uint64_t);
@@ -531,6 +570,10 @@ plist_t plist_new_int(int64_t val)
531plist_t plist_new_uid(uint64_t val) 570plist_t plist_new_uid(uint64_t val)
532{ 571{
533 plist_data_t data = plist_new_plist_data(); 572 plist_data_t data = plist_new_plist_data();
573 if (!data) {
574 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
575 return NULL;
576 }
534 data->type = PLIST_UID; 577 data->type = PLIST_UID;
535 data->intval = val; 578 data->intval = val;
536 data->length = sizeof(uint64_t); 579 data->length = sizeof(uint64_t);
@@ -540,6 +583,10 @@ plist_t plist_new_uid(uint64_t val)
540plist_t plist_new_real(double val) 583plist_t plist_new_real(double val)
541{ 584{
542 plist_data_t data = plist_new_plist_data(); 585 plist_data_t data = plist_new_plist_data();
586 if (!data) {
587 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
588 return NULL;
589 }
543 data->type = PLIST_REAL; 590 data->type = PLIST_REAL;
544 data->realval = val; 591 data->realval = val;
545 data->length = sizeof(double); 592 data->length = sizeof(double);
@@ -549,11 +596,19 @@ plist_t plist_new_real(double val)
549plist_t plist_new_data(const char *val, uint64_t length) 596plist_t plist_new_data(const char *val, uint64_t length)
550{ 597{
551 plist_data_t data = plist_new_plist_data(); 598 plist_data_t data = plist_new_plist_data();
599 if (!data) {
600 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
601 return NULL;
602 }
552 data->type = PLIST_DATA; 603 data->type = PLIST_DATA;
553if (val && length) { 604 if (val && length) {
554 data->buff = (uint8_t *) malloc(length); 605 data->buff = (uint8_t *) malloc(length);
555 memcpy(data->buff, val, length); 606 if (!data->buff) {
556} 607 PLIST_ERR("%s: failed to allocate %" PRIu64 " bytes\n", __func__, length);
608 return NULL;
609 }
610 memcpy(data->buff, val, length);
611 }
557 data->length = length; 612 data->length = length;
558 return plist_new_node(data); 613 return plist_new_node(data);
559} 614}
@@ -561,6 +616,10 @@ if (val && length) {
561plist_t plist_new_date(int32_t sec, int32_t usec) 616plist_t plist_new_date(int32_t sec, int32_t usec)
562{ 617{
563 plist_data_t data = plist_new_plist_data(); 618 plist_data_t data = plist_new_plist_data();
619 if (!data) {
620 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
621 return NULL;
622 }
564 data->type = PLIST_DATE; 623 data->type = PLIST_DATE;
565 data->realval = (double)sec + (double)usec / 1000000; 624 data->realval = (double)sec + (double)usec / 1000000;
566 data->length = sizeof(double); 625 data->length = sizeof(double);
@@ -570,6 +629,10 @@ plist_t plist_new_date(int32_t sec, int32_t usec)
570plist_t plist_new_unix_date(int64_t sec) 629plist_t plist_new_unix_date(int64_t sec)
571{ 630{
572 plist_data_t data = plist_new_plist_data(); 631 plist_data_t data = plist_new_plist_data();
632 if (!data) {
633 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
634 return NULL;
635 }
573 data->type = PLIST_DATE; 636 data->type = PLIST_DATE;
574 data->realval = (double)sec - MAC_EPOCH; 637 data->realval = (double)sec - MAC_EPOCH;
575 data->length = sizeof(double); 638 data->length = sizeof(double);
@@ -579,6 +642,10 @@ plist_t plist_new_unix_date(int64_t sec)
579plist_t plist_new_null(void) 642plist_t plist_new_null(void)
580{ 643{
581 plist_data_t data = plist_new_plist_data(); 644 plist_data_t data = plist_new_plist_data();
645 if (!data) {
646 PLIST_ERR("%s: failed to allocate plist data\n", __func__);
647 return NULL;
648 }
582 data->type = PLIST_NULL; 649 data->type = PLIST_NULL;
583 data->intval = 0; 650 data->intval = 0;
584 data->length = 0; 651 data->length = 0;
@@ -1128,7 +1195,6 @@ plist_t plist_dict_get_item(plist_t node, const char* key)
1128 return NULL; 1195 return NULL;
1129 } 1196 }
1130 plist_data_t data = plist_get_data(node); 1197 plist_data_t data = plist_get_data(node);
1131 assert(data);
1132 if (!data) { 1198 if (!data) {
1133 PLIST_ERR("%s: invalid node\n", __func__); 1199 PLIST_ERR("%s: invalid node\n", __func__);
1134 return NULL; 1200 return NULL;
@@ -1187,7 +1253,10 @@ void plist_dict_set_item(plist_t node, const char* key, plist_t item)
1187 PLIST_ERR("%s: corrupt dict (value without key)\n", __func__); 1253 PLIST_ERR("%s: corrupt dict (value without key)\n", __func__);
1188 return; 1254 return;
1189 } 1255 }
1190 assert(PLIST_IS_KEY((plist_t)old_key)); 1256 if (!PLIST_IS_KEY((plist_t)old_key)) {
1257 PLIST_ERR("%s: corrupt dict ('key' node is not PLIST_KEY\n", __func__);
1258 return;
1259 }
1191 1260
1192 // detach old value (do NOT free yet) 1261 // detach old value (do NOT free yet)
1193 int idx = node_detach((node_t)node, old_val); 1262 int idx = node_detach((node_t)node, old_val);
@@ -1525,10 +1594,11 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu
1525{ 1594{
1526 plist_data_t data = NULL; 1595 plist_data_t data = NULL;
1527 1596
1528 if (!node) 1597 if (!node || !type || !value || !length)
1529 return; 1598 return;
1530 1599
1531 data = plist_get_data(node); 1600 data = plist_get_data(node);
1601 if (!data) return;
1532 1602
1533 *type = data->type; 1603 *type = data->type;
1534 *length = data->length; 1604 *length = data->length;
@@ -1549,9 +1619,17 @@ static void plist_get_type_and_value(plist_t node, plist_type * type, void *valu
1549 case PLIST_KEY: 1619 case PLIST_KEY:
1550 case PLIST_STRING: 1620 case PLIST_STRING:
1551 *((char **) value) = strdup(data->strval); 1621 *((char **) value) = strdup(data->strval);
1622 if (!*((char **) value)) {
1623 PLIST_ERR("%s: strdup failed\n", __func__);
1624 return;
1625 }
1552 break; 1626 break;
1553 case PLIST_DATA: 1627 case PLIST_DATA:
1554 *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t)); 1628 *((uint8_t **) value) = (uint8_t *) malloc(*length * sizeof(uint8_t));
1629 if (!*((uint8_t **) value)) {
1630 PLIST_ERR("%s: malloc failed\n", __func__);
1631 return;
1632 }
1555 memcpy(*((uint8_t **) value), data->buff, *length * sizeof(uint8_t)); 1633 memcpy(*((uint8_t **) value), data->buff, *length * sizeof(uint8_t));
1556 break; 1634 break;
1557 case PLIST_ARRAY: 1635 case PLIST_ARRAY:
@@ -1789,11 +1867,14 @@ char plist_compare_node_value(plist_t node_l, plist_t node_r)
1789 return plist_data_compare(node_l, node_r); 1867 return plist_data_compare(node_l, node_r);
1790} 1868}
1791 1869
1792static void plist_set_element_val(plist_t node, plist_type type, const void *value, uint64_t length) 1870static plist_err_t plist_set_element_val(plist_t node, plist_type type, const void *value, uint64_t length)
1793{ 1871{
1794 //free previous allocated buffer 1872 //free previous allocated buffer
1795 plist_data_t data = plist_get_data(node); 1873 plist_data_t data = plist_get_data(node);
1796 assert(data); // a node should always have data attached 1874 if (!data) { // a node should always have data attached
1875 PLIST_ERR("%s: Failed to allocate plist data\n", __func__);
1876 return PLIST_ERR_NO_MEM;
1877 }
1797 1878
1798 switch (data->type) 1879 switch (data->type)
1799 { 1880 {
@@ -1831,9 +1912,17 @@ static void plist_set_element_val(plist_t node, plist_type type, const void *val
1831 case PLIST_KEY: 1912 case PLIST_KEY:
1832 case PLIST_STRING: 1913 case PLIST_STRING:
1833 data->strval = strdup((char *) value); 1914 data->strval = strdup((char *) value);
1915 if (!data->strval) {
1916 PLIST_ERR("%s: strdup failed\n", __func__);
1917 return PLIST_ERR_NO_MEM;
1918 }
1834 break; 1919 break;
1835 case PLIST_DATA: 1920 case PLIST_DATA:
1836 data->buff = (uint8_t *) malloc(length); 1921 data->buff = (uint8_t *) malloc(length);
1922 if (!data->buff) {
1923 PLIST_ERR("%s: malloc failed\n", __func__);
1924 return PLIST_ERR_NO_MEM;
1925 }
1837 memcpy(data->buff, value, length); 1926 memcpy(data->buff, value, length);
1838 break; 1927 break;
1839 case PLIST_ARRAY: 1928 case PLIST_ARRAY:
@@ -1841,6 +1930,7 @@ static void plist_set_element_val(plist_t node, plist_type type, const void *val
1841 default: 1930 default:
1842 break; 1931 break;
1843 } 1932 }
1933 return PLIST_ERR_SUCCESS;
1844} 1934}
1845 1935
1846void plist_set_key_val(plist_t node, const char *val) 1936void plist_set_key_val(plist_t node, const char *val)
diff --git a/src/xplist.c b/src/xplist.c
index 6100afc..73e2b9f 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -1216,7 +1216,17 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist)
1216 goto handle_closing; 1216 goto handle_closing;
1217 } 1217 }
1218 plist_data_t data = plist_new_plist_data(); 1218 plist_data_t data = plist_new_plist_data();
1219 if (!data) {
1220 PLIST_XML_ERR("failed to allocate plist data\n");
1221 ctx->err = PLIST_ERR_NO_MEM;
1222 goto err_out;
1223 }
1219 subnode = plist_new_node(data); 1224 subnode = plist_new_node(data);
1225 if (!subnode) {
1226 PLIST_XML_ERR("failed to create node\n");
1227 ctx->err = PLIST_ERR_NO_MEM;
1228 goto err_out;
1229 }
1220 1230
1221 if (!strcmp(tag, XPLIST_DICT)) { 1231 if (!strcmp(tag, XPLIST_DICT)) {
1222 data->type = PLIST_DICT; 1232 data->type = PLIST_DICT;
@@ -1425,6 +1435,12 @@ static plist_err_t node_from_xml(parse_ctx ctx, plist_t *plist)
1425 size_t size = tp->length; 1435 size_t size = tp->length;
1426 if (size > 0) { 1436 if (size > 0) {
1427 data->buff = base64decode(str_content, &size); 1437 data->buff = base64decode(str_content, &size);
1438 if (!data->buff) {
1439 text_parts_free((text_part_t*)first_part.next);
1440 PLIST_XML_ERR("failed to decode base64 stream\n");
1441 ctx->err = PLIST_ERR_NO_MEM;
1442 goto err_out;
1443 }
1428 data->length = size; 1444 data->length = size;
1429 } 1445 }
1430 1446