aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-10-02 14:47:14 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:07 -0500
commitaa6b5ec5b9344bce0ea38d294aef0782c3745548 (patch)
tree0481c7a3b690a35728b0649d281e0ba3e321a428 /src
parent6076b31090176be685c30aa198edd3cebfd4fd7a (diff)
downloadserd-aa6b5ec5b9344bce0ea38d294aef0782c3745548.tar.gz
serd-aa6b5ec5b9344bce0ea38d294aef0782c3745548.tar.bz2
serd-aa6b5ec5b9344bce0ea38d294aef0782c3745548.zip
Add assertions for all non-null pointers in the public API
Diffstat (limited to 'src')
-rw-r--r--src/byte_source.c4
-rw-r--r--src/caret.c8
-rw-r--r--src/env.c7
-rw-r--r--src/node.c26
-rw-r--r--src/reader.c14
-rw-r--r--src/sink.c16
-rw-r--r--src/statement.c8
-rw-r--r--src/syntax.c6
-rw-r--r--src/uri.c10
-rw-r--r--src/world.c2
-rw-r--r--src/writer.c13
11 files changed, 112 insertions, 2 deletions
diff --git a/src/byte_source.c b/src/byte_source.c
index 397ea6d7..7a839875 100644
--- a/src/byte_source.c
+++ b/src/byte_source.c
@@ -8,6 +8,7 @@
#include "serd/node.h"
#include "serd/string_view.h"
+#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
@@ -46,7 +47,10 @@ serd_byte_source_open_source(SerdByteSource* const source,
const SerdNode* const name,
const size_t page_size)
{
+ assert(read_func);
+ assert(error_func);
assert(page_size > 0);
+
memset(source, '\0', sizeof(*source));
source->read_func = read_func;
source->error_func = error_func;
diff --git a/src/caret.c b/src/caret.c
index 1b0de444..02d5b94b 100644
--- a/src/caret.c
+++ b/src/caret.c
@@ -5,6 +5,7 @@
#include "serd/caret.h"
+#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@@ -14,6 +15,8 @@ serd_caret_new(const SerdNode* const document,
const unsigned line,
const unsigned column)
{
+ assert(document);
+
SerdCaret* caret = (SerdCaret*)malloc(sizeof(SerdCaret));
if (caret) {
@@ -53,17 +56,22 @@ serd_caret_equals(const SerdCaret* const l, const SerdCaret* const r)
const SerdNode*
serd_caret_document(const SerdCaret* const caret)
{
+ assert(caret);
+ assert(caret->document);
+
return caret->document;
}
unsigned
serd_caret_line(const SerdCaret* const caret)
{
+ assert(caret);
return caret->line;
}
unsigned
serd_caret_column(const SerdCaret* const caret)
{
+ assert(caret);
return caret->col;
}
diff --git a/src/env.c b/src/env.c
index 8f37ffa4..b0f652c3 100644
--- a/src/env.c
+++ b/src/env.c
@@ -115,6 +115,8 @@ serd_env_base_uri(const SerdEnv* const env)
SerdStatus
serd_env_set_base_uri(SerdEnv* const env, const SerdStringView uri)
{
+ assert(env);
+
if (!uri.length) {
serd_node_free(env->base_uri_node);
env->base_uri_node = NULL;
@@ -179,6 +181,8 @@ serd_env_set_prefix(SerdEnv* const env,
const SerdStringView name,
const SerdStringView uri)
{
+ assert(env);
+
if (serd_uri_string_has_scheme(uri.data)) {
// Set prefix to absolute URI
serd_env_add(env, name, uri);
@@ -323,6 +327,9 @@ serd_env_expand(const SerdEnv* env, const SerdNode* node)
SerdStatus
serd_env_write_prefixes(const SerdEnv* const env, const SerdSink* const sink)
{
+ assert(env);
+ assert(sink);
+
SerdStatus st = SERD_SUCCESS;
for (size_t i = 0; !st && i < env->n_prefixes; ++i) {
diff --git a/src/node.c b/src/node.c
index a5e2a7fa..ba2490bb 100644
--- a/src/node.c
+++ b/src/node.c
@@ -341,6 +341,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);
@@ -350,6 +352,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);
@@ -359,6 +363,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);
@@ -368,6 +374,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);
@@ -751,24 +759,32 @@ serd_new_base64(const void* buf, size_t size, const SerdNode* 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;
@@ -777,6 +793,8 @@ serd_node_string_view(const SerdNode* const node)
SERD_PURE_FUNC 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;
}
@@ -784,7 +802,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;
}
@@ -796,7 +816,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;
}
diff --git a/src/reader.c b/src/reader.c
index 00af2095..73ddc95f 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -15,6 +15,7 @@
#include "serd/string_view.h"
#include "serd/uri.h"
+#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
@@ -149,6 +150,8 @@ emit_statement(SerdReader* const reader,
SerdStatus
serd_reader_read_document(SerdReader* const reader)
{
+ assert(reader);
+
if (!reader->source.prepared) {
SerdStatus st = serd_reader_prepare(reader);
if (st) {
@@ -166,6 +169,9 @@ serd_reader_new(SerdWorld* const world,
const SerdReaderFlags flags,
const SerdSink* const sink)
{
+ assert(world);
+ assert(sink);
+
const size_t stack_size = world->limits.reader_stack_size;
if (stack_size < 3 * sizeof(SerdNode) + 192 + serd_node_align) {
return NULL;
@@ -212,6 +218,8 @@ serd_reader_free(SerdReader* const reader)
void
serd_reader_add_blank_prefix(SerdReader* const reader, const char* const prefix)
{
+ assert(reader);
+
free(reader->bprefix);
reader->bprefix_len = 0;
reader->bprefix = NULL;
@@ -249,6 +257,8 @@ serd_reader_start_stream(SerdReader* const reader,
const SerdNode* const name,
const size_t page_size)
{
+ assert(reader);
+
return serd_byte_source_open_source(
&reader->source, read_func, error_func, NULL, stream, name, page_size);
}
@@ -305,6 +315,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);
@@ -325,5 +337,7 @@ serd_reader_read_chunk(SerdReader* const reader)
SerdStatus
serd_reader_finish(SerdReader* const reader)
{
+ assert(reader);
+
return serd_byte_source_close(&reader->source);
}
diff --git a/src/sink.c b/src/sink.c
index 8343a62a..adf85b08 100644
--- a/src/sink.c
+++ b/src/sink.c
@@ -42,12 +42,18 @@ serd_sink_free(SerdSink* sink)
SerdStatus
serd_sink_write_event(const SerdSink* sink, const SerdEvent* event)
{
+ assert(sink);
+ assert(event);
+
return sink->on_event ? sink->on_event(sink->handle, event) : SERD_SUCCESS;
}
SerdStatus
serd_sink_write_base(const SerdSink* sink, const SerdNode* uri)
{
+ assert(sink);
+ assert(uri);
+
const SerdBaseEvent ev = {SERD_BASE, uri};
return sink->on_event ? sink->on_event(sink->handle, (const SerdEvent*)&ev)
@@ -59,6 +65,10 @@ serd_sink_write_prefix(const SerdSink* sink,
const SerdNode* name,
const SerdNode* uri)
{
+ assert(sink);
+ assert(name);
+ assert(uri);
+
const SerdPrefixEvent ev = {SERD_PREFIX, name, uri};
return sink->on_event ? sink->on_event(sink->handle, (const SerdEvent*)&ev)
@@ -70,6 +80,9 @@ serd_sink_write_statement(const SerdSink* sink,
const SerdStatementFlags flags,
const SerdStatement* statement)
{
+ assert(sink);
+ assert(statement);
+
const SerdStatementEvent statement_ev = {SERD_STATEMENT, flags, statement};
SerdEvent ev = {SERD_STATEMENT};
ev.statement = statement_ev;
@@ -97,6 +110,9 @@ serd_sink_write(const SerdSink* sink,
SerdStatus
serd_sink_write_end(const SerdSink* sink, const SerdNode* node)
{
+ assert(sink);
+ assert(node);
+
const SerdEndEvent ev = {SERD_END, node};
return sink->on_event ? sink->on_event(sink->handle, (const SerdEvent*)&ev)
diff --git a/src/statement.c b/src/statement.c
index 430194b7..434d41d4 100644
--- a/src/statement.c
+++ b/src/statement.c
@@ -85,36 +85,42 @@ serd_statement_free(SerdStatement* const statement)
const SerdNode*
serd_statement_node(const SerdStatement* const statement, const SerdField field)
{
+ assert(statement);
return statement->nodes[field];
}
const SerdNode*
serd_statement_subject(const SerdStatement* const statement)
{
+ assert(statement);
return statement->nodes[SERD_SUBJECT];
}
const SerdNode*
serd_statement_predicate(const SerdStatement* const statement)
{
+ assert(statement);
return statement->nodes[SERD_PREDICATE];
}
const SerdNode*
serd_statement_object(const SerdStatement* const statement)
{
+ assert(statement);
return statement->nodes[SERD_OBJECT];
}
const SerdNode*
serd_statement_graph(const SerdStatement* const statement)
{
+ assert(statement);
return statement->nodes[SERD_GRAPH];
}
const SerdCaret*
serd_statement_caret(const SerdStatement* const statement)
{
+ assert(statement);
return statement->caret;
}
@@ -135,6 +141,8 @@ serd_statement_matches(const SerdStatement* const statement,
const SerdNode* const object,
const SerdNode* const graph)
{
+ assert(statement);
+
return (serd_node_pattern_match(statement->nodes[0], subject) &&
serd_node_pattern_match(statement->nodes[1], predicate) &&
serd_node_pattern_match(statement->nodes[2], object) &&
diff --git a/src/syntax.c b/src/syntax.c
index d75865b1..6b95446d 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -5,6 +5,7 @@
#include "serd/syntax.h"
+#include <assert.h>
#include <stdbool.h>
#include <string.h>
@@ -25,6 +26,8 @@ static const Syntax syntaxes[] = {
SerdSyntax
serd_syntax_by_name(const char* const name)
{
+ assert(name);
+
const size_t len = strlen(name);
if (len) {
for (const Syntax* s = syntaxes; s->name; ++s) {
@@ -33,12 +36,15 @@ serd_syntax_by_name(const char* const name)
}
}
}
+
return SERD_SYNTAX_EMPTY;
}
SerdSyntax
serd_guess_syntax(const char* const filename)
{
+ assert(filename);
+
const char* ext = strrchr(filename, '.');
if (ext && ext[1]) {
const size_t ext_len = strlen(ext);
diff --git a/src/uri.c b/src/uri.c
index aed669c8..bc9d9fad 100644
--- a/src/uri.c
+++ b/src/uri.c
@@ -9,6 +9,7 @@
#include "serd/string_view.h"
#include "serd/uri.h"
+#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -18,6 +19,8 @@
char*
serd_parse_file_uri(const char* const uri, char** const hostname)
{
+ assert(uri);
+
const char* path = uri;
if (hostname) {
*hostname = NULL;
@@ -71,6 +74,8 @@ serd_parse_file_uri(const char* const uri, char** const hostname)
bool
serd_uri_string_has_scheme(const char* const string)
{
+ assert(string);
+
if (is_alpha(string[0])) {
for (size_t i = 1; string[i]; ++i) {
if (!is_uri_scheme_char(string[i])) {
@@ -89,6 +94,8 @@ serd_uri_string_has_scheme(const char* const string)
SerdURIView
serd_parse_uri(const char* const string)
{
+ assert(string);
+
SerdURIView result = SERD_URI_NULL;
const char* ptr = string;
@@ -440,6 +447,9 @@ serd_write_uri(const SerdURIView uri,
const SerdWriteFunc sink,
void* const stream)
{
+ assert(sink);
+ assert(stream);
+
size_t len = 0;
if (uri.scheme.data) {
diff --git a/src/world.c b/src/world.c
index dcf0b6af..386d7a55 100644
--- a/src/world.c
+++ b/src/world.c
@@ -139,6 +139,8 @@ serd_world_get_blank(SerdWorld* const world)
{
#define BLANK_CHARS 12
+ assert(world);
+
char* buf = serd_node_buffer(world->blank_node);
memset(buf, 0, BLANK_CHARS + 1);
diff --git a/src/writer.c b/src/writer.c
index dc41d522..7201c976 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -1330,6 +1330,8 @@ serd_writer_on_event(SerdWriter* writer, const SerdEvent* event)
SerdStatus
serd_writer_finish(SerdWriter* writer)
{
+ assert(writer);
+
const SerdStatus st0 = terminate_context(writer);
const SerdStatus st1 = serd_byte_sink_flush(&writer->byte_sink);
@@ -1347,6 +1349,10 @@ serd_writer_new(SerdWorld* world,
SerdWriteFunc ssink,
void* stream)
{
+ assert(world);
+ assert(env);
+ assert(ssink);
+
const size_t max_depth = world->limits.writer_max_depth;
const WriteContext context = WRITE_CONTEXT_NULL;
SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter));
@@ -1375,6 +1381,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;
@@ -1390,6 +1398,8 @@ serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix)
SERD_NODISCARD static SerdStatus
serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri)
{
+ assert(writer);
+
if (uri && serd_node_type(uri) != SERD_URI) {
return SERD_BAD_ARG;
}
@@ -1419,6 +1429,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;
@@ -1476,6 +1488,7 @@ serd_writer_free(SerdWriter* writer)
const SerdSink*
serd_writer_sink(SerdWriter* writer)
{
+ assert(writer);
return &writer->iface;
}