]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: refactor kmod_module_hex_to_str
authorTobias Stoeckmann <tobias@stoeckmann.org>
Mon, 24 Feb 2025 18:55:28 +0000 (19:55 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 7 Mar 2025 04:57:37 +0000 (22:57 -0600)
The hex output uses a heap-based strbuf. It can be turned into a
stack-based strbuf by refactoring kmod_module_hex_to_str. Instead of
returning a C string, a supplied strbuf can be filled with hex values
in ASCII representation. Renamed to kmod_module_strbuf_pushhex.

A size of 512 is sufficient for signatures on Arch Linux and removes
heap allocations. Additional benefit is implicit strbuf_init and
strbuf_release due to DECLARE_STRBUF_WITH_STACK.

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-module.c

index 661b3cf62b4226b04a0085472bc1d3ed55c6b62a..dc660f24a79b17d6a27e2afa770d57db0b952280 100644 (file)
@@ -1799,46 +1799,46 @@ static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const
        return n;
 }
 
-static char *kmod_module_hex_to_str(const char *hex, size_t len)
+static bool kmod_module_strbuf_pushhex(struct strbuf *sbuf, const char *hex, size_t len)
 {
        static const char digits[] = "0123456789ABCDEF";
-       _cleanup_strbuf_ struct strbuf sbuf;
        const size_t line_limit = 20;
 
-       strbuf_init(&sbuf);
-
        for (size_t i = 0; i < len; i++) {
-               if (!strbuf_pushchar(&sbuf, digits[(hex[i] >> 4) & 0x0F]) ||
-                   !strbuf_pushchar(&sbuf, digits[hex[i] & 0x0F]))
-                       return NULL;
+               if (!strbuf_pushchar(sbuf, digits[(hex[i] >> 4) & 0x0F]) ||
+                   !strbuf_pushchar(sbuf, digits[hex[i] & 0x0F]))
+                       return false;
 
                if (i < len - 1) {
-                       if (!strbuf_pushchar(&sbuf, ':'))
-                               return NULL;
+                       if (!strbuf_pushchar(sbuf, ':'))
+                               return false;
 
-                       if ((i + 1) % line_limit == 0 &&
-                           !strbuf_pushchars(&sbuf, "\n\t\t"))
-                               return NULL;
+                       if ((i + 1) % line_limit == 0 && !strbuf_pushchars(sbuf, "\n\t\t"))
+                               return false;
                }
        }
 
-       return strbuf_steal(&sbuf);
+       return true;
 }
 
 static struct kmod_list *kmod_module_info_append_hex(struct kmod_list **list,
                                                     const char *key, size_t keylen,
                                                     const char *value, size_t valuelen)
 {
-       char *hex;
        struct kmod_list *n;
 
        if (valuelen > 0) {
+               DECLARE_STRBUF_WITH_STACK(sbuf, 512);
+               const char *hex;
+
                /* Display as 01:12:DE:AD:BE:EF:... */
-               hex = kmod_module_hex_to_str(value, valuelen);
+               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));
-               free(hex);
                if (n == NULL)
                        goto list_error;
        } else {