From e5df3638d79c51c464d89e7c26d45d0c615af1ff Mon Sep 17 00:00:00 2001 From: Petr Tesarik Date: Thu, 17 May 2018 09:13:52 +0200 Subject: [PATCH] Add assert() calls to ensure consistent handling of the empty flag If a new block is created without the empty flag, make sure that its size is non-zero. When a block must not be empty, add the corresponding check. When block size is set to a value, ensure that this value is not zero. Signed-off-by: Petr Tesarik --- libhed/file.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libhed/file.c b/libhed/file.c index 10442ab..d1da9a1 100644 --- a/libhed/file.c +++ b/libhed/file.c @@ -481,6 +481,7 @@ new_virt_block(struct file_priv *file, hed_uoff_t pos, hed_uoff_t size, if (!new) return NULL; + assert(size != 0); new->t.maxoff = size - 1; new->phys_pos = pos; BDEBUG("Spawned new virtual block [%llx] at %llx\n", size, pos); @@ -497,6 +498,7 @@ new_data_block(struct file_priv *file, hed_uoff_t pos, hed_uoff_t size, cache_get(dataobj); new->dataobj = dataobj; + assert(size != 0); new->t.maxoff = size - 1; new->phys_pos = pos; new->dataoff = FILE_BLOCK_OFF(pos); @@ -595,6 +597,8 @@ split_block(struct file_priv *file, struct hed_block *block, { struct hed_block *head; + assert(!hed_block_is_empty(block)); + head = new_block(file, block->flags & ~HED_BLOCK_TERMINAL); if (!head) return NULL; @@ -606,6 +610,7 @@ split_block(struct file_priv *file, struct hed_block *block, } else assert(hed_block_is_virtual(block)); + assert(splitpoint != 0); head->t.maxoff = splitpoint - 1; head->phys_pos = block->phys_pos; @@ -1507,6 +1512,7 @@ prepare_modify(struct file_priv *file, hed_cursor_t *curs, size_t len) newblock->dataoff = FILE_BLOCK_OFF(newblock->phys_pos); if (len > FILE_BLOCK_SIZE - newblock->dataoff) len = FILE_BLOCK_SIZE - newblock->dataoff; + assert(len != 0); newblock->t.maxoff = len - 1; if (replace_chunk(file, block, block_offset, newblock)) @@ -1809,6 +1815,7 @@ insert_block(struct file_priv *file, hed_cursor_t *curs, memcpy(hed_block_data(block) + curs->off, buf, len); block->t.maxoff += len; + assert(len != 0); hed_block_clear_empty(block); recalc_block_recursive(block); curs->off += len; -- 2.11.4.GIT