From 155fceabe7070b6610d577734734d038d097b088 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 2 Jan 2022 14:12:54 -0500 Subject: Add assertions for all non-null pointers in the public API Clang issues warnings at build time based on the SERD_NONNULL annotations, which is a much better approach in general. However, it does not cover cases where the API is being used with another compiler, or without a compiler that can statically check things at all (such as Python or other dynamic language bindings). In those situations, getting a clear assertion message is a lot less confusing than a random crash somewhere in serd, and it makes it clear that the bug is in the caller, so I think it's worth the tedious verbosity. --- src/node.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/node.c') diff --git a/src/node.c b/src/node.c index 6958ea5e..550fa732 100644 --- a/src/node.c +++ b/src/node.c @@ -546,6 +546,8 @@ serd_node_get_value_as(const SerdNode* const node, bool serd_get_boolean(const SerdNode* const node) { + assert(node); + bool value = false; serd_node_get_value_as(node, EXESS_BOOLEAN, sizeof(value), &value); @@ -555,6 +557,8 @@ serd_get_boolean(const SerdNode* const node) double serd_get_double(const SerdNode* const node) { + assert(node); + double value = (double)NAN; // NOLINT(google-readability-casting) serd_node_get_value_as(node, EXESS_DOUBLE, sizeof(value), &value); @@ -564,6 +568,8 @@ serd_get_double(const SerdNode* const node) float serd_get_float(const SerdNode* const node) { + assert(node); + float value = (float)NAN; // NOLINT(google-readability-casting) serd_node_get_value_as(node, EXESS_FLOAT, sizeof(value), &value); @@ -573,6 +579,8 @@ serd_get_float(const SerdNode* const node) int64_t serd_get_integer(const SerdNode* const node) { + assert(node); + int64_t value = 0; serd_node_get_value_as(node, EXESS_LONG, sizeof(value), &value); @@ -865,24 +873,32 @@ serd_new_base64(const void* buf, size_t size, const SerdStringView datatype) SerdNodeType serd_node_type(const SerdNode* const node) { + assert(node); + return node->type; } const char* serd_node_string(const SerdNode* const node) { + assert(node); + return (const char*)(node + 1); } size_t serd_node_length(const SerdNode* const node) { + assert(node); + return node->length; } SerdStringView serd_node_string_view(const SerdNode* const node) { + assert(node); + const SerdStringView r = {(const char*)(node + 1), node->length}; return r; @@ -891,6 +907,8 @@ serd_node_string_view(const SerdNode* const node) SerdURIView serd_node_uri_view(const SerdNode* const node) { + assert(node); + return (node->type == SERD_URI) ? serd_parse_uri(serd_node_string(node)) : SERD_URI_NULL; } @@ -898,7 +916,9 @@ serd_node_uri_view(const SerdNode* const node) const SerdNode* serd_node_datatype(const SerdNode* const node) { - if (!node || !(node->flags & SERD_HAS_DATATYPE)) { + assert(node); + + if (!(node->flags & SERD_HAS_DATATYPE)) { return NULL; } @@ -910,7 +930,9 @@ serd_node_datatype(const SerdNode* const node) const SerdNode* serd_node_language(const SerdNode* const node) { - if (!node || !(node->flags & SERD_HAS_LANGUAGE)) { + assert(node); + + if (!(node->flags & SERD_HAS_LANGUAGE)) { return NULL; } -- cgit v1.2.1