diff options
author | David Robillard <d@drobilla.net> | 2021-02-20 16:50:11 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-08 23:23:05 -0500 |
commit | 9cf28251f67e91a97bb75a74ad1ed23bac609b0d (patch) | |
tree | 947bf6128615d4bd0c801d4ebe2123b2ebd95eb2 /src | |
parent | a0ff18dcc526779e482a4a8e7741fc407c6940ae (diff) | |
download | serd-9cf28251f67e91a97bb75a74ad1ed23bac609b0d.tar.gz serd-9cf28251f67e91a97bb75a74ad1ed23bac609b0d.tar.bz2 serd-9cf28251f67e91a97bb75a74ad1ed23bac609b0d.zip |
Zero node padding before passing to reader sinks
Diffstat (limited to 'src')
-rw-r--r-- | src/n3.c | 5 | ||||
-rw-r--r-- | src/node.c | 30 | ||||
-rw-r--r-- | src/node.h | 3 | ||||
-rw-r--r-- | src/reader.c | 4 | ||||
-rw-r--r-- | src/stack.h | 1 |
5 files changed, 41 insertions, 2 deletions
@@ -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) { @@ -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; } @@ -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; } |