From 6c75e06ccf71c5ece8f32d5b78c01867fceb468d 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 | 42 +++++++++++++++++++++++++++++++++++------- src/static_nodes.h | 2 +- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/node.c b/src/node.c index 8800bac0..7dc8ed62 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 size_t serd_node_total_size(const SerdNode* node) { @@ -122,6 +139,7 @@ serd_node_new_simple_node(SerdType type, const char* str, const size_t len) SerdNode* node = serd_node_malloc(len, 0, type); memcpy(serd_node_buffer(node), str, len); node->n_bytes = len; + serd_node_check_padding(node); return node; } @@ -137,6 +155,7 @@ serd_node_new_string(const char* str) SerdNode* node = serd_node_malloc(n_bytes, flags, SERD_LITERAL); memcpy(serd_node_buffer(node), str, n_bytes); node->n_bytes = n_bytes; + serd_node_check_padding(node); return node; } @@ -160,6 +179,8 @@ serd_node_new_literal(const char* str, const SerdNode* datatype, const char* lang) { + serd_node_check_padding(datatype); + if (!str || (lang && datatype && strcmp(serd_node_buffer_c(datatype), NS_RDF "langString")) || (datatype && serd_node_type(datatype) != SERD_URI)) { @@ -183,6 +204,7 @@ serd_node_new_literal(const char* str, lang_node->type = SERD_LITERAL; lang_node->n_bytes = lang_len; memcpy(serd_node_buffer(lang_node), lang, lang_len); + serd_node_check_padding(lang_node); } else if (datatype) { flags |= SERD_HAS_DATATYPE; const size_t datatype_len = strlen(serd_node_buffer_c(datatype)); @@ -193,12 +215,14 @@ serd_node_new_literal(const char* str, SerdNode* datatype_node = node + 1 + (len / serd_node_align); memcpy(datatype_node, datatype, sizeof(SerdNode) + datatype_len); + serd_node_check_padding(datatype_node); } else { node = serd_node_malloc(n_bytes, flags, SERD_LITERAL); memcpy(serd_node_buffer(node), str, n_bytes); node->n_bytes = n_bytes; } + serd_node_check_padding(node); return node; } @@ -239,6 +263,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* @@ -249,13 +277,7 @@ 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 + serd_node_check_padding(node); SerdNode* copy = (SerdNode*)calloc(1, size + 3); memcpy(copy, node, size); return copy; @@ -396,6 +418,7 @@ serd_node_new_file_uri(const char* path, const char* hostname, bool escape) SerdNode* node = serd_node_new_uri((const char*)buffer.buf); free(buffer.buf); + serd_node_check_padding(node); return node; } @@ -415,6 +438,7 @@ serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base) serd_node_buffer(node)[actual_len] = '\0'; node->n_bytes = actual_len; + serd_node_check_padding(node); return node; } @@ -445,6 +469,7 @@ serd_node_new_relative_uri(const char* str, serd_node_buffer(node)[actual_len] = '\0'; node->n_bytes = actual_len; + serd_node_check_padding(node); return node; } @@ -514,6 +539,7 @@ serd_node_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; } @@ -545,6 +571,7 @@ serd_node_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; } @@ -573,6 +600,7 @@ serd_node_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 62e38c91..430fa58c 100644 --- a/src/static_nodes.h +++ b/src/static_nodes.h @@ -22,7 +22,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