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/writer.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/writer.c')
-rw-r--r-- | src/writer.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/writer.c b/src/writer.c index 5416d144..e73533e7 100644 --- a/src/writer.c +++ b/src/writer.c @@ -1268,6 +1268,8 @@ serd_writer_write_node(SerdWriter* writer, const SerdNode* node) SerdStatus serd_writer_finish(SerdWriter* writer) { + assert(writer); + SerdStatus st = SERD_SUCCESS; if (ctx(writer, SERD_SUBJECT)) { st = write_sep(writer, writer->context.flags, SEP_END_S); @@ -1296,6 +1298,10 @@ serd_writer_new(SerdWorld* world, const SerdEnv* env, SerdByteSink* byte_sink) { + assert(world); + assert(env); + assert(byte_sink); + const WriteContext context = WRITE_CONTEXT_NULL; SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter)); @@ -1321,6 +1327,8 @@ serd_writer_new(SerdWorld* world, void serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) { + assert(writer); + free(writer->bprefix); writer->bprefix_len = 0; writer->bprefix = NULL; @@ -1336,6 +1344,8 @@ serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) { + assert(writer); + if (uri->type != SERD_URI) { return SERD_ERR_BAD_ARG; } @@ -1362,6 +1372,8 @@ serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) SerdStatus serd_writer_set_root_uri(SerdWriter* writer, const SerdNode* uri) { + assert(writer); + serd_node_free(writer->root_node); writer->root_node = NULL; writer->root_uri = SERD_URI_NULL; @@ -1421,6 +1433,7 @@ serd_writer_free(SerdWriter* writer) const SerdSink* serd_writer_sink(SerdWriter* writer) { + assert(writer); return &writer->iface; } @@ -1430,7 +1443,10 @@ serd_buffer_sink(const void* const buf, const size_t nmemb, void* const stream) { + assert(buf); assert(size == 1); + assert(stream); + (void)size; SerdBuffer* buffer = (SerdBuffer*)stream; @@ -1443,6 +1459,8 @@ serd_buffer_sink(const void* const buf, char* serd_buffer_sink_finish(SerdBuffer* const stream) { + assert(stream); + serd_buffer_sink("", 1, 1, stream); return (char*)stream->buf; } |