diff options
author | David Robillard <d@drobilla.net> | 2024-03-28 10:20:46 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2024-03-28 10:20:46 -0400 |
commit | 6aed064084c8c1af02adb46b0066aaee1f436edf (patch) | |
tree | d618dc61fe9d2ed60f592e83b33753029eb91441 | |
parent | a3051f0fbe9b76bb91498d62647dad1d27b43691 (diff) | |
download | serd-6aed064084c8c1af02adb46b0066aaee1f436edf.tar.gz serd-6aed064084c8c1af02adb46b0066aaee1f436edf.tar.bz2 serd-6aed064084c8c1af02adb46b0066aaee1f436edf.zip |
Add assertions for all non-null pointers in the public API
-rw-r--r-- | src/base64.c | 4 | ||||
-rw-r--r-- | src/byte_source.c | 5 | ||||
-rw-r--r-- | src/env.c | 10 | ||||
-rw-r--r-- | src/n3.c | 2 | ||||
-rw-r--r-- | src/node.c | 13 | ||||
-rw-r--r-- | src/reader.c | 26 | ||||
-rw-r--r-- | src/string.c | 3 | ||||
-rw-r--r-- | src/uri.c | 18 | ||||
-rw-r--r-- | src/writer.c | 25 |
9 files changed, 106 insertions, 0 deletions
diff --git a/src/base64.c b/src/base64.c index ce8fe74d..42d296b3 100644 --- a/src/base64.c +++ b/src/base64.c @@ -8,6 +8,7 @@ #include "serd/serd.h" +#include <assert.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> @@ -98,6 +99,9 @@ serd_base64_decode(const uint8_t* const str, const size_t len, size_t* const size) { + assert(str); + assert(size); + void* buf = malloc((len * 3) / 4 + 2); *size = 0; diff --git a/src/byte_source.c b/src/byte_source.c index 7c191745..5443f4dd 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -7,6 +7,7 @@ #include "serd/serd.h" +#include <assert.h> #include <stdbool.h> #include <stdint.h> #include <string.h> @@ -43,6 +44,10 @@ serd_byte_source_open_source(SerdByteSource* const source, { const Cursor cur = {name, 1, 1}; + assert(read_func); + assert(error_func); + assert(page_size > 0); + memset(source, '\0', sizeof(*source)); source->stream = stream; source->from_stream = true; @@ -3,6 +3,7 @@ #include "serd/serd.h" +#include <assert.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> @@ -55,6 +56,8 @@ serd_env_free(SerdEnv* const env) const SerdNode* serd_env_get_base_uri(const SerdEnv* const env, SerdURI* const out) { + assert(env); + if (out) { *out = env->base_uri; } @@ -65,6 +68,8 @@ serd_env_get_base_uri(const SerdEnv* const env, SerdURI* const out) SerdStatus serd_env_set_base_uri(SerdEnv* const env, const SerdNode* const uri) { + assert(env); + if (uri && uri->type != SERD_URI) { return SERD_ERR_BAD_ARG; } @@ -134,6 +139,8 @@ serd_env_set_prefix(SerdEnv* const env, const SerdNode* const name, const SerdNode* const uri) { + assert(env); + if (!name->buf || uri->type != SERD_URI) { return SERD_ERR_BAD_ARG; } @@ -263,6 +270,9 @@ serd_env_foreach(const SerdEnv* const env, const SerdPrefixSink func, void* const handle) { + assert(env); + assert(func); + for (size_t i = 0; i < env->n_prefixes; ++i) { func(handle, &env->prefixes[i].name, &env->prefixes[i].uri); } @@ -1696,6 +1696,8 @@ read_n3_statement(SerdReader* const reader) SerdStatus serd_reader_skip_until_byte(SerdReader* const reader, const uint8_t byte) { + assert(reader); + int c = peek_byte(reader); while (c != byte && c != EOF) { @@ -102,6 +102,9 @@ serd_node_copy(const SerdNode* const node) bool serd_node_equals(const SerdNode* const a, const SerdNode* const b) { + assert(a); + assert(b); + return (a == b) || (a->type == b->type && a->n_bytes == b->n_bytes && a->n_chars == b->n_chars && @@ -114,6 +117,8 @@ serd_node_new_uri_from_node(const SerdNode* const uri_node, const SerdURI* const base, SerdURI* const out) { + assert(uri_node); + return (uri_node->type == SERD_URI && uri_node->buf) ? serd_node_new_uri_from_string(uri_node->buf, base, out) : SERD_NODE_NULL; @@ -186,6 +191,8 @@ serd_node_new_file_uri(const uint8_t* const path, SerdURI* const out, const bool escape) { + assert(path); + const size_t path_len = strlen((const char*)path); const size_t hostname_len = hostname ? strlen((const char*)hostname) : 0; const bool is_windows = is_windows_path(path); @@ -238,6 +245,8 @@ serd_node_new_uri(const SerdURI* const uri, const SerdURI* const base, SerdURI* const out) { + assert(uri); + SerdURI abs_uri = *uri; if (base) { serd_uri_resolve(uri, base, &abs_uri); @@ -266,6 +275,8 @@ serd_node_new_relative_uri(const SerdURI* const uri, const SerdURI* const root, SerdURI* const out) { + assert(uri); + const size_t uri_len = serd_uri_string_length(uri); const size_t base_len = serd_uri_string_length(base); uint8_t* buf = (uint8_t*)malloc(uri_len + base_len + 1); @@ -377,6 +388,8 @@ serd_node_new_blob(const void* const buf, const size_t size, const bool wrap_lines) { + assert(buf); + const size_t len = serd_base64_get_length(size, wrap_lines); uint8_t* str = (uint8_t*)calloc(len + 2, 1); SerdNode node = {str, len, len, 0, SERD_LITERAL}; diff --git a/src/reader.c b/src/reader.c index 5d87216d..1936808a 100644 --- a/src/reader.c +++ b/src/reader.c @@ -9,6 +9,7 @@ #include "serd_internal.h" +#include <assert.h> #include <errno.h> #include <stdarg.h> #include <stdint.h> @@ -237,6 +238,7 @@ serd_reader_free(SerdReader* const reader) void* serd_reader_get_handle(const SerdReader* const reader) { + assert(reader); return reader->handle; } @@ -244,6 +246,8 @@ void serd_reader_add_blank_prefix(SerdReader* const reader, const uint8_t* const prefix) { + assert(reader); + free(reader->bprefix); reader->bprefix_len = 0; reader->bprefix = NULL; @@ -260,6 +264,7 @@ void serd_reader_set_default_graph(SerdReader* const reader, const SerdNode* const graph) { + assert(reader); serd_node_free(&reader->default_graph); reader->default_graph = serd_node_copy(graph); } @@ -267,6 +272,9 @@ serd_reader_set_default_graph(SerdReader* const reader, SerdStatus serd_reader_read_file(SerdReader* const reader, const uint8_t* const uri) { + assert(reader); + assert(uri); + uint8_t* const path = serd_file_uri_parse(uri, NULL); if (!path) { return SERD_ERR_BAD_ARG; @@ -307,6 +315,9 @@ serd_reader_start_stream(SerdReader* const reader, const uint8_t* const name, const bool bulk) { + assert(reader); + assert(file); + return serd_reader_start_source_stream(reader, bulk ? (SerdSource)fread : serd_file_read_byte, @@ -324,6 +335,10 @@ serd_reader_start_source_stream(SerdReader* const reader, const uint8_t* const name, const size_t page_size) { + assert(reader); + assert(read_func); + assert(error_func); + return serd_byte_source_open_source( &reader->source, read_func, error_func, stream, name, page_size); } @@ -345,6 +360,8 @@ serd_reader_prepare(SerdReader* const reader) SerdStatus serd_reader_read_chunk(SerdReader* const reader) { + assert(reader); + SerdStatus st = SERD_SUCCESS; if (!reader->source.prepared) { st = serd_reader_prepare(reader); @@ -365,6 +382,8 @@ serd_reader_read_chunk(SerdReader* const reader) SerdStatus serd_reader_end_stream(SerdReader* const reader) { + assert(reader); + return serd_byte_source_close(&reader->source); } @@ -389,6 +408,10 @@ serd_reader_read_source(SerdReader* const reader, const uint8_t* const name, const size_t page_size) { + assert(reader); + assert(source); + assert(error); + SerdStatus st = serd_reader_start_source_stream( reader, source, error, stream, name, page_size); @@ -408,6 +431,9 @@ serd_reader_read_source(SerdReader* const reader, SerdStatus serd_reader_read_string(SerdReader* const reader, const uint8_t* const utf8) { + assert(reader); + assert(utf8); + serd_byte_source_open_string(&reader->source, utf8); SerdStatus st = serd_reader_prepare(reader); diff --git a/src/string.c b/src/string.c index ecba3463..52fb4a91 100644 --- a/src/string.c +++ b/src/string.c @@ -5,6 +5,7 @@ #include "serd/serd.h" +#include <assert.h> #include <math.h> #include <stdint.h> #include <stdlib.h> @@ -131,6 +132,8 @@ read_sign(const char** const sptr) double serd_strtod(const char* const str, char** const endptr) { + assert(str); + double result = 0.0; // Point s at the first non-whitespace character @@ -7,6 +7,7 @@ #include "serd/serd.h" +#include <assert.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> @@ -16,6 +17,8 @@ const uint8_t* serd_uri_to_path(const uint8_t* uri) { + assert(uri); + const uint8_t* path = uri; if (!is_windows_path(uri) && serd_uri_string_has_scheme(uri)) { if (!!strncmp((const char*)uri, "file:", 5)) { @@ -42,6 +45,8 @@ serd_uri_to_path(const uint8_t* uri) uint8_t* serd_file_uri_parse(const uint8_t* const uri, uint8_t** const hostname) { + assert(uri); + const uint8_t* path = uri; if (hostname) { *hostname = NULL; @@ -113,6 +118,9 @@ serd_uri_string_has_scheme(const uint8_t* utf8) SerdStatus serd_uri_parse(const uint8_t* const utf8, SerdURI* const out) { + assert(utf8); + assert(out); + *out = SERD_URI_NULL; const uint8_t* ptr = utf8; @@ -293,6 +301,10 @@ serd_uri_resolve(const SerdURI* const r, const SerdURI* const base, SerdURI* const t) { + assert(r); + assert(base); + assert(t); + if (!base->scheme.len) { *t = *r; // Don't resolve against non-absolute URIs return; @@ -416,6 +428,10 @@ serd_uri_serialise_relative(const SerdURI* const uri, SerdSink sink, void* const stream) { + assert(uri); + assert(sink); + assert(stream); + size_t len = 0; const bool relative = root ? uri_is_under(uri, root) : uri_is_related(uri, base); @@ -468,5 +484,7 @@ serd_uri_serialise_relative(const SerdURI* const uri, size_t serd_uri_serialise(const SerdURI* const uri, SerdSink sink, void* const stream) { + assert(sink); + assert(stream); return serd_uri_serialise_relative(uri, NULL, NULL, sink, stream); } diff --git a/src/writer.c b/src/writer.c index b1ffdbdd..96e6135d 100644 --- a/src/writer.c +++ b/src/writer.c @@ -12,6 +12,7 @@ #include "serd/serd.h" +#include <assert.h> #include <errno.h> #include <stdarg.h> #include <stdbool.h> @@ -944,6 +945,8 @@ serd_writer_write_statement(SerdWriter* writer, const SerdNode* datatype, const SerdNode* lang) { + assert(writer); + SerdStatus st = SERD_SUCCESS; if (!is_resource(subject) || !is_resource(predicate) || !object || @@ -1093,6 +1096,8 @@ serd_writer_write_statement(SerdWriter* writer, SerdStatus serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) { + assert(writer); + SerdStatus st = SERD_SUCCESS; if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) { @@ -1122,6 +1127,8 @@ serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) SerdStatus serd_writer_finish(SerdWriter* writer) { + assert(writer); + const SerdStatus st0 = terminate_context(writer); const SerdStatus st1 = serd_byte_sink_flush(&writer->byte_sink); free_anon_stack(writer); @@ -1137,6 +1144,9 @@ serd_writer_new(SerdSyntax syntax, SerdSink ssink, void* stream) { + assert(env); + assert(sink); + const WriteContext context = WRITE_CONTEXT_NULL; SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter)); @@ -1159,6 +1169,8 @@ serd_writer_set_error_sink(SerdWriter* writer, SerdErrorSink error_sink, void* error_handle) { + assert(writer); + assert(error_sink); writer->error_sink = error_sink; writer->error_handle = error_handle; } @@ -1166,6 +1178,8 @@ serd_writer_set_error_sink(SerdWriter* writer, void serd_writer_chop_blank_prefix(SerdWriter* writer, const uint8_t* prefix) { + assert(writer); + free(writer->bprefix); writer->bprefix_len = 0; writer->bprefix = NULL; @@ -1181,6 +1195,8 @@ serd_writer_chop_blank_prefix(SerdWriter* writer, const uint8_t* prefix) SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) { + assert(writer); + SerdStatus st = SERD_SUCCESS; TRY(st, serd_env_set_base_uri(writer->env, uri)); @@ -1201,6 +1217,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); if (uri && uri->buf) { @@ -1260,18 +1278,24 @@ serd_writer_free(SerdWriter* writer) SerdEnv* serd_writer_get_env(SerdWriter* writer) { + assert(writer); return writer->env; } size_t serd_file_sink(const void* buf, size_t len, void* stream) { + assert(buf); + assert(stream); return fwrite(buf, 1, len, (FILE*)stream); } size_t serd_chunk_sink(const void* buf, size_t len, void* stream) { + assert(buf); + assert(stream); + SerdChunk* chunk = (SerdChunk*)stream; uint8_t* new_buf = (uint8_t*)realloc((uint8_t*)chunk->buf, chunk->len + len); if (new_buf) { @@ -1285,6 +1309,7 @@ serd_chunk_sink(const void* buf, size_t len, void* stream) uint8_t* serd_chunk_sink_finish(SerdChunk* stream) { + assert(stream); serd_chunk_sink("", 1, stream); return (uint8_t*)stream->buf; } |