aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/n3.c7
-rw-r--r--src/node.c28
-rw-r--r--src/node.h1
-rw-r--r--src/reader.c4
-rw-r--r--src/stack.h1
5 files changed, 39 insertions, 2 deletions
diff --git a/src/n3.c b/src/n3.c
index a0dce998..8c2bf450 100644
--- a/src/n3.c
+++ b/src/n3.c
@@ -1114,10 +1114,10 @@ read_object(SerdReader* reader, ReadContext* ctx, bool emit, bool* ate_dot)
o->flags = flags;
}
- if (!ret && emit && simple) {
+ if (!ret && emit && simple && o) {
ret = emit_statement(reader, *ctx, o);
} else if (!ret && !emit) {
- ctx->object = o;
+ ctx->object = o;
return SERD_SUCCESS;
}
@@ -1343,6 +1343,7 @@ read_base(SerdReader* reader, bool sparql, bool token)
SerdNode* uri = NULL;
TRY(st, read_IRIREF(reader, &uri));
if (reader->sink->base) {
+ serd_node_zero_pad(uri);
TRY(st, reader->sink->base(reader->sink->handle, uri));
}
@@ -1381,6 +1382,8 @@ read_prefixID(SerdReader* reader, bool sparql, bool token)
TRY(st, read_IRIREF(reader, &uri));
if (reader->sink->prefix) {
+ serd_node_zero_pad(name);
+ serd_node_zero_pad(uri);
st = reader->sink->prefix(reader->sink->handle, name, uri);
}
if (!sparql) {
diff --git a/src/node.c b/src/node.c
index 96afc075..8800bac0 100644
--- a/src/node.c
+++ b/src/node.c
@@ -220,6 +220,27 @@ serd_node_new_uri(const char* str)
return str ? serd_node_new_simple_node(SERD_URI, str, strlen(str)) : NULL;
}
+/**
+ Zero node padding.
+
+ This is used for nodes which live in re-used stack memory during reading,
+ which must be normalized before being passed to a sink so comparison will
+ work correctly.
+*/
+void
+serd_node_zero_pad(SerdNode* node)
+{
+ char* buf = serd_node_buffer(node);
+ const size_t size = node->n_bytes;
+ const size_t padded_size = serd_node_pad_size(node->n_bytes);
+
+ memset(buf + size, 0, padded_size - size);
+
+ if (node->flags & (SERD_HAS_DATATYPE|SERD_HAS_LANGUAGE)) {
+ serd_node_zero_pad(serd_node_meta(node));
+ }
+}
+
SerdNode*
serd_node_copy(const SerdNode* node)
{
@@ -228,6 +249,13 @@ serd_node_copy(const SerdNode* node)
}
const size_t size = serd_node_total_size(node);
+#ifndef NDEBUG
+ const size_t unpadded_size = node->n_bytes;
+ const size_t padded_size = serd_node_pad_size(node->n_bytes);
+ for (size_t i = 0; i < padded_size - unpadded_size; ++i) {
+ assert(serd_node_buffer_c(node)[unpadded_size + i] == '\0');
+ }
+#endif
SerdNode* copy = (SerdNode*)calloc(1, size + 3);
memcpy(copy, node, size);
return copy;
diff --git a/src/node.h b/src/node.h
index 8bc6083e..2a37217c 100644
--- a/src/node.h
+++ b/src/node.h
@@ -41,6 +41,7 @@ serd_node_buffer_c(const SerdNode* node)
SerdNode* serd_node_malloc(size_t n_bytes, SerdNodeFlags flags, SerdType type);
void serd_node_set(SerdNode** dst, const SerdNode* src);
+void serd_node_zero_pad(SerdNode* node);
SerdNode* serd_node_new_resolved_uri_i(const char* str, const SerdURI* base);
#endif // SERD_NODE_H
diff --git a/src/reader.c b/src/reader.c
index c3e2e268..065a6040 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -115,6 +115,10 @@ emit_statement(SerdReader* reader, ReadContext ctx, SerdNode* o)
graph = reader->default_graph;
}
+ /* Zero the pad of the object node on the top of the stack. Lower nodes
+ (subject and predicate) were already zeroed by subsequent pushes. */
+ serd_node_zero_pad(o);
+
const SerdStatus st =
!reader->sink->statement
? SERD_SUCCESS
diff --git a/src/stack.h b/src/stack.h
index cbbe6b18..c8716ebd 100644
--- a/src/stack.h
+++ b/src/stack.h
@@ -85,6 +85,7 @@ static inline void
serd_stack_pop_to(SerdStack* stack, size_t n_bytes)
{
assert(stack->size >= n_bytes);
+ memset(stack->buf + n_bytes, 0, stack->size - n_bytes);
stack->size = n_bytes;
}