From: Tobias Stoeckmann Date: Mon, 24 Feb 2025 19:07:05 +0000 (+0100) Subject: shared: let strbuf_str never fail X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2b475981783d5eba139df1c093c8933f7428d663;p=thirdparty%2Fkmod.git shared: let strbuf_str never fail 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 Link: https://github.com/kmod-project/kmod/pull/296 Signed-off-by: Lucas De Marchi --- diff --git a/libkmod/libkmod-index.c b/libkmod/libkmod-index.c index ae784d6e..cc11f4d3 100644 --- a/libkmod/libkmod-index.c +++ b/libkmod/libkmod-index.c @@ -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); } diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index dc660f24..ff04ee19 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -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) diff --git a/shared/strbuf.c b/shared/strbuf.c index 6ce1f05e..8e6dd72d 100644 --- a/shared/strbuf.c +++ b/shared/strbuf.c @@ -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; } diff --git a/tools/depmod.c b/tools/depmod.c index dcc7acbe..5db9701c 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -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; }