aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/n3.c5
-rw-r--r--src/node.c30
-rw-r--r--src/node.h3
-rw-r--r--src/reader.c4
-rw-r--r--src/stack.h1
5 files changed, 41 insertions, 2 deletions
diff --git a/src/n3.c b/src/n3.c
index a5299c38..bf65b038 100644
--- a/src/n3.c
+++ b/src/n3.c
@@ -1178,7 +1178,7 @@ 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;
@@ -1408,6 +1408,7 @@ read_base(SerdReader* reader, bool sparql, bool token)
SerdNode* uri = NULL;
TRY(st, read_IRIREF(reader, &uri));
+ serd_node_zero_pad(uri);
TRY(st, serd_sink_write_base(reader->sink, uri));
read_ws_star(reader);
@@ -1448,6 +1449,8 @@ read_prefixID(SerdReader* reader, bool sparql, bool token)
SerdNode* uri = NULL;
TRY(st, read_IRIREF(reader, &uri));
+ serd_node_zero_pad(name);
+ serd_node_zero_pad(uri);
st = serd_sink_write_prefix(reader->sink, name, uri);
if (!sparql) {
diff --git a/src/node.c b/src/node.c
index 522a939a..ed113bbd 100644
--- a/src/node.c
+++ b/src/node.c
@@ -109,6 +109,27 @@ serd_node_set(SerdNode** dst, const SerdNode* src)
memcpy(*dst, src, size);
}
+/**
+ 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_new_simple_node(SerdNodeType type, const SerdStringView str)
{
@@ -206,7 +227,14 @@ serd_node_copy(const SerdNode* node)
}
const size_t size = serd_node_total_size(node);
- SerdNode* copy = (SerdNode*)calloc(1, size + 3);
+#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 5c6f790e..6bebdb6d 100644
--- a/src/node.h
+++ b/src/node.h
@@ -46,6 +46,9 @@ void
serd_node_set(SerdNode* SERD_NONNULL* SERD_NONNULL dst,
const SerdNode* SERD_NONNULL src);
+void
+serd_node_zero_pad(SerdNode* SERD_NONNULL node);
+
/// Create a new URI from a string, resolved against a base URI
SerdNode* SERD_ALLOCATED
serd_new_resolved_uri(SerdStringView string, SerdURIView base_uri);
diff --git a/src/reader.c b/src/reader.c
index 24240de8..f375be42 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -122,6 +122,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 = serd_sink_write(
reader->sink, *ctx.flags, ctx.subject, ctx.predicate, o, graph);
diff --git a/src/stack.h b/src/stack.h
index 4c53b4ae..62f3402b 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;
}