From fa593a6c62f47952069dffd644d0e5e777d0b5d1 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 26 May 2018 11:49:43 +0200 Subject: Add debug checks for node padding --- src/node.c | 44 ++++++++++++++++++++++++++++++++++++-------- src/static_nodes.h | 2 +- 2 files changed, 37 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/node.c b/src/node.c index ed113bbd..ed962150 100644 --- a/src/node.c +++ b/src/node.c @@ -75,6 +75,23 @@ serd_node_maybe_get_meta_c(const SerdNode* node) : NULL; } +static void +serd_node_check_padding(const SerdNode* node) +{ + (void)node; +#ifndef NDEBUG + if (node) { + 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'); + } + + serd_node_check_padding(serd_node_maybe_get_meta_c(node)); + } +#endif +} + static SERD_PURE_FUNC size_t serd_node_total_size(const SerdNode* node) @@ -128,6 +145,10 @@ serd_node_zero_pad(SerdNode* node) if (node->flags & (SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE)) { serd_node_zero_pad(serd_node_meta(node)); } + + if (node->flags & (SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE)) { + serd_node_zero_pad(serd_node_meta(node)); + } } SerdNode* @@ -143,6 +164,8 @@ serd_new_simple_node(SerdNodeType type, const SerdStringView str) memcpy(serd_node_buffer(node), str.buf, n_bytes); node->n_bytes = n_bytes; + + serd_node_check_padding(node); return node; } @@ -156,6 +179,7 @@ serd_new_string(const SerdStringView str) memcpy(serd_node_buffer(node), str.buf, str.len); node->n_bytes = n_bytes; + serd_node_check_padding(node); return node; } @@ -185,6 +209,7 @@ serd_new_literal(const SerdStringView str, lang_node->type = SERD_LITERAL; lang_node->n_bytes = lang.len; memcpy(serd_node_buffer(lang_node), lang.buf, lang.len); + serd_node_check_padding(lang_node); } else if (datatype_uri.len) { const size_t total_len = len + sizeof(SerdNode) + datatype_uri.len; @@ -197,6 +222,7 @@ serd_new_literal(const SerdStringView str, datatype_node->type = SERD_URI; datatype_node->n_bytes = datatype_uri.len; memcpy(serd_node_buffer(datatype_node), datatype_uri.buf, datatype_uri.len); + serd_node_check_padding(datatype_node); } else { node = serd_node_malloc(n_bytes, flags, SERD_LITERAL); @@ -204,6 +230,7 @@ serd_new_literal(const SerdStringView str, node->n_bytes = n_bytes; } + serd_node_check_padding(node); return node; } @@ -227,14 +254,8 @@ 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); + SerdNode* copy = (SerdNode*)calloc(1, size + 3); + memcpy(copy, node, size); return copy; } @@ -299,6 +320,7 @@ serd_new_parsed_uri(const SerdURIView uri) serd_node_buffer(node)[actual_len] = '\0'; node->n_bytes = actual_len; + serd_node_check_padding(node); return node; } @@ -314,6 +336,7 @@ serd_new_from_uri(const SerdURIView uri, const SerdURIView base) serd_node_buffer(node)[actual_len] = '\0'; node->n_bytes = actual_len; + serd_node_check_padding(node); return node; } @@ -333,6 +356,7 @@ serd_new_resolved_uri(const SerdStringView string, const SerdURIView base) return NULL; } + serd_node_check_padding(result); return result; } @@ -415,6 +439,7 @@ serd_new_file_uri(const SerdStringView path, const SerdStringView hostname) serd_new_uri(SERD_STRING_VIEW((const char*)buffer.buf, buffer.len - 1)); free(buffer.buf); + serd_node_check_padding(node); return node; } @@ -484,6 +509,7 @@ serd_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype) } memcpy(serd_node_meta(node), type, type_len); + serd_node_check_padding(node); return node; } @@ -514,6 +540,7 @@ serd_new_integer(int64_t i, const SerdNode* datatype) } while ((abs_i /= 10) > 0); memcpy(serd_node_meta(node), type, type_len); + serd_node_check_padding(node); return node; } @@ -542,6 +569,7 @@ serd_new_blob(const void* buf, node->n_bytes = len; memcpy(serd_node_meta(node), type, type_len); + serd_node_check_padding(node); return node; } diff --git a/src/static_nodes.h b/src/static_nodes.h index 1b1fc062..18950444 100644 --- a/src/static_nodes.h +++ b/src/static_nodes.h @@ -24,7 +24,7 @@ typedef struct StaticNode { SerdNode node; - char buf[sizeof(NS_XSD "base64Binary")]; + char buf[sizeof(NS_XSD "base64Binary") + sizeof(SerdNode)]; } StaticNode; #define DEFINE_XSD_NODE(name) \ -- cgit v1.2.1