aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-06-20 20:58:27 +0200
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:05 -0500
commit40a83074705976ea59f0e1a5d06a4e5c7705472c (patch)
tree942f70ec83597833ed71a9aa6bb507d1b473bc15
parentd4a5e5c0581aa25ac91e1d3d7d5900f0d7202ed9 (diff)
downloadserd-40a83074705976ea59f0e1a5d06a4e5c7705472c.tar.gz
serd-40a83074705976ea59f0e1a5d06a4e5c7705472c.tar.bz2
serd-40a83074705976ea59f0e1a5d06a4e5c7705472c.zip
Make const Env functions tolerant of NULL
This is convenient in places where you don't necessarily need an Env, since in this case these methods work the same as if with an empty Env.
-rw-r--r--include/serd/serd.h8
-rw-r--r--src/env.c10
-rw-r--r--test/test_env.c4
3 files changed, 17 insertions, 5 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h
index 79353e50..cfdc8693 100644
--- a/include/serd/serd.h
+++ b/include/serd/serd.h
@@ -1030,7 +1030,7 @@ serd_env_free(SerdEnv* SERD_NULLABLE env);
/// Get the current base URI
SERD_PURE_API
const SerdNode* SERD_NULLABLE
-serd_env_base_uri(const SerdEnv* SERD_NONNULL env);
+serd_env_base_uri(const SerdEnv* SERD_NULLABLE env);
/// Set the current base URI
SERD_API
@@ -1058,7 +1058,7 @@ serd_env_set_prefix(SerdEnv* SERD_NONNULL env,
*/
SERD_API
SerdNode* SERD_ALLOCATED
-serd_env_qualify(const SerdEnv* SERD_NONNULL env,
+serd_env_qualify(const SerdEnv* SERD_NULLABLE env,
const SerdNode* SERD_NONNULL uri);
/**
@@ -1072,8 +1072,8 @@ serd_env_qualify(const SerdEnv* SERD_NONNULL env,
*/
SERD_API
SerdNode* SERD_ALLOCATED
-serd_env_expand(const SerdEnv* SERD_NONNULL env,
- const SerdNode* SERD_NONNULL node);
+serd_env_expand(const SerdEnv* SERD_NULLABLE env,
+ const SerdNode* SERD_NULLABLE node);
/// Write all prefixes in `env` to `sink`
SERD_API
diff --git a/src/env.c b/src/env.c
index 29df5286..49d2efa2 100644
--- a/src/env.c
+++ b/src/env.c
@@ -115,7 +115,7 @@ serd_env_base_uri_view(const SerdEnv* env)
const SerdNode*
serd_env_base_uri(const SerdEnv* env)
{
- return env->base_uri_node;
+ return env ? env->base_uri_node : NULL;
}
SerdStatus
@@ -202,6 +202,10 @@ serd_env_qualify_in_place(const SerdEnv* env,
const SerdNode** prefix,
SerdStringView* suffix)
{
+ if (!env) {
+ return false;
+ }
+
for (size_t i = 0; i < env->n_prefixes; ++i) {
const SerdNode* const prefix_uri = env->prefixes[i].uri;
if (uri->n_bytes >= prefix_uri->n_bytes) {
@@ -244,6 +248,10 @@ serd_env_expand_in_place(const SerdEnv* env,
SerdStringView* uri_prefix,
SerdStringView* uri_suffix)
{
+ if (!env) {
+ return SERD_ERR_BAD_CURIE;
+ }
+
const char* const str = serd_node_string(curie);
const char* const colon = (const char*)memchr(str, ':', curie->n_bytes + 1);
if (curie->type != SERD_CURIE || !colon) {
diff --git a/test/test_env.c b/test/test_env.c
index fcc45c53..651bf8c9 100644
--- a/test/test_env.c
+++ b/test/test_env.c
@@ -46,6 +46,10 @@ test_env(void)
serd_env_set_prefix(env, prefix, eg);
+ assert(!serd_env_base_uri(NULL));
+ assert(!serd_env_expand(NULL, NULL));
+ assert(!serd_env_qualify(NULL, foo_u));
+
assert(!serd_env_base_uri(env));
assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING()));
assert(!serd_env_base_uri(env));