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/log.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/log.c')
-rw-r--r-- | src/log.c | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -18,6 +18,7 @@ #include "serd/serd.h" +#include <assert.h> #include <stdarg.h> #include <stdbool.h> #include <stdio.h> @@ -106,6 +107,8 @@ serd_set_log_func(SerdWorld* const world, const SerdLogFunc log_func, void* const handle) { + assert(world); + world->log_func = log_func; world->log_handle = handle; } @@ -118,6 +121,9 @@ serd_vxlogf(const SerdWorld* const world, const char* const fmt, va_list args) { + assert(world); + assert(fmt); + if (world->log_func) { char message[512] = {0}; const int r = vsnprintf(message, sizeof(message), fmt, args); @@ -165,6 +171,9 @@ serd_xlogf(const SerdWorld* const world, const char* const fmt, ...) { + assert(world); + assert(fmt); + va_list args; va_start(args, fmt); @@ -180,6 +189,9 @@ serd_vlogf(const SerdWorld* const world, const char* const fmt, va_list args) { + assert(world); + assert(fmt); + return serd_vxlogf(world, level, 0u, NULL, fmt, args); } @@ -189,6 +201,9 @@ serd_logf(const SerdWorld* const world, const char* const fmt, ...) { + assert(world); + assert(fmt); + va_list args; va_start(args, fmt); @@ -205,6 +220,9 @@ serd_vlogf_at(const SerdWorld* SERD_NONNULL world, const char* SERD_NONNULL fmt, va_list args) { + assert(world); + assert(fmt); + if (!caret) { return serd_vxlogf(world, level, 0u, NULL, fmt, args); } @@ -230,6 +248,9 @@ serd_logf_at(const SerdWorld* SERD_NONNULL world, const char* SERD_NONNULL fmt, ...) { + assert(world); + assert(fmt); + va_list args; va_start(args, fmt); |