Skip to content

Commit

Permalink
libkmod: check strbuf return values
Browse files Browse the repository at this point in the history
The strbuf functions may fail, so handle errors. Make especially sure
that push and pop operations stay in sync.

Signed-off-by: Tobias Stoeckmann <[email protected]>
  • Loading branch information
stoeckmann committed Sep 24, 2024
1 parent bfa1c76 commit bf2dc89
Showing 1 changed file with 56 additions and 40 deletions.
96 changes: 56 additions & 40 deletions libkmod/libkmod-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -389,9 +393,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);
Expand All @@ -408,8 +413,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);
}

Expand Down Expand Up @@ -493,8 +498,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++;
}

Expand All @@ -504,13 +508,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);
Expand Down Expand Up @@ -548,23 +555,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') {
Expand Down Expand Up @@ -871,9 +881,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);
Expand All @@ -890,8 +901,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);
}

Expand Down Expand Up @@ -978,8 +989,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++;
}

Expand All @@ -989,13 +999,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);
Expand Down Expand Up @@ -1033,23 +1046,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') {
Expand Down

0 comments on commit bf2dc89

Please sign in to comment.