diff options
author | David Robillard <d@drobilla.net> | 2021-02-20 16:50:11 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | b16765b4830587c42db16587eda86f1eebb9a352 (patch) | |
tree | a1cc261786bbf0e06f59bad76a9fa11c5646d8d9 /src/node.c | |
parent | 8771f4611a37efbe2841abe6db06c662490bdd88 (diff) | |
download | serd-b16765b4830587c42db16587eda86f1eebb9a352.tar.gz serd-b16765b4830587c42db16587eda86f1eebb9a352.tar.bz2 serd-b16765b4830587c42db16587eda86f1eebb9a352.zip |
Zero node padding before passing to reader sinks
Diffstat (limited to 'src/node.c')
-rw-r--r-- | src/node.c | 30 |
1 files changed, 29 insertions, 1 deletions
@@ -152,6 +152,27 @@ serd_node_set(SerdNode** const dst, const SerdNode* const 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->length; + const size_t padded_size = serd_node_pad_size(size); + + 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_token(const SerdNodeType type, const SerdStringView str) { @@ -275,7 +296,14 @@ serd_node_copy(const SerdNode* node) } const size_t size = serd_node_total_size(node); - SerdNode* copy = (SerdNode*)serd_calloc_aligned(serd_node_align, size); +#ifndef NDEBUG + const size_t unpadded_size = node->length; + const size_t padded_size = serd_node_pad_size(node->length); + 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*)serd_calloc_aligned(serd_node_align, size); memcpy(copy, node, size); return copy; } |