From 44afc616ada1315bfcc1f2fb6bd82656ced8b2f5 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 20 Jun 2020 20:58:27 +0200 Subject: 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. --- src/env.c | 10 +++++++++- tests/env_test.c | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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)); -- cgit v1.2.1