126 Commits

Author SHA1 Message Date
Emil Velikov
bffe1a7800 Remove _always_inline_ attribute
We have a single _always_inline_ instance in-tree, which lives in a
header file. Just make it a "static inline", which will practically do
the same thing and what we already have elsewhere in-tree.

While in there drop the CAPS from ALIGN_POWER2 - it's a function, so we
don't need the shouting.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/376
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-07-08 07:17:14 -05:00
Tobias Stoeckmann
9c1a363e3e util: Use local variable in get_backoff_delta_msec
This might make the code easier to read.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/377
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-07-07 10:20:33 -05:00
Tobias Stoeckmann
65afd0f1c6 util: check for overflow in get_backoff_delta_msec
If a large delta is given, the multiplication might overflow.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/377
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-07-07 10:20:33 -05:00
Tobias Stoeckmann
f14f260382 util: fix endless loop in get_backoff_delta_msec
If current time t is already past tend, the while loop in
get_backoff_delta_msec never ends.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/377
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-07-07 10:20:33 -05:00
Emil Velikov
d44eb2ffa4 Use explicit ENOMEM when {m,re}alloc fails
Currently our codebase has a mix of explicitly using ENOMEM and
propagating the errno... The latter of which is guaranteed to be ENOMEM
as documented in the manual pages.

Consolidate on ENOMEM, both for consistency sake and to remove a few
bytes off our binaries ;-)

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/368
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-06-11 08:03:26 -05:00
Emil Velikov
d2ee70eac8 shared/util: remove unused t0 in get_backoff_delta_msec()
The argument was unused ever since the function was introduced - remove
it.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/361
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-06-11 07:54:40 -05:00
Emil Velikov
52e20d50a6 libkmod: add TODO for module hashkey handling
We currently "leak" the hashkey handling in a few places. As result we
ignore the name passed to kmod_module_new().

Add a TODO entry and silence the unused parameter compiler warning.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/361
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-06-11 07:54:40 -05:00
Emil Velikov
5610688ec0 shared: properly __attribute__ all macros
Handful of the attributes were missing the trailing double underscore.
Thus they were not picked by our git grep combo and ultimately missing
from .clang-format.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/361
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-06-11 07:54:40 -05:00
Emil Velikov
20e91b3a6b shared/macro: avoid nesting macros where possible
Always try to use the attribute notation, so that we can reliably
collect all the macros in our .clang-format.

In the process, we can remove some (no longer used) macros.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/361
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-06-11 07:54:40 -05:00
Emil Velikov
2497950466 tools/depmod: use nsec granularity when checking timestamps
Current code uses the POSIX.1-2001 stat::st_mtime which has one second
granularity. Considering the depmod execution time may be smaller, we
should be using the POSIX.1-2008 stat::st_mtim which has nano-second
granularity instead.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/359
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-30 09:04:02 -05:00
Emil Velikov
2077cb39f2 Remove dietlibc stat::st_mtim workaround
Some versions of dietlibc lack struct stat::st_mtim, which was
introduced with POSIX.1-2008.

Considering the slow development cycle of dietlibc (last three releases
are 2024, 2018 and 2013) and there's no popular user (nor one in CI) of
it, let's remove the workaround.

All the other runtimes that we target - glibc, bionic and musl - have
the struct member.

Iff needed we can reintroduce the workaround at a later stage alongside
a CI permutation.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/359
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-30 09:04:02 -05:00
Emil Velikov
1996121ec1 Convert most #ifdef HAVE_FOO checks to #if HAVE_FOO
The former is somewhat error prone, since the pre-processor will
evaluate the presence of the define, which might have been omitted due
to wrong include or otherwise.

Swap for the other solution we already have in-tree - always set the
relevant define(s) and evaluate them numerically. That combined with
-Wundef means that we'll get meaningful compiler warning when we get
something wrong.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/356
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-30 09:02:15 -05:00
Tobias Stoeckmann
dcd652b57c check strtol/strtoul/strtoull results
The strto* family of functions may fail if the input string contains
a number which is too large for the designated data type. In such
cases, errno is set to ERANGE. Check for this error condition and if
subsequent casts would truncate the value.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/357
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-29 22:57:51 -05:00
Emil Velikov
13b646a73f Remove default initializer for static variables
The spec dictates that static variables are initialized to 0/NULL.
Remove the unnecessary explicit initialization - on older compilers this
sheds a few bytes off the data section of respective binaries.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/358
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-29 21:16:00 -05:00
Emil Velikov
e3acc66363 shared: declare DECLARE_(DL,PTR)SYM as static
Declare the symbol pointers as static, otherwise the compiler will add
them to the data section instead of bss.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/358
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-29 21:16:00 -05:00
Emil Velikov
dfaed74ccb shared: use strlen() for string literals
Older compilers had struggles expanding strlen(string-literal) to a
constant compile time expression. Thus our code-base used sizeof() - 1
instead.

This has been resolved for years, so let's use the correct function.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/354
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-29 17:10:16 -05:00
Emil Velikov
879e5fabc6 shared: don't reset errno in read_str_{u,}long()
Currently we reset errno prior to calling strto{u,}l(). This is not
needed, since a) we don't check errno to see if the function was
successful and b) we explicitly propagate the error code by returning it
directly to the caller.

Reference: https://github.com/kmod-project/kmod/issues/244
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/346
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-19 21:51:43 -05:00
Emil Velikov
9ad9a8d6b2 shared: rework fd_lookup_path() helper
Reduce the stack and heap memory usage of the helper by using a caller
provided buffer. This allows us to avoid copying the path, yet again,
within the caller.

Aside: readlink(3) returns -1 on all errors, it won't return the
prospective size if the output buffer is too small.

v2:
 - swap fd_lookup_path() path/pathlen argument order

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/345
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-09 13:52:46 -05:00
Emil Velikov
9615035e7b shared: remove unreachable snprintf error case
It is practically impossible for `/proc/self/fd/%d` to result in a
string of PATH_MAX size. Remove the unreachable error handling.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/345
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-09 13:52:46 -05:00
Emil Velikov
6c5e7997a1 Use sizeof() over PATH_MAX for bounds handling
Bunch of our codebase uses sizeof() while the rest hard-coded length for
the destination buffer. Converge on the former since it makes for more
obvious and less error prone code.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/345
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-09 13:52:46 -05:00
Emil Velikov
0fd84a53ab Use sizeof() over PATH_MAX in snprintf() contexts
Bunch of our codebase uses sizeof() while the rest hard-coded length for
the destination buffer. Converge on the former since it makes for more
obvious and less error prone code.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/345
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-05-09 13:52:46 -05:00
Lucas De Marchi
ec9ccdadf0 tmpfile-util: Drop libgen.h
We don't want the version of basename() that may leak memory - we want
the sane one. I remembered to add the missing.h while editing commit
aae48bc9f73a ("depmod: add tmpfile-util to generate temporary file") to
merge it, but completely forgot to remove the libgen.h. Fix it now.

Fixes: aae48bc9f73a ("depmod: add tmpfile-util to generate temporary file")
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/335
2025-04-05 21:24:33 -05:00
Wang, Wenjie2
aae48bc9f7 depmod: add tmpfile-util to generate temporary file
* we use `mkstemp` to create the temporary file since it's a general
  function in linux system and O_TMPFILE is not supported on all linux
  system.
* add `struct tmpfile` to keep the `dirfd`, temp file `fd` and the
  filename.

Co-developed-by: Wenjie Wang <wenjie2.wang@intel.com>
Signed-off-by: Wenjie Wang <wenjie2.wang@intel.com>
Co-developed-by: Gongjun Song <gongjun.song@intel.com>
Signed-off-by: Gongjun Song <gongjun.song@intel.com>
Signed-off-by: Dan He <dan.h.he@intel.com>
Signed-off-by: Qingqing Li <qingqing.li@intel.com>
Signed-off-by: Yuchi Chen <yuchi.chen@intel.com>
Link: https://github.com/kmod-project/kmod/pull/305
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-04-02 23:23:28 -05:00
Tobias Stoeckmann
b4f1546bd3 shared: improve strbuf_reserve_extra performance
If strbuf is used (depmod, modprobe -c) then strbuf_reserve_extra is
performance critical. This reduces amount of instructions for
modprobe -c by around 10 %, the total instruction count for depmod
by 1 % (majority is within reading module files).

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>
2025-03-06 22:57:37 -06:00
Tobias Stoeckmann
55c779fa56 shared: turn strbuf_reserve_extra private
We can make it public if it's needed again.

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>
2025-03-06 22:57:37 -06:00
Tobias Stoeckmann
2b47598178 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 <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/296
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-03-06 22:57:37 -06:00
Tobias Stoeckmann
84579a015f shared: drop strbuf_steal
The only use case of strbuf_steal is gone. If the requirement for this
function returns, we can add it again.

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>
2025-03-06 22:57:37 -06:00
Lucas De Marchi
3ff564c3ca strbuf: Remove alloca.h include
This is not used anymore.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-02-14 19:18:43 -06:00
Lucas De Marchi
44855d773b shared/macro: Add _alignedptr_
Peek what systemd does and do something similar. Instead of hardcoding
8, add a ptr abstraction for the aligned attribute.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/274
2025-01-03 22:44:18 -06:00
Lucas De Marchi
d7e7c4c499 shared/macro: Add macros for more attributes
Move attributes from test definition to a wrapper macro.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/274
2025-01-03 22:44:18 -06:00
Tobias Stoeckmann
235a38bf51 strbuf: Extend strbuf_reserve_extra size check
Make sure that alignment to BUF_STEP won't overflow if excessively
huge amount of memory is requested to be reserved.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/268
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2025-01-02 14:35:52 -06:00
Lucas De Marchi
c9f80c49b1 libkmod: Add ELF notes to compression libraries
Follow the new spec for ELF notes as detailed in
https://systemd.io/ELF_PACKAGE_METADATA/.

We can copy mostly verbatim the macros from systemd codebase.

Example output:

	$ meson setup --native-file build-dev.ini -Dxz=disabled -Ddlopen=zlib build
	...
	    dlopen           : zlib

	    features         : +ZSTD -XZ +ZLIB +OPENSSL

	$ dlopen-notes.py build/libkmod.so.2
	# build/libkmod.so.2
	[
	  {
	    "feature": "xz",
	    "description": "Support for uncompressing xz-compressed modules",
	    "priority": "recommended",
	    "soname": [
	      "liblzma.so.5"
	    ]
	  }
	]

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/262
2024-12-06 13:16:28 -08:00
Lucas De Marchi
01d9c6a076 build: Always define ENABLE_* macros
Define either to 0 or 1 so codebase is forced to used `#if ENABLE_*` or
similar. It's confusing to have some leaving undefined and others
defining as 0 or 1.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/262
2024-12-06 13:06:42 -08:00
Lucas De Marchi
1f49475e3d util: Add dlfcn helpers
Heavily based on systemd: add similar (but simplified) functions to
dlopen() a library and load specific symbols from it. This will be
useful to allow to load the compression libraries as needed. A few
differences from the systemd implementation:

1) It's allowed to link directly to the library and hence bypass the
   dlopen() + dlsym()
2) The only entrypoint is dlsym_many() which is already declared with
   the sentinel: it's expected callers will use an x-macro that doesn't
   allow forgetting the sentinel
3) No support yet for ELF info annotation yet

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/262
2024-12-06 13:05:09 -08:00
Lucas De Marchi
1c8fae2f77 Add TAKE_PTR()
Similar to macro in systemd codebase: add a macro that documents we are
"leaking" a pointer that would otherwise be cleaned up by the cleanup
attribute.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/264
2024-12-06 12:59:10 -08:00
Martin Wilck
62eaff4a16 libkmod: hash_new(): always use a step size of at least 4
The current algorithm would choose a step size of 4 for
n_buckets == 31, but 1 for n_buckets == 32, which seems wrong.
Fix it.

Signed-off-by: Martin Wilck <mwilck@suse.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-11-29 09:15:21 -06:00
Martin Wilck
5f154ec21e libkmod: strbuf_pushchars: handle pushing empty string gracefully
If buf->bytes, buf->used, and len are all 0, buf_grow() will do nothing,
and memcpy() willbe called with a NULL first argument. This will cause
an error because the function is annotated with __nonnull(1, 2).

Fixes: e62d8c7 ("strbuf: make strbuf_pushchars() a little less dumb")
Signed-off-by: Martin Wilck <martin_wilck@gmx.de>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-11-29 09:15:21 -06:00
Martin Wilck
e6a5cc2139 libkmod: hash_del: handle deleting a non-existing element gracefully
It can happen that bucket->entries is NULL, but bsearch is annotated
such that it's second argument must be non-NULL.

Fixes: 7db0865 ("Add simple hash implementation")
Signed-off-by: Martin Wilck <martin_wilck@gmx.de>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-11-29 09:15:21 -06:00
Martin Wilck
eac0a4a6ab libkmod: hash_del: fix out-of-bounds memory access
Assume bucket->used is 1, and element 0 is deleted. We shouldn't access any
memory above (entry + 1) in this case. Likewise, if bucked->used is 2, only
one element should be shifted, etc.

Fixes: 7db0865 ("Add simple hash implementation")
Signed-off-by: Martin Wilck <martin_wilck@gmx.de>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-11-29 09:15:21 -06:00
Lucas De Marchi
63302cf655 util: Promote path_is_absolute() to header
This is a trivial function that can be used elsewhere. There's no point
in keeping the assert if we are going to crash in the very next
instruction. Rather add the relevant attribute and drop the assert.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/243
2024-11-20 22:25:30 -06:00
Tobias Stoeckmann
088e2e2d69 libkmod: Fix typo in comment
The word practise only exists in British English and is a verb.
Switch to practice and adjust style.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Link: https://github.com/kmod-project/kmod/pull/241
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2024-11-19 00:23:32 -06:00
Lucas De Marchi
c6b4652dd9 Remove scratchbuf implementation
All its unique features have been ported to strbuf and users converted.
Nuke it.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
2024-11-17 15:35:14 -06:00
Lucas De Marchi
14ce34d755 strbuf: Add strbuf_shrink_to()
Useful when working with paths on a loop and resetting to a base path
component on every loop iteration.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
2024-11-17 15:35:13 -06:00
Lucas De Marchi
5706fb7d61 strbuf: Add strbuf_used()
So users don't feel tempted to look at inside the strbuf. Just add a
function for it and proper tests.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
2024-11-17 15:35:13 -06:00
Lucas De Marchi
68c0b9fb8c strbuf: Add strbuf_pushmem()
Wrapper for memcpy(). It's similar to strbuf_pushchars(), but push any
char, including the NUL byte, relying on the size passed as argument.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
2024-11-17 15:35:13 -06:00
Lucas De Marchi
cc2ba0b74a strbuf: Do not append '\0' if not needed
Unconditionally appending '\0' is not a big problem, but that does
trigger the buffer to potentially be re-allocated. Avoid that by
checking the last char is not already NUL.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
2024-11-17 15:35:13 -06:00
Lucas De Marchi
952bf223e0 strbuf: Add strbuf_reserve_extra()
To accomplish the same task as scratchbuf_alloc() does: make sure the
buffer has enough space for next operations. One difference is that
ensures **extra** space, not the total space. If needed in future,
a strbuf_reserve(), that resembles C++'s std::vector::reserve(), can be
added on top.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
2024-11-17 15:35:13 -06:00
Lucas De Marchi
1e36bfada5 strbuf: Allow to start with stack space
This is the main functionality of the scratchbuf implementation: it
starts with a buffer on stack that covers most of the calls, but also
has a fallback to allocate the buffer if it grows beyond the initial
size.

Port that functionality from scratchbuf to strbuf, so the former can be
eventually removed.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
2024-11-17 15:35:13 -06:00
Lucas De Marchi
25f2b2e096 strbuf: Invalidate (only) when stolen
The main for strbuf_steal() to free() on error was to allow the caller
to pass the NULL up the stack with just a return call to
strbuf_steal().

However this is error-prone and surprising that the buffer is still
invalidated on error. Provide an strbuf cleanup attribute that can be
used for the same purpose and make sure that when the string is stolen,
it's set to NULL, so there's no dangling pointer around.

Since we run the  testsuite with AddressSanitizer, a simple test can be
added to make sure the stolen string becomes valid when the attribute is
used.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
2024-11-17 15:35:13 -06:00
Lucas De Marchi
b8776806de strbuf: Document strbuf_popchar(s)
Document the behavior for these functions and also clarify why the
testsuite can check the buffer for equality after calling strbuf_str().

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
2024-11-17 15:35:13 -06:00