From 04084d0c36fc15179d195b2a8f759adebb609592 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 24 Jul 2021 21:08:10 -0400 Subject: Be precise about the meaning of node "length" and "size" --- src/n3.c | 5 +++-- src/node.c | 50 ++++++++++++++++++++++---------------------------- src/reader.c | 16 +++++++++------- src/reader.h | 4 ++-- 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/n3.c b/src/n3.c index ac46dc61..47ae2d32 100644 --- a/src/n3.c +++ b/src/n3.c @@ -1325,7 +1325,8 @@ read_collection(SerdReader* const reader, /* The order of node allocation here is necessarily not in stack order, so we create two nodes and recycle them throughout. */ SerdNode* n1 = - push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); + push_node_padded(reader, genid_length(reader), SERD_BLANK, "", 0); + SerdNode* node = n1; SerdNode* rest = 0; @@ -1349,7 +1350,7 @@ read_collection(SerdReader* const reader, rest = blank_id(reader); // First pass, push assert(rest); // Can't overflow since read_object() popped } else { - set_blank_id(reader, rest, genid_size(reader)); + set_blank_id(reader, rest, genid_length(reader) + 1); } } diff --git a/src/node.c b/src/node.c index 611a343e..9d9d6532 100644 --- a/src/node.c +++ b/src/node.c @@ -74,7 +74,7 @@ string_sink(const void* const buf, } static size_t -serd_node_pad_size(const size_t n_bytes) +serd_node_pad_length(const size_t n_bytes) { const size_t pad = sizeof(SerdNode) - (n_bytes + 2) % sizeof(SerdNode); const size_t size = n_bytes + 2 + pad; @@ -82,16 +82,16 @@ serd_node_pad_size(const size_t n_bytes) return size; } -static SerdNode* -serd_node_meta(SerdNode* const node) +static const SerdNode* +serd_node_meta_c(const SerdNode* const node) { - return node + 1 + (serd_node_pad_size(node->length) / sizeof(SerdNode)); + return node + 1 + (serd_node_pad_length(node->length) / sizeof(SerdNode)); } -static const SerdNode* -serd_node_meta_c(const SerdNode* const node) +static SerdNode* +serd_node_meta(SerdNode* const node) { - return node + 1 + (serd_node_pad_size(node->length) / sizeof(SerdNode)); + return node + 1 + (serd_node_pad_length(node->length) / sizeof(SerdNode)); } static const SerdNode* @@ -106,10 +106,9 @@ serd_node_check_padding(const SerdNode* node) (void)node; #ifndef NDEBUG if (node) { - const size_t unpadded_size = node->length; - const size_t padded_size = serd_node_pad_size(unpadded_size); - for (size_t i = 0; i < padded_size - unpadded_size; ++i) { - assert(serd_node_buffer_c(node)[unpadded_size + i] == '\0'); + const size_t padded_length = serd_node_pad_length(node->length); + for (size_t i = 0; i < padded_length - node->length; ++i) { + assert(serd_node_buffer_c(node)[node->length + i] == '\0'); } serd_node_check_padding(serd_node_maybe_get_meta_c(node)); @@ -120,7 +119,7 @@ serd_node_check_padding(const SerdNode* node) size_t serd_node_total_size(const SerdNode* const node) { - return node ? (sizeof(SerdNode) + serd_node_pad_size(node->length) + + return node ? (sizeof(SerdNode) + serd_node_pad_length(node->length) + serd_node_total_size(serd_node_maybe_get_meta_c(node))) : 0; } @@ -130,7 +129,7 @@ serd_node_malloc(const size_t length, const SerdNodeFlags flags, const SerdNodeType type) { - const size_t size = sizeof(SerdNode) + serd_node_pad_size(length); + const size_t size = sizeof(SerdNode) + serd_node_pad_length(length); SerdNode* node = (SerdNode*)serd_calloc_aligned(serd_node_align, size); node->length = 0; @@ -170,11 +169,10 @@ serd_node_set(SerdNode** const dst, const SerdNode* const src) 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); + char* buf = serd_node_buffer(node); + const size_t padded_length = serd_node_pad_length(node->length); - memset(buf + size, 0, padded_size - size); + memset(buf + node->length, 0, padded_length - node->length); if (node->flags & (SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE)) { serd_node_zero_pad(serd_node_meta(node)); @@ -237,7 +235,7 @@ serd_new_plain_literal_i(const SerdStringView str, flags |= SERD_HAS_LANGUAGE; - const size_t len = serd_node_pad_size(str.len); + const size_t len = serd_node_pad_length(str.len); const size_t total_len = len + sizeof(SerdNode) + lang.len; SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL); @@ -284,7 +282,7 @@ serd_new_typed_literal(const SerdStringView str, flags |= SERD_HAS_DATATYPE; - const size_t len = serd_node_pad_size(str.len); + const size_t len = serd_node_pad_length(str.len); const size_t total_len = len + sizeof(SerdNode) + datatype_uri.len; SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL); @@ -605,7 +603,7 @@ serd_new_custom_literal(const void* const user_data, } const size_t datatype_size = serd_node_total_size(datatype); - const size_t total_size = serd_node_pad_size(len + 1) + datatype_size; + const size_t total_size = serd_node_pad_length(len) + datatype_size; SerdNode* const node = serd_node_malloc( total_size, datatype ? SERD_HAS_DATATYPE : 0u, SERD_LITERAL); @@ -663,10 +661,8 @@ serd_new_decimal(const double d, const SerdNode* const datatype) assert(!r.status); // Allocate node with enough space for value and datatype URI - SerdNode* const node = - serd_node_malloc(serd_node_pad_size(r.count + 1) + type_size, - SERD_HAS_DATATYPE, - SERD_LITERAL); + SerdNode* const node = serd_node_malloc( + serd_node_pad_length(r.count) + type_size, SERD_HAS_DATATYPE, SERD_LITERAL); // Write string directly into node r = exess_write_decimal(d, r.count + 1, serd_node_buffer(node)); @@ -690,10 +686,8 @@ serd_new_integer(const int64_t i, const SerdNode* const datatype) assert(!r.status); // Allocate node with enough space for value and datatype URI - SerdNode* const node = - serd_node_malloc(serd_node_pad_size(r.count + 1) + type_size, - SERD_HAS_DATATYPE, - SERD_LITERAL); + SerdNode* const node = serd_node_malloc( + serd_node_pad_length(r.count) + type_size, SERD_HAS_DATATYPE, SERD_LITERAL); // Write string directly into node r = exess_write_long(i, r.count + 1, serd_node_buffer(node)); diff --git a/src/reader.c b/src/reader.c index 09e90076..f0b81e7c 100644 --- a/src/reader.c +++ b/src/reader.c @@ -57,9 +57,9 @@ set_blank_id(SerdReader* const reader, } size_t -genid_size(const SerdReader* const reader) +genid_length(const SerdReader* const reader) { - return reader->bprefix_len + 1 + 10 + 1; // + "b" + UINT32_MAX + \0 + return reader->bprefix_len + 10; // + "b" + UINT32_MAX } bool @@ -80,17 +80,19 @@ tolerate_status(const SerdReader* const reader, const SerdStatus status) SerdNode* blank_id(SerdReader* const reader) { - SerdNode* ref = - push_node_padded(reader, genid_size(reader), SERD_BLANK, "", 0); + SerdNode* const ref = + push_node_padded(reader, genid_length(reader), SERD_BLANK, "", 0); + if (ref) { - set_blank_id(reader, ref, genid_size(reader)); + set_blank_id(reader, ref, genid_length(reader) + 1); } + return ref; } SerdNode* push_node_padded(SerdReader* const reader, - const size_t maxlen, + const size_t max_length, const SerdNodeType type, const char* const str, const size_t length) @@ -103,7 +105,7 @@ push_node_padded(SerdReader* const reader, *terminator = 0; void* mem = serd_stack_push_aligned( - &reader->stack, sizeof(SerdNode) + maxlen + 1, sizeof(SerdNode)); + &reader->stack, sizeof(SerdNode) + max_length + 1, sizeof(SerdNode)); if (!mem) { return NULL; diff --git a/src/reader.h b/src/reader.h index f45b69eb..a8c154dd 100644 --- a/src/reader.h +++ b/src/reader.h @@ -67,7 +67,7 @@ r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...); SerdNode* push_node_padded(SerdReader* reader, - size_t maxlen, + size_t max_length, SerdNodeType type, const char* str, size_t length); @@ -80,7 +80,7 @@ push_node(SerdReader* reader, SERD_PURE_FUNC size_t -genid_size(const SerdReader* reader); +genid_length(const SerdReader* reader); SERD_PURE_FUNC bool -- cgit v1.2.1