From 6398aef09d36fdca8165dfedf9cc5d36f8fca8f8 Mon Sep 17 00:00:00 2001 From: Allan McRae Date: Wed, 3 Apr 2024 13:58:35 +1000 Subject: [PATCH] _alpm_archive_fgets(): bail early if reached end of block Bailing early when there are 0 blocks remaining means that we do not call memchr on a NULL string (although with a 0 size parameter). Fixes issues reported using -fsanitise=address,undefined Signed-off-by: Allan McRae (cherry picked from commit 4dc21b965b891042edc951d53f9ce93bf265cdfd) --- lib/libalpm/util.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 83160f00..e1f87142 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -1138,6 +1138,14 @@ int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b) b->block_offset = b->block; block_remaining = b->block_size; + if(block_remaining == 0) { + /* there was no new data, return what is left; ARCHIVE_EOF will be + * returned on next call */ + b->line_offset[0] = '\0'; + b->real_line_size = b->line_offset - b->line; + return ARCHIVE_OK; + } + /* error, cleanup */ if(b->ret < ARCHIVE_OK) { goto cleanup; @@ -1193,13 +1201,6 @@ int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b) memcpy(b->line_offset, b->block_offset, len); b->line_offset += len; b->block_offset = b->block + b->block_size; - /* there was no new data, return what is left; saved ARCHIVE_EOF will be - * returned on next call */ - if(len == 0) { - b->line_offset[0] = '\0'; - b->real_line_size = b->line_offset - b->line; - return ARCHIVE_OK; - } } }