diff options
author | David Robillard <d@drobilla.net> | 2022-01-02 14:12:54 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:05 -0500 |
commit | 155fceabe7070b6610d577734734d038d097b088 (patch) | |
tree | 5bbbf327a00c2637f85f006c4b429ecc3b3cb1a3 /src/node.c | |
parent | 1159aea45d9bc4ade2e82856be403d58e050f32d (diff) | |
download | serd-155fceabe7070b6610d577734734d038d097b088.tar.gz serd-155fceabe7070b6610d577734734d038d097b088.tar.bz2 serd-155fceabe7070b6610d577734734d038d097b088.zip |
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.
Diffstat (limited to 'src/node.c')
-rw-r--r-- | src/node.c | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -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; } |