diff options
| author | 2019-05-16 04:00:06 +0200 | |
|---|---|---|
| committer | 2019-05-16 04:00:06 +0200 | |
| commit | 77bef0de4362f7bd1dd07c14c63324ec988403a0 (patch) | |
| tree | f64f4b788ba70619eca458c0a33acfafd43c1f0a | |
| parent | 8e5b802cb93a23bb4b97a0e12ff29d70e2064fe5 (diff) | |
| download | libplist-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.c | 69 |
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 @@ | |||
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | #include <string.h> | 25 | #include <string.h> |
| 26 | #include <assert.h> | ||
| 27 | #include "plist.h" | 26 | #include "plist.h" |
| 28 | #include <stdlib.h> | 27 | #include <stdlib.h> |
| 29 | #include <stdio.h> | 28 | #include <stdio.h> |
| 30 | #include <math.h> | 29 | #include <math.h> |
| 30 | #include <assert.h> | ||
| 31 | 31 | ||
| 32 | #ifdef WIN32 | 32 | #ifdef WIN32 |
| 33 | #include <windows.h> | 33 | #include <windows.h> |
| @@ -324,6 +324,7 @@ static void plist_copy_node(node_t *node, void *parent_node_ptr) | |||
| 324 | plist_data_t newdata = plist_new_plist_data(); | 324 | plist_data_t newdata = plist_new_plist_data(); |
| 325 | 325 | ||
| 326 | assert(data); // plist should always have data | 326 | assert(data); // plist should always have data |
| 327 | assert(newdata); | ||
| 327 | 328 | ||
| 328 | memcpy(newdata, data, sizeof(struct plist_data_s)); | 329 | memcpy(newdata, data, sizeof(struct plist_data_s)); |
| 329 | 330 | ||
| @@ -761,75 +762,105 @@ PLIST_API plist_type plist_get_node_type(plist_t node) | |||
| 761 | 762 | ||
| 762 | PLIST_API void plist_get_key_val(plist_t node, char **val) | 763 | PLIST_API void plist_get_key_val(plist_t node, char **val) |
| 763 | { | 764 | { |
| 765 | if (!node || !val) | ||
| 766 | return; | ||
| 764 | plist_type type = plist_get_node_type(node); | 767 | plist_type type = plist_get_node_type(node); |
| 765 | uint64_t length = 0; | 768 | uint64_t length = 0; |
| 766 | if (PLIST_KEY == type) | 769 | if (PLIST_KEY != type) |
| 767 | plist_get_type_and_value(node, &type, (void *) val, &length); | 770 | return; |
| 771 | plist_get_type_and_value(node, &type, (void *) val, &length); | ||
| 772 | if (!*val) | ||
| 773 | return; | ||
| 768 | assert(length == strlen(*val)); | 774 | assert(length == strlen(*val)); |
| 769 | } | 775 | } |
| 770 | 776 | ||
| 771 | PLIST_API void plist_get_string_val(plist_t node, char **val) | 777 | PLIST_API void plist_get_string_val(plist_t node, char **val) |
| 772 | { | 778 | { |
| 779 | if (!node || !val) | ||
| 780 | return; | ||
| 773 | plist_type type = plist_get_node_type(node); | 781 | plist_type type = plist_get_node_type(node); |
| 774 | uint64_t length = 0; | 782 | uint64_t length = 0; |
| 775 | if (PLIST_STRING == type) | 783 | if (PLIST_STRING != type) |
| 776 | plist_get_type_and_value(node, &type, (void *) val, &length); | 784 | return; |
| 785 | plist_get_type_and_value(node, &type, (void *) val, &length); | ||
| 786 | if (!*val) | ||
| 787 | return; | ||
| 777 | assert(length == strlen(*val)); | 788 | assert(length == strlen(*val)); |
| 778 | } | 789 | } |
| 779 | 790 | ||
| 780 | PLIST_API void plist_get_bool_val(plist_t node, uint8_t * val) | 791 | PLIST_API void plist_get_bool_val(plist_t node, uint8_t * val) |
| 781 | { | 792 | { |
| 793 | if (!node || !val) | ||
| 794 | return; | ||
| 782 | plist_type type = plist_get_node_type(node); | 795 | plist_type type = plist_get_node_type(node); |
| 783 | uint64_t length = 0; | 796 | uint64_t length = 0; |
| 784 | if (PLIST_BOOLEAN == type) | 797 | if (PLIST_BOOLEAN != type) |
| 785 | plist_get_type_and_value(node, &type, (void *) val, &length); | 798 | return; |
| 799 | plist_get_type_and_value(node, &type, (void *) val, &length); | ||
| 786 | assert(length == sizeof(uint8_t)); | 800 | assert(length == sizeof(uint8_t)); |
| 787 | } | 801 | } |
| 788 | 802 | ||
| 789 | PLIST_API void plist_get_uint_val(plist_t node, uint64_t * val) | 803 | PLIST_API void plist_get_uint_val(plist_t node, uint64_t * val) |
| 790 | { | 804 | { |
| 805 | if (!node || !val) | ||
| 806 | return; | ||
| 791 | plist_type type = plist_get_node_type(node); | 807 | plist_type type = plist_get_node_type(node); |
| 792 | uint64_t length = 0; | 808 | uint64_t length = 0; |
| 793 | if (PLIST_UINT == type) | 809 | if (PLIST_UINT != type) |
| 794 | plist_get_type_and_value(node, &type, (void *) val, &length); | 810 | return; |
| 811 | plist_get_type_and_value(node, &type, (void *) val, &length); | ||
| 795 | assert(length == sizeof(uint64_t) || length == 16); | 812 | assert(length == sizeof(uint64_t) || length == 16); |
| 796 | } | 813 | } |
| 797 | 814 | ||
| 798 | PLIST_API void plist_get_uid_val(plist_t node, uint64_t * val) | 815 | PLIST_API void plist_get_uid_val(plist_t node, uint64_t * val) |
| 799 | { | 816 | { |
| 817 | if (!node || !val) | ||
| 818 | return; | ||
| 800 | plist_type type = plist_get_node_type(node); | 819 | plist_type type = plist_get_node_type(node); |
| 801 | uint64_t length = 0; | 820 | uint64_t length = 0; |
| 802 | if (PLIST_UID == type) | 821 | if (PLIST_UID != type) |
| 803 | plist_get_type_and_value(node, &type, (void *) val, &length); | 822 | return; |
| 823 | plist_get_type_and_value(node, &type, (void *) val, &length); | ||
| 804 | assert(length == sizeof(uint64_t)); | 824 | assert(length == sizeof(uint64_t)); |
| 805 | } | 825 | } |
| 806 | 826 | ||
| 807 | PLIST_API void plist_get_real_val(plist_t node, double *val) | 827 | PLIST_API void plist_get_real_val(plist_t node, double *val) |
| 808 | { | 828 | { |
| 829 | if (!node || !val) | ||
| 830 | return; | ||
| 809 | plist_type type = plist_get_node_type(node); | 831 | plist_type type = plist_get_node_type(node); |
| 810 | uint64_t length = 0; | 832 | uint64_t length = 0; |
| 811 | if (PLIST_REAL == type) | 833 | if (PLIST_REAL != type) |
| 812 | plist_get_type_and_value(node, &type, (void *) val, &length); | 834 | return; |
| 835 | plist_get_type_and_value(node, &type, (void *) val, &length); | ||
| 813 | assert(length == sizeof(double)); | 836 | assert(length == sizeof(double)); |
| 814 | } | 837 | } |
| 815 | 838 | ||
| 816 | PLIST_API void plist_get_data_val(plist_t node, char **val, uint64_t * length) | 839 | PLIST_API void plist_get_data_val(plist_t node, char **val, uint64_t * length) |
| 817 | { | 840 | { |
| 841 | if (!node || !val || !length) | ||
| 842 | return; | ||
| 818 | plist_type type = plist_get_node_type(node); | 843 | plist_type type = plist_get_node_type(node); |
| 819 | if (PLIST_DATA == type) | 844 | if (PLIST_DATA != type) |
| 820 | plist_get_type_and_value(node, &type, (void *) val, length); | 845 | return; |
| 846 | plist_get_type_and_value(node, &type, (void *) val, length); | ||
| 821 | } | 847 | } |
| 822 | 848 | ||
| 823 | PLIST_API void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) | 849 | PLIST_API void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) |
| 824 | { | 850 | { |
| 851 | if (!node) | ||
| 852 | return; | ||
| 825 | plist_type type = plist_get_node_type(node); | 853 | plist_type type = plist_get_node_type(node); |
| 826 | uint64_t length = 0; | 854 | uint64_t length = 0; |
| 827 | double val = 0; | 855 | double val = 0; |
| 828 | if (PLIST_DATE == type) | 856 | if (PLIST_DATE != type) |
| 829 | plist_get_type_and_value(node, &type, (void *) &val, &length); | 857 | return; |
| 858 | plist_get_type_and_value(node, &type, (void *) &val, &length); | ||
| 830 | assert(length == sizeof(double)); | 859 | assert(length == sizeof(double)); |
| 831 | *sec = (int32_t)val; | 860 | if (sec) |
| 832 | *usec = (int32_t)fabs((val - (int64_t)val) * 1000000); | 861 | *sec = (int32_t)val; |
| 862 | if (usec) | ||
| 863 | *usec = (int32_t)fabs((val - (int64_t)val) * 1000000); | ||
| 833 | } | 864 | } |
| 834 | 865 | ||
| 835 | int plist_data_compare(const void *a, const void *b) | 866 | int plist_data_compare(const void *a, const void *b) |
