coreboot/payloads/libpayload/libc
Julius Werner 8097809c8a libpayload: Fix strsep() edge cases
Our strsep() function is slightly incorrect in that it leaves the
`stringp` pointer pointing to the terminating NUL byte after parsing the
last token. The man page for official implementations says:

> In case no delimiter was found, the token is taken to be the entire
> string *stringp, and *stringp is made NULL.

This doesn't affect things in practice much because we also
(incorrectly) return NULL when called with `**stringp == '\0'`, meaning
the usual pattern of calling `strsep()` in a row without checking
results first still works when there are less tokens than expected,
since we terminate early from that case instead. But it does break the
edge cases where the caller wants to check if there were extra bytes
beyond the last token (`stringp == NULL`), and where we call `strsep()`
on a pointer pointing directly to a terminating NUL byte already
(supposed to return an empty string but our implementation actually
returns NULL). It doesn't look like these edge cases occur anywhere in
current libpayload or depthcharge code.

This patch fixes the issue and also adds a unit test to ensure it
remains correct in the future. (Also move the definition of the `errno`
variable from lib.c into string.c, because `perror()` in string.c is the
only function that actually needs that, and the crazy linker error you
get when only linking one but not the other into a test will waste you
half an hour to figure out.)

Change-Id: I610b5117710c110bcba4fac2a0bb6c13f4f8d046
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/88729
Reviewed-by: Yu-Ping Wu <yupingso@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
2025-08-13 17:44:31 +00:00
..
args.c treewide: Remove "this file is part of" lines 2020-05-11 17:11:40 +00:00
console.c treewide: Remove "this file is part of" lines 2020-05-11 17:11:40 +00:00
coreboot.c payloads: Propagate SPI flash address mode flag to libpayload 2025-06-28 02:40:48 +00:00
ctype.c treewide: Remove "this file is part of" lines 2020-05-11 17:11:40 +00:00
fmap.c libpayload: Remove legacy CBFS API 2024-02-22 21:18:39 +00:00
fpmath.c libpayload: Add simple 32.32 fixed-point math API 2020-07-09 00:32:11 +00:00
getopt_long.c commonlib: Change GCD function to always use 64 bits 2024-02-08 13:13:58 +00:00
hexdump.c treewide: Remove "this file is part of" lines 2020-05-11 17:11:40 +00:00
lib.c libpayload: Fix strsep() edge cases 2025-08-13 17:44:31 +00:00
libgcc.c treewide: Remove "this file is part of" lines 2020-05-11 17:11:40 +00:00
lp_vboot.c libpayload: Add vboot and reboot utility functions 2023-09-18 15:42:31 +00:00
Makefile.mk libpayload: Unify selfboot() implementations 2025-02-11 20:59:57 +00:00
malloc.c libpayload: Add dma_allocator_range() 2023-11-21 13:50:53 +00:00
memory.c libpayload: Use Kconfig for architecture memcpy, not weak symbols 2025-05-22 19:27:24 +00:00
printf.c libpayload: Fix clang fallthrough warning 2023-08-28 02:12:25 +00:00
qsort.c
rand.c treewide: Remove "this file is part of" lines 2020-05-11 17:11:40 +00:00
readline.c treewide: Remove "this file is part of" lines 2020-05-11 17:11:40 +00:00
selfboot.c libpayload: Unify selfboot() implementations 2025-02-11 20:59:57 +00:00
string.c libpayload: Fix strsep() edge cases 2025-08-13 17:44:31 +00:00
strlcpy.c
sysinfo.c treewide: Remove "this file is part of" lines 2020-05-11 17:11:40 +00:00
time.c libpayload: Use Kconfig instead of weak symbol for arch_ndelay() 2025-05-22 19:27:30 +00:00