Attention is currently required from: Jakub Czapiga, Maximilian Brune, Yu-Ping Wu.
Julius Werner has posted comments on this change by Yu-Ping Wu. ( https://review.coreboot.org/c/coreboot/+/83830?usp=email )
Change subject: commonlib/bsd: Add strnlen() and strlen() functions ......................................................................
Patch Set 1: Code-Review+2
(3 comments)
File payloads/libpayload/libc/string.c:
https://review.coreboot.org/c/coreboot/+/83830/comment/de0c20d8_f6aeeb3d?usp... : PS1, Line 71: /* NULL and empty strings have length 0. */ : if (!str) : return 0;
You don't have this NULL check in the commonlib implementation. […]
There's no NULL check required here by POSIX and other major libc implementations (e.g. glibc: https://github.com/bminor/glibc/blob/master/sysdeps/i386/strlen.c) don't seem to do that. I see no reason why libpayload should do something completely out of turn here, and this seems like as good an opportunity as any to fix that. (It's arguable whether returning 0 is even "better" here. If a caller calls `strlen()` on `NULL` that's always a bug anyway and just silently returning a result that's as incorrect as any other doesn't necessarily make things better.)
File src/commonlib/bsd/string.c:
https://review.coreboot.org/c/coreboot/+/83830/comment/b4e3a6b5_f9174799?usp... : PS1, Line 12: return len; nit: This is probably overthinking it, but I get a shorter loop in assembly with ``` const char *ptr = 0; while (*ptr++) /* walk string */; return ptr - str - 1; ```
https://review.coreboot.org/c/coreboot/+/83830/comment/194d8b21_563d6938?usp... : PS1, Line 20: return len; Here it would be ``` const char *ptr = str; const char *end = str + n + 1; while (*ptr++ && ptr < end) /* walk string */; return ptr - str - 1; ``` (I know that makes readability suffer a bit so maybe it's not a good idea. But in general, anything that it can rewrite to check a condition at the end tends to save a jump in the loop. It also seems be able to fit in the post-increment better when it happens at the start (especially on AArch64), and there's no separate counter it could use to calculate offsets instead.)