aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-06-20 20:58:27 +0200
committerDavid Robillard <d@drobilla.net>2020-10-27 13:13:59 +0100
commit44afc616ada1315bfcc1f2fb6bd82656ced8b2f5 (patch)
treebff66abc7b8df8bd0a4e081e21b2c388f6635b95
parentf9c7cac95abb7c5430c9ecd6f0a7220c3f8b5205 (diff)
downloadserd-44afc616ada1315bfcc1f2fb6bd82656ced8b2f5.tar.gz
serd-44afc616ada1315bfcc1f2fb6bd82656ced8b2f5.tar.bz2
serd-44afc616ada1315bfcc1f2fb6bd82656ced8b2f5.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--src/env.c10
-rw-r--r--tests/env_test.c4
2 files changed, 13 insertions, 1 deletions
diff --git a/src/env.c b/src/env.c
index 5996b221..11655134 100644
--- a/src/env.c
+++ b/src/env.c
@@ -108,7 +108,7 @@ serd_env_get_parsed_base_uri(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
@@ -204,6 +204,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) {
@@ -263,6 +267,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/tests/env_test.c b/tests/env_test.c
index cc04366d..cff8e3cf 100644
--- a/tests/env_test.c
+++ b/tests/env_test.c
@@ -46,6 +46,10 @@ test_env(void)
SerdEnv* env = serd_env_new(NULL);
serd_env_set_prefix(env, pre, eg);
+ assert(!serd_env_base_uri(NULL));
+ assert(!serd_env_expand(NULL, NULL));
+ assert(!serd_env_qualify(NULL, NULL));
+
assert(!serd_env_base_uri(env));
assert(serd_env_set_base_uri(env, NULL));
assert(serd_env_set_base_uri(env, empty));