]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
shared: let strbuf_str never fail
authorTobias Stoeckmann <tobias@stoeckmann.org>
Mon, 24 Feb 2025 19:07:05 +0000 (20:07 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 7 Mar 2025 04:57:37 +0000 (22:57 -0600)
The strbuf functionality is always used to create C strings, so already
reserve an extra byte for NUL whenever size allocations occur.

Together with the fact that strbuf_str already returns a const char *,
an empty strbuf may return an unmodifiable empty string.

This renders all strbuf_str return value checks obsolete.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/296
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-index.c
libkmod/libkmod-module.c
shared/strbuf.c
tools/depmod.c

index ae784d6e2f78baa7a3b5c36f9a6311008d17ba62..cc11f4d3187cfcb3f294a538c022caf8c779ab69 100644 (file)
@@ -534,7 +534,7 @@ static void index_searchwild__all(struct index_node_f *node, int j, struct strbu
        if (node->values) {
                const char *s = strbuf_str(buf);
 
-               if (s != NULL && fnmatch(s, subkey, 0) == 0)
+               if (fnmatch(s, subkey, 0) == 0)
                        index_searchwild__allvalues(node, out);
                else
                        index_close(node);
@@ -997,7 +997,7 @@ static void index_mm_searchwild_all(struct index_mm_node *node, int j, struct st
        if (node->value_count > 0) {
                const char *s = strbuf_str(buf);
 
-               if (s != NULL && fnmatch(s, subkey, 0) == 0)
+               if (fnmatch(s, subkey, 0) == 0)
                        index_mm_searchwild_allvalues(node, out);
        }
 
index dc660f24a79b17d6a27e2afa770d57db0b952280..ff04ee19f4a224123dad16b86f57c942f33f9d76 100644 (file)
@@ -1835,8 +1835,6 @@ static struct kmod_list *kmod_module_info_append_hex(struct kmod_list **list,
                if (!kmod_module_strbuf_pushhex(&sbuf, value, valuelen))
                        goto list_error;
                hex = strbuf_str(&sbuf);
-               if (hex == NULL)
-                       goto list_error;
 
                n = kmod_module_info_append(list, key, keylen, hex, strlen(hex));
                if (n == NULL)
index 6ce1f05e55652833a64f4cf770f7af62d1b9743c..8e6dd72d37d81ae136a21dfa3ceebff02e441a49 100644 (file)
@@ -36,13 +36,13 @@ static bool buf_realloc(struct strbuf *buf, size_t sz)
 
 bool strbuf_reserve_extra(struct strbuf *buf, size_t n)
 {
-       if (uaddsz_overflow(buf->used, n, &n) || n > SIZE_MAX - BUF_STEP)
+       if (uaddsz_overflow(buf->used, n, &n) || n >= SIZE_MAX - BUF_STEP)
                return false;
 
-       if (n <= buf->size)
+       if (n < buf->size)
                return true;
 
-       if (n % BUF_STEP)
+       if (++n % BUF_STEP)
                n = ((n / BUF_STEP) + 1) * BUF_STEP;
 
        return buf_realloc(buf, n);
@@ -64,11 +64,11 @@ void strbuf_release(struct strbuf *buf)
 
 const char *strbuf_str(struct strbuf *buf)
 {
-       if (!buf->used || buf->bytes[buf->used - 1]) {
-               if (!strbuf_reserve_extra(buf, 1))
-                       return NULL;
+       if (!buf->used)
+               return "";
+
+       if (buf->bytes[buf->used - 1])
                buf->bytes[buf->used] = '\0';
-       }
        return buf->bytes;
 }
 
index dcc7acbeb481532fbc94ab23bac7fe1e6c93982d..5db9701c12060df2c2cc7e91abac4d9f11b9bcae 100644 (file)
@@ -1362,7 +1362,7 @@ static int depmod_modules_search_dir(struct depmod *depmod, DIR *d, struct strbu
                namelen = strlen(name);
 
                if (!strbuf_pushchars(path, name) ||
-                   /* Ensure space for (possible) '/' or '\0' */
+                   /* Ensure space for (possible) '/' */
                    !strbuf_reserve_extra(path, 1)) {
                        err = -ENOMEM;
                        ERR("No memory\n");
@@ -2360,8 +2360,7 @@ static int output_symbols_bin(struct depmod *depmod, FILE *out)
 
                strbuf_shrink_to(&salias, baselen);
 
-               if (!strbuf_pushchars(&salias, sym->name) ||
-                   !strbuf_reserve_extra(&salias, 1)) {
+               if (!strbuf_pushchars(&salias, sym->name)) {
                        ret = -ENOMEM;
                        goto err_alloc;
                }