From 85a7ec2b8e11085b74c3a5966753a0b43a264c8a Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 11 Apr 2014 18:49:32 +0000 Subject: Minor hardening. git-svn-id: http://svn.drobilla.net/serd/trunk@459 490d8e77-9747-427b-9fa3-0b8f29cee8a0 --- src/env.c | 12 ++++++------ src/node.c | 10 +++++++--- src/uri.c | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/env.c b/src/env.c index c37b6b6b..77682d6d 100644 --- a/src/env.c +++ b/src/env.c @@ -36,12 +36,8 @@ SERD_API SerdEnv* serd_env_new(const SerdNode* base_uri) { - SerdEnv* env = (SerdEnv*)malloc(sizeof(struct SerdEnvImpl)); - env->prefixes = NULL; - env->n_prefixes = 0; - env->base_uri_node = SERD_NODE_NULL; - env->base_uri = SERD_URI_NULL; - if (base_uri) { + SerdEnv* env = (SerdEnv*)calloc(sizeof(struct SerdEnvImpl), 1); + if (env && base_uri) { serd_env_set_base_uri(env, base_uri); } return env; @@ -76,6 +72,10 @@ SerdStatus serd_env_set_base_uri(SerdEnv* env, const SerdNode* uri_node) { + if (!env || !uri_node) { + return SERD_ERR_BAD_ARG; + } + // Resolve base URI and create a new node and URI for it SerdURI base_uri; SerdNode base_uri_node = serd_node_new_uri_from_node( diff --git a/src/node.c b/src/node.c index 065ff875..a5305474 100644 --- a/src/node.c +++ b/src/node.c @@ -102,7 +102,7 @@ serd_node_new_uri_from_node(const SerdNode* uri_node, const SerdURI* base, SerdURI* out) { - return (uri_node->type == SERD_URI) + return (uri_node->type == SERD_URI && uri_node->buf) ? serd_node_new_uri_from_string(uri_node->buf, base, out) : SERD_NODE_NULL; } @@ -114,7 +114,11 @@ serd_node_new_uri_from_string(const uint8_t* str, SerdURI* out) { if (!str || str[0] == '\0') { - return serd_node_new_uri(base, NULL, out); // Empty URI => Base URI + if (base) { + return serd_node_new_uri(base, NULL, out); // Empty URI => Base + } else { + return SERD_NODE_NULL; // Nonsense + } } SerdURI uri; serd_uri_parse(str, &uri); @@ -340,7 +344,7 @@ SERD_API void serd_node_free(SerdNode* node) { - if (node->buf) { + if (node && node->buf) { free((uint8_t*)node->buf); node->buf = NULL; } diff --git a/src/uri.c b/src/uri.c index 8f3bb997..6f7380b0 100644 --- a/src/uri.c +++ b/src/uri.c @@ -100,7 +100,7 @@ bool serd_uri_string_has_scheme(const uint8_t* utf8) { // RFC3986: scheme ::= ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) - if (!is_alpha(utf8[0])) { + if (!utf8 || !is_alpha(utf8[0])) { return false; // Invalid scheme initial character, URI is relative } for (uint8_t c; (c = *++utf8) != '\0';) { -- cgit v1.2.1