diff options
| author | Tom Rini <[email protected]> | 2026-05-12 15:41:52 -0600 |
|---|---|---|
| committer | Tom Rini <[email protected]> | 2026-05-12 15:41:52 -0600 |
| commit | e3e651c480c46b332f16a7555b97c6c6fd640a40 (patch) | |
| tree | 01f7277b103a10ef301e6981d7f4ca2e33f653db /test/lib/string.c | |
| parent | 5732bd0f457b4c671e46574d64d4acb099c0f0a5 (diff) | |
| parent | 8d209186a1e4aca4ec44745d05d51de7e80f7e3e (diff) | |
Merge patch series "add memdup_nul(), use it and memdup() in a few places"
Rasmus Villemoes <[email protected]> says:
There are quite a few places where we allocate X+1 bytes, initialize
the first X bytes via memcpy() and then set the last byte to 0.
The kernel has a helper for that, kmemdup_nul(). Introduce a similar
one, and start making use of it in a few places. Also the existing
memdup() helper can be put to more use.
There are lots more places one could modify. But for code shared with
host tools, one would need to do some refactoring, putting memdup()
and memdup_nul() in their own str-util.c TU which could then also be
included in the tools build.
Link: https://lore.kernel.org/r/[email protected]
Diffstat (limited to 'test/lib/string.c')
| -rw-r--r-- | test/lib/string.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/test/lib/string.c b/test/lib/string.c index f56c2e4c946..db6f28dbfdf 100644 --- a/test/lib/string.c +++ b/test/lib/string.c @@ -223,6 +223,40 @@ static int lib_memdup(struct unit_test_state *uts) } LIB_TEST(lib_memdup, 0); +/** lib_memdup_nul() - unit test for memdup_nul() */ +static int lib_memdup_nul(struct unit_test_state *uts) +{ + char buf[BUFLEN]; + size_t len; + char *p, *q; + + /* Zero size should return a buffer containing a single nul byte */ + p = memdup_nul(NULL, 0); + ut_assertnonnull(p); + ut_assert(p[0] == '\0'); + free(p); + + p = memdup_nul(buf, 0); + ut_assertnonnull(p); + ut_assert(p[0] == '\0'); + free(p); + + strcpy(buf, TEST_STR); + len = sizeof(TEST_STR); + p = memdup_nul(buf, len); + ut_asserteq_mem(p, buf, len); + ut_assert(p[len] == '\0'); + + q = memdup_nul(p, len); + ut_asserteq_mem(q, buf, len); + ut_assert(q[len] == '\0'); + free(q); + free(p); + + return 0; +} +LIB_TEST(lib_memdup_nul, 0); + /** lib_strnstr() - unit test for strnstr() */ static int lib_strnstr(struct unit_test_state *uts) { |
