From 07078da0f07dfd410398ec9c816b2d7d888f2d38 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 3 Jan 2021 16:18:42 +0100 Subject: extract: finish: fix bad assertion - ‘idx’ may be equal to ‘size’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ‘idx’ is where _new_ data should be pushed into the buffer. Thus it is perfectly valid for it to be equal to ‘size’ - it just means we need to allocate more space before pushing data to it. --- extract.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'extract.c') diff --git a/extract.c b/extract.c index 7b5da229..d6214f70 100644 --- a/extract.c +++ b/extract.c @@ -72,11 +72,14 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len) ctx->buf[ctx->idx] = L'\0'; } else { assert(ctx->idx > 0); - assert(ctx->idx < ctx->size); + assert(ctx->idx <= ctx->size); if (ctx->buf[ctx->idx - 1] == L'\n') ctx->buf[ctx->idx - 1] = L'\0'; - else + else { + if (!ensure_size(ctx, 1)) + goto out; ctx->buf[ctx->idx] = L'\0'; + } } size_t _len = wcstombs(NULL, ctx->buf, 0); -- cgit v1.3 From fd5d68c819d41cb5966408713ed3fb5d27e3ff57 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 3 Jan 2021 16:21:36 +0100 Subject: extract: finish: increase ‘idx’ when pushing new data, for consistency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We don’t write anything more to the buffer after this, but this makes this code consistent with all other code that pushes new data to the buffer. This makes it easier to search, and validate, the ensure_size()+push-data pattern. --- extract.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'extract.c') diff --git a/extract.c b/extract.c index d6214f70..a6492716 100644 --- a/extract.c +++ b/extract.c @@ -69,7 +69,7 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len) /* Selection of empty cells only */ if (!ensure_size(ctx, 1)) goto out; - ctx->buf[ctx->idx] = L'\0'; + ctx->buf[ctx->idx++] = L'\0'; } else { assert(ctx->idx > 0); assert(ctx->idx <= ctx->size); @@ -78,7 +78,7 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len) else { if (!ensure_size(ctx, 1)) goto out; - ctx->buf[ctx->idx] = L'\0'; + ctx->buf[ctx->idx++] = L'\0'; } } -- cgit v1.3