summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2019-05-16 04:00:06 +0200
committerGravatar Nikias Bassen2019-05-16 04:00:06 +0200
commit77bef0de4362f7bd1dd07c14c63324ec988403a0 (patch)
treef64f4b788ba70619eca458c0a33acfafd43c1f0a
parent8e5b802cb93a23bb4b97a0e12ff29d70e2064fe5 (diff)
downloadlibplist-77bef0de4362f7bd1dd07c14c63324ec988403a0.tar.gz
libplist-77bef0de4362f7bd1dd07c14c63324ec988403a0.tar.bz2
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
-rw-r--r--src/plist.c69
1 files 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 <string.h>
-#include <assert.h>
#include "plist.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
+#include <assert.h>
#ifdef WIN32
#include <windows.h>
@@ -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)