From: Tobias Stoeckmann Date: Tue, 24 Sep 2024 18:56:40 +0000 (+0200) Subject: libkmod: check strbuf return values X-Git-Tag: v34~273 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=889d02b1650229154be7289181d93b9a203a39a2;p=thirdparty%2Fkmod.git libkmod: check strbuf return values The strbuf functions may fail, so handle errors. Make especially sure that push and pop operations stay in sync. Signed-off-by: Tobias Stoeckmann Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/152 Signed-off-by: Lucas De Marchi --- diff --git a/libkmod/libkmod-index.c b/libkmod/libkmod-index.c index 1c730ea4..1dec1edd 100644 --- a/libkmod/libkmod-index.c +++ b/libkmod/libkmod-index.c @@ -289,6 +289,10 @@ static struct index_node_f *index_read(FILE *in, uint32_t offset) goto err; } value = strbuf_str(&buf); + if (value == NULL) { + strbuf_release(&buf); + goto err; + } add_value(&node->values, value, buf.used, priority); strbuf_clear(&buf); } @@ -391,9 +395,10 @@ static void index_dump_node(struct index_node_f *node, struct strbuf *buf, int f if (!child) continue; - strbuf_pushchar(buf, ch); - index_dump_node(child, buf, fd); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, ch)) { + index_dump_node(child, buf, fd); + strbuf_popchar(buf); + } } strbuf_popchars(buf, pushed); @@ -410,8 +415,8 @@ void index_dump(struct index_file *in, int fd, const char *prefix) return; strbuf_init(&buf); - strbuf_pushchars(&buf, prefix); - index_dump_node(root, &buf, fd); + if (strbuf_pushchars(&buf, prefix)) + index_dump_node(root, &buf, fd); strbuf_release(&buf); } @@ -495,8 +500,7 @@ static void index_searchwild__all(struct index_node_f *node, int j, struct strbu while (node->prefix[j]) { ch = node->prefix[j]; - strbuf_pushchar(buf, ch); - pushed++; + pushed += strbuf_pushchar(buf, ch); j++; } @@ -506,13 +510,16 @@ static void index_searchwild__all(struct index_node_f *node, int j, struct strbu if (!child) continue; - strbuf_pushchar(buf, ch); - index_searchwild__all(child, 0, buf, subkey, out); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, ch)) { + index_searchwild__all(child, 0, buf, subkey, out); + strbuf_popchar(buf); + } } if (node->values) { - if (fnmatch(strbuf_str(buf), subkey, 0) == 0) + const char *s = strbuf_str(buf); + + if (s != NULL && fnmatch(s, subkey, 0) == 0) index_searchwild__allvalues(node, out); else index_close(node); @@ -550,23 +557,26 @@ static void index_searchwild__node(struct index_node_f *node, struct strbuf *buf child = index_readchild(node, '*'); if (child) { - strbuf_pushchar(buf, '*'); - index_searchwild__all(child, 0, buf, &key[i], out); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, '*')) { + index_searchwild__all(child, 0, buf, &key[i], out); + strbuf_popchar(buf); + } } child = index_readchild(node, '?'); if (child) { - strbuf_pushchar(buf, '?'); - index_searchwild__all(child, 0, buf, &key[i], out); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, '?')) { + index_searchwild__all(child, 0, buf, &key[i], out); + strbuf_popchar(buf); + } } child = index_readchild(node, '['); if (child) { - strbuf_pushchar(buf, '['); - index_searchwild__all(child, 0, buf, &key[i], out); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, '[')) { + index_searchwild__all(child, 0, buf, &key[i], out); + strbuf_popchar(buf); + } } if (key[i] == '\0') { @@ -873,9 +883,10 @@ static void index_mm_dump_node(struct index_mm_node *node, struct strbuf *buf, i if (child == NULL) continue; - strbuf_pushchar(buf, ch); - index_mm_dump_node(child, buf, fd); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, ch)) { + index_mm_dump_node(child, buf, fd); + strbuf_popchar(buf); + } } strbuf_popchars(buf, pushed); @@ -892,8 +903,8 @@ void index_mm_dump(struct index_mm *idx, int fd, const char *prefix) return; strbuf_init(&buf); - strbuf_pushchars(&buf, prefix); - index_mm_dump_node(root, &buf, fd); + if (strbuf_pushchars(&buf, prefix)) + index_mm_dump_node(root, &buf, fd); strbuf_release(&buf); } @@ -980,8 +991,7 @@ static void index_mm_searchwild_all(struct index_mm_node *node, int j, struct st while (node->prefix[j]) { ch = node->prefix[j]; - strbuf_pushchar(buf, ch); - pushed++; + pushed += strbuf_pushchar(buf, ch); j++; } @@ -991,13 +1001,16 @@ static void index_mm_searchwild_all(struct index_mm_node *node, int j, struct st if (!child) continue; - strbuf_pushchar(buf, ch); - index_mm_searchwild_all(child, 0, buf, subkey, out); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, ch)) { + index_mm_searchwild_all(child, 0, buf, subkey, out); + strbuf_popchar(buf); + } } if (node->values.len > 0) { - if (fnmatch(strbuf_str(buf), subkey, 0) == 0) + const char *s = strbuf_str(buf); + + if (s != NULL && fnmatch(s, subkey, 0) == 0) index_mm_searchwild_allvalues(node, out); else index_mm_free_node(node); @@ -1035,23 +1048,26 @@ static void index_mm_searchwild_node(struct index_mm_node *node, struct strbuf * child = index_mm_readchild(node, '*'); if (child) { - strbuf_pushchar(buf, '*'); - index_mm_searchwild_all(child, 0, buf, &key[i], out); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, '*')) { + index_mm_searchwild_all(child, 0, buf, &key[i], out); + strbuf_popchar(buf); + } } child = index_mm_readchild(node, '?'); if (child) { - strbuf_pushchar(buf, '?'); - index_mm_searchwild_all(child, 0, buf, &key[i], out); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, '?')) { + index_mm_searchwild_all(child, 0, buf, &key[i], out); + strbuf_popchar(buf); + } } child = index_mm_readchild(node, '['); if (child) { - strbuf_pushchar(buf, '['); - index_mm_searchwild_all(child, 0, buf, &key[i], out); - strbuf_popchar(buf); + if (strbuf_pushchar(buf, '[')) { + index_mm_searchwild_all(child, 0, buf, &key[i], out); + strbuf_popchar(buf); + } } if (key[i] == '\0') {