From 07078da0f07dfd410398ec9c816b2d7d888f2d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 3 Jan 2021 16:18:42 +0100 Subject: [PATCH] =?UTF-8?q?extract:=20finish:=20fix=20bad=20assertion=20-?= =?UTF-8?q?=20=E2=80=98idx=E2=80=99=20may=20be=20equal=20to=20=E2=80=98siz?= =?UTF-8?q?e=E2=80=99?= 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);