From 77bef0de4362f7bd1dd07c14c63324ec988403a0 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Thu, 16 May 2019 04:00:06 +0200 Subject: Ignore invalid input in plist_get_*_val() to prevent unnecessary assertions Also fixes #126 by skipping the strlen() in the assert() if for some reason NULL is returned as data --- src/plist.c | 69 ++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/src/plist.c b/src/plist.c index 1b33ec3..70386bc 100644 --- a/src/plist.c +++ b/src/plist.c @@ -23,11 +23,11 @@ #include -#include #include "plist.h" #include #include #include +#include #ifdef WIN32 #include @@ -324,6 +324,7 @@ static void plist_copy_node(node_t *node, void *parent_node_ptr) plist_data_t newdata = plist_new_plist_data(); assert(data); // plist should always have data + assert(newdata); memcpy(newdata, data, sizeof(struct plist_data_s)); @@ -761,75 +762,105 @@ PLIST_API plist_type plist_get_node_type(plist_t node) PLIST_API void plist_get_key_val(plist_t node, char **val) { + if (!node || !val) + return; plist_type type = plist_get_node_type(node); uint64_t length = 0; - if (PLIST_KEY == type) - plist_get_type_and_value(node, &type, (void *) val, &length); + if (PLIST_KEY != type) + return; + plist_get_type_and_value(node, &type, (void *) val, &length); + if (!*val) + return; assert(length == strlen(*val)); } PLIST_API void plist_get_string_val(plist_t node, char **val) { + if (!node || !val) + return; plist_type type = plist_get_node_type(node); uint64_t length = 0; - if (PLIST_STRING == type) - plist_get_type_and_value(node, &type, (void *) val, &length); + if (PLIST_STRING != type) + return; + plist_get_type_and_value(node, &type, (void *) val, &length); + if (!*val) + return; assert(length == strlen(*val)); } PLIST_API void plist_get_bool_val(plist_t node, uint8_t * val) { + if (!node || !val) + return; plist_type type = plist_get_node_type(node); uint64_t length = 0; - if (PLIST_BOOLEAN == type) - plist_get_type_and_value(node, &type, (void *) val, &length); + if (PLIST_BOOLEAN != type) + return; + plist_get_type_and_value(node, &type, (void *) val, &length); assert(length == sizeof(uint8_t)); } PLIST_API void plist_get_uint_val(plist_t node, uint64_t * val) { + if (!node || !val) + return; plist_type type = plist_get_node_type(node); uint64_t length = 0; - if (PLIST_UINT == type) - plist_get_type_and_value(node, &type, (void *) val, &length); + if (PLIST_UINT != type) + return; + plist_get_type_and_value(node, &type, (void *) val, &length); assert(length == sizeof(uint64_t) || length == 16); } PLIST_API void plist_get_uid_val(plist_t node, uint64_t * val) { + if (!node || !val) + return; plist_type type = plist_get_node_type(node); uint64_t length = 0; - if (PLIST_UID == type) - plist_get_type_and_value(node, &type, (void *) val, &length); + if (PLIST_UID != type) + return; + plist_get_type_and_value(node, &type, (void *) val, &length); assert(length == sizeof(uint64_t)); } PLIST_API void plist_get_real_val(plist_t node, double *val) { + if (!node || !val) + return; plist_type type = plist_get_node_type(node); uint64_t length = 0; - if (PLIST_REAL == type) - plist_get_type_and_value(node, &type, (void *) val, &length); + if (PLIST_REAL != type) + return; + plist_get_type_and_value(node, &type, (void *) val, &length); assert(length == sizeof(double)); } PLIST_API void plist_get_data_val(plist_t node, char **val, uint64_t * length) { + if (!node || !val || !length) + return; plist_type type = plist_get_node_type(node); - if (PLIST_DATA == type) - plist_get_type_and_value(node, &type, (void *) val, length); + if (PLIST_DATA != type) + return; + plist_get_type_and_value(node, &type, (void *) val, length); } PLIST_API void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) { + if (!node) + return; plist_type type = plist_get_node_type(node); uint64_t length = 0; double val = 0; - if (PLIST_DATE == type) - plist_get_type_and_value(node, &type, (void *) &val, &length); + if (PLIST_DATE != type) + return; + plist_get_type_and_value(node, &type, (void *) &val, &length); assert(length == sizeof(double)); - *sec = (int32_t)val; - *usec = (int32_t)fabs((val - (int64_t)val) * 1000000); + if (sec) + *sec = (int32_t)val; + if (usec) + *usec = (int32_t)fabs((val - (int64_t)val) * 1000000); } int plist_data_compare(const void *a, const void *b) -- cgit v1.1-32-gdbae