summaryrefslogtreecommitdiffstats
path: root/src/bplist.c
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 /src/bplist.c
parent8c78d89041b713bffcb0b09fee4468304a3a54d5 (diff)
downloadlibplist-4e82bc85671cfe50763de2637b54cb8576d7976f.tar.gz
libplist-4e82bc85671cfe50763de2637b54cb8576d7976f.tar.bz2
Add NULL checks across codebase
Diffstat (limited to 'src/bplist.c')
-rw-r--r--src/bplist.c64
1 files changed, 59 insertions, 5 deletions
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