From 27249b15a561221ae693a6f26e817eb65d19404d Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 28 Oct 2018 16:57:18 +0100 Subject: Ensure serd_new_resolved_uri returns an absolute URI or NULL --- src/env.c | 5 ++--- src/node.c | 16 ++++++++++++---- tests/serd_test.c | 5 +++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/env.c b/src/env.c index 5a7c04fb..00585426 100644 --- a/src/env.c +++ b/src/env.c @@ -120,11 +120,10 @@ serd_env_set_base_uri(SerdEnv* env, SerdNode* base_uri_node = serd_new_resolved_uri_i( serd_node_get_string(uri), &env->base_uri); - SerdURI base_uri; - serd_uri_parse(serd_node_get_string(base_uri_node), &base_uri); - if (base_uri_node) { // Replace the current base URI + SerdURI base_uri; + serd_uri_parse(serd_node_get_string(base_uri_node), &base_uri); serd_node_free(env->base_uri_node); env->base_uri_node = base_uri_node; env->base_uri = base_uri; diff --git a/src/node.c b/src/node.c index dc19c2c6..1f22bf4f 100644 --- a/src/node.c +++ b/src/node.c @@ -357,14 +357,22 @@ serd_node_resolve(const SerdNode* node, const SerdNode* base) SerdNode* serd_new_resolved_uri_i(const char* str, const SerdURI* base) { + SerdNode* result = NULL; if (!str || str[0] == '\0') { // Empty URI => Base URI, or nothing if no base is given - return base ? serd_new_from_uri(base, NULL) : NULL; + result = base ? serd_new_from_uri(base, NULL) : NULL; + } else { + SerdURI uri; + serd_uri_parse(str, &uri); + result = serd_new_from_uri(&uri, base); } - SerdURI uri; - serd_uri_parse(str, &uri); - return serd_new_from_uri(&uri, base); + if (!serd_uri_string_has_scheme(serd_node_get_string(result))) { + serd_node_free(result); + return NULL; + } + + return result; } static inline bool diff --git a/tests/serd_test.c b/tests/serd_test.c index 29f48cef..5ce94905 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -476,6 +476,7 @@ main(void) SerdNode* eg = serd_new_uri("http://example.org/"); SerdNode* foo_u = serd_new_uri("http://example.org/foo"); + SerdNode* empty = serd_new_uri(""); SerdNode* foo_c = serd_new_curie("eg.2:foo"); SerdNode* b = serd_new_curie("invalid"); SerdNode* pre = serd_new_curie("eg.2"); @@ -490,6 +491,10 @@ main(void) FAIL("Successfully set NULL base URI\n"); } + if (!serd_env_set_base_uri(env, empty)) { + FAIL("Successfully set empty base URI\n"); + } + if (!serd_env_set_base_uri(env, hello)) { FAILF("Set base URI to %s\n", serd_node_get_string(hello)); } -- cgit v1.2.1