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(-) 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