From: Tobias Stoeckmann Date: Mon, 24 Feb 2025 18:55:28 +0000 (+0100) Subject: libkmod: refactor kmod_module_hex_to_str X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0766b418a8d7f010695925ede08e6ed8966e83dd;p=thirdparty%2Fkmod.git libkmod: refactor kmod_module_hex_to_str 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 Link: https://github.com/kmod-project/kmod/pull/296 Signed-off-by: Lucas De Marchi --- diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 661b3cf6..dc660f24 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -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 {