[PATCH 0/4] tools/nolibc: Misc fixes for strlcpy() and strlcat()

From: Rodrigo Campos
Date: Mon Jan 29 2024 - 09:16:10 EST


As requested by Willy and Thomas[1], here go some more fixes and tests for
strlcpy() and strlcat().

>From the previous discussion, I uderstand that we want to apply the first patch
(export strlen()) as is, I've included it again here just in case. Maybe we should just include the
-Wl,--gc-sections in the nolibc.h comment about the flags to use when compiling?

The rest of the commits are quite simple too, they just:
* Fix the return code of both functions
* Make sure to always null-terminate the dst buffer
* Don't copy more than what size allows us (this handles the size=0 case
for free too)

All has been checked against the corresponding libbsd implementation[2].

I thought the manpage was clear, but when checking against that, I noted a few
twists (like the manpage says the return code of strlcat is strlen(src) +
strlen(dst), but it was not clear it is not that if size < strlen(dst). When
looking at the libbsd implementation and re-reading the manpage, I understood
what it really meant).

Let me know what you think :)

Best,
Rodrigo


[1]: https://lore.kernel.org/all/9538a8fe-b92f-42a5-99d1-25969cf51647@xxxxxxxxxxx/
[2]: https://gitlab.freedesktop.org/libbsd/libbsd.git


Rodrigo Campos (4):
tools/nolibc/string: export strlen()
tools/nolibc: Fix strlcat() return code and size usage
tools/nolibc: Fix strlcpy() return code and size usage
selftests/nolibc: Add tests for strlcat() and strlcpy()

tools/include/nolibc/string.h | 25 +++++++-------
tools/testing/selftests/nolibc/nolibc-test.c | 34 ++++++++++++++++++++
2 files changed, 47 insertions(+), 12 deletions(-)

--
2.43.0