-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sys/string_utils: add strscpy() #18621
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice one!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, whether or not the assert(0)
is there is bike-shedding. As long as the API says "do not call it with a zero-length dest buffer, as you won't get a zero terminated string in dest
" the API is as safe to use as it gets with C.
Thanks for this addition! :)
The guard in #if !defined(BOARD_NATIVE) \
&& !(IS_USED(MODULE_PICOLIBC) && __BSD_VISIBLE) \
&& !(IS_USED(MODULE_NEWLIB) && __BSD_VISIBLE && !defined(MCU_ESP8266))
[...]
static inline void explicit_bzero(void *dest, size_t n_bytes)
[...]
#endif doesn't trigger |
But |
Contribution description
strncpy
is known to be dangerous as it will truncate the string without inserting a terminating zero.The replacement
strlcpy
had the problem that the return value had to be compared to the destination buffer to detect truncation.In Linux the consensus now seems to have settled on
strscpy()
which returns-E2BIG
if the string was truncated.https://lwn.net/Articles/905777/
Testing procedure
Issues/PRs references