Skip to content
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

Merged
merged 4 commits into from
Sep 28, 2022
Merged

sys/string_utils: add strscpy() #18621

merged 4 commits into from
Sep 28, 2022

Conversation

benpicco
Copy link
Contributor

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

@benpicco benpicco requested a review from miri64 as a code owner September 21, 2022 14:33
@github-actions github-actions bot added Area: build system Area: Build system Area: sys Area: System Area: tests Area: tests and testing framework labels Sep 21, 2022
Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one!

Copy link
Member

@maribu maribu left a 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! :)

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 21, 2022
@benpicco benpicco added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Sep 23, 2022
@github-actions github-actions bot added the Area: Kconfig Area: Kconfig integration label Sep 26, 2022
@benpicco benpicco added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Sep 27, 2022
@benpicco benpicco enabled auto-merge September 27, 2022 23:16
@maribu
Copy link
Member

maribu commented Sep 28, 2022

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 tests_external_board_dirs for the custom board native2. I wonder if just adding an CFLAGS += -DBOARD_NATIVE to the test is a sensible workaround. Adding an && !defined(BOARD_NATIVE2) just for a test app seems to be the wrong approach to me.

@benpicco
Copy link
Contributor Author

But CPU_NATIVE should work?

@benpicco benpicco merged commit c35a4ba into RIOT-OS:master Sep 28, 2022
@benpicco benpicco deleted the strscpy branch September 28, 2022 20:49
@maribu maribu added this to the Release 2022.10 milestone Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: Kconfig Area: Kconfig integration Area: sys Area: System Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants