aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-05-26 11:49:43 +0200
committerDavid Robillard <d@drobilla.net>2019-12-19 20:55:11 -0500
commit11bcffbf7728063edc8dcfcbdfba6e9b37e4c160 (patch)
tree0de98a2ccfa6d4faff35ef538986d13ca262415c
parent8892afbf7aca85acb37fdc5fda2e6de333d95bb1 (diff)
downloadserd-11bcffbf7728063edc8dcfcbdfba6e9b37e4c160.tar.gz
serd-11bcffbf7728063edc8dcfcbdfba6e9b37e4c160.tar.bz2
serd-11bcffbf7728063edc8dcfcbdfba6e9b37e4c160.zip
Add debug checks for node padding
-rw-r--r--src/node.c42
-rw-r--r--src/static_nodes.h2
2 files changed, 36 insertions, 8 deletions
diff --git a/src/node.c b/src/node.c
index 0211996a..b8a251c1 100644
--- a/src/node.c
+++ b/src/node.c
@@ -73,6 +73,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)
{
@@ -120,6 +137,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;
}
@@ -135,6 +153,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;
}
@@ -158,6 +177,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_get_type(datatype) != SERD_URI)) {
@@ -181,6 +202,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));
@@ -191,12 +213,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;
}
@@ -237,6 +261,10 @@ serd_node_zero_pad(SerdNode* node)
if (node->flags & (SERD_HAS_DATATYPE|SERD_HAS_LANGUAGE)) {
serd_node_zero_pad(serd_node_get_meta(node));
}
+
+ if (node->flags & (SERD_HAS_DATATYPE|SERD_HAS_LANGUAGE)) {
+ serd_node_zero_pad(serd_node_get_meta(node));
+ }
}
SerdNode*
@@ -247,13 +275,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;
@@ -391,6 +413,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;
}
@@ -410,6 +433,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;
}
@@ -440,6 +464,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;
}
@@ -509,6 +534,7 @@ serd_node_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype)
}
memcpy(serd_node_get_meta(node), type, type_len);
+ serd_node_check_padding(node);
return node;
}
@@ -540,6 +566,7 @@ serd_node_new_integer(int64_t i, const SerdNode* datatype)
} while ((abs_i /= 10) > 0);
memcpy(serd_node_get_meta(node), type, type_len);
+ serd_node_check_padding(node);
return node;
}
@@ -567,6 +594,7 @@ serd_node_new_blob(const void* buf,
node->n_bytes = len;
memcpy(serd_node_get_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 7dd6e140..4ab44066 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) \