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/byte_sink.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/byte_sink.c')
-rw-r--r-- | src/byte_sink.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/byte_sink.c b/src/byte_sink.c index e93e5a07..42d12f7b 100644 --- a/src/byte_sink.c +++ b/src/byte_sink.c @@ -21,6 +21,7 @@ #include "serd/serd.h" +#include <assert.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> @@ -32,6 +33,8 @@ SerdByteSink* serd_byte_sink_new_buffer(SerdBuffer* const buffer) { + assert(buffer); + SerdByteSink* sink = (SerdByteSink*)calloc(1, sizeof(SerdByteSink)); sink->write_func = serd_buffer_sink; @@ -65,6 +68,8 @@ serd_byte_sink_new_internal(const SerdWriteFunc write_func, SerdByteSink* serd_byte_sink_new_filename(const char* const path, const size_t block_size) { + assert(path); + if (!block_size) { return NULL; } @@ -87,6 +92,8 @@ serd_byte_sink_new_function(const SerdWriteFunc write_func, void* const stream, const size_t block_size) { + assert(write_func); + return block_size ? serd_byte_sink_new_internal( write_func, stream, block_size, TO_FUNCTION) : NULL; @@ -95,6 +102,8 @@ serd_byte_sink_new_function(const SerdWriteFunc write_func, void serd_byte_sink_flush(SerdByteSink* sink) { + assert(sink); + if (sink->block_size > 1 && sink->size > 0) { sink->write_func(sink->buf, 1, sink->size, sink->stream); sink->size = 0; @@ -104,6 +113,8 @@ serd_byte_sink_flush(SerdByteSink* sink) SerdStatus serd_byte_sink_close(SerdByteSink* sink) { + assert(sink); + serd_byte_sink_flush(sink); if (sink->type == TO_FILENAME && sink->stream) { |