From 3ac86ba67f0423914616b1b3c271df54880f92f2 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Tue, 10 Sep 2024 18:13:41 +0200 Subject: [PATCH] libkmod: handle read_char errors If read_char fails, stop operations and return an error instead. Signed-off-by: Tobias Stoeckmann Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/123 Signed-off-by: Lucas De Marchi --- libkmod/libkmod-index.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libkmod/libkmod-index.c b/libkmod/libkmod-index.c index 6f6c2f4e..77d43dba 100644 --- a/libkmod/libkmod-index.c +++ b/libkmod/libkmod-index.c @@ -196,7 +196,7 @@ static ssize_t buf_freadchars(struct strbuf *buf, FILE *in) int ch; while ((ch = read_char(in))) { - if (!strbuf_pushchar(buf, ch)) + if (ch == EOF || !strbuf_pushchar(buf, ch)) return -1; i++; } @@ -240,15 +240,19 @@ static struct index_node_f *index_read(FILE *in, uint32_t offset) prefix = NOFAIL(strdup("")); if (offset & INDEX_NODE_CHILDS) { - char first = read_char(in); - char last = read_char(in); + int first = read_char(in); + int last = read_char(in); + + if (first == EOF || last == EOF) + goto err; + child_count = last - first + 1; node = NOFAIL(malloc(sizeof(struct index_node_f) + sizeof(uint32_t) * child_count)); - node->first = first; - node->last = last; + node->first = (unsigned char) first; + node->last = (unsigned char) last; for (i = 0; i < child_count; i++) if (read_long(in, &node->children[i]) < 0) -- 2.47.3