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/tiny_strerror: add tiny_strerror_minimal #18768

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ PSEUDOMODULES += suit_transport_%
PSEUDOMODULES += suit_storage_%
PSEUDOMODULES += sys_bus_%
PSEUDOMODULES += tiny_strerror_as_strerror
PSEUDOMODULES += tiny_strerror_minimal
PSEUDOMODULES += vdd_lc_filter_%
## @defgroup pseudomodule_vfs_auto_format vfs_auto_format
## @brief Format mount points at startup unless they can be mounted
Expand Down
7 changes: 7 additions & 0 deletions sys/include/tiny_strerror.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
* @note Using module `tiny_strerror_as_strerror` will replace all calls
* to `strerror()` by calls to `tiny_strerror()`, which may safe
* a bit of ROM.
*
* @note Using module `tiny_strerror_minimal` will just print the error
* code value.
* This will save ~1k of ROM, but won't provide much more information.
*
* @warning The module `tiny_strerror_minimal` is not thread-safe.
*
* @{
*
*
Expand Down
7 changes: 7 additions & 0 deletions sys/tiny_strerror/tiny_strerror.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "kernel_defines.h"
Expand Down Expand Up @@ -112,6 +113,12 @@ const char *tiny_strerror(int errnum)
const char *retval = "-unknown";
unsigned offset = 1;

if (IS_USED(MODULE_TINY_STRERROR_MINIMAL)) {
static char buf[4];
Copy link
Member

Choose a reason for hiding this comment

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

This is not thread-safe. I don't think this is much of an issue, as in few cases more than one thread cares about user-friendly error messages. And if so they would have to interrupt each other during error reporting. And even then likely they report errors via stdio, which gets garbled anyway when two threads concurrently write to it.

Still, I think this is something we need to point out clearly with a big fat @warning.

snprintf(buf, sizeof(buf), "%d", errnum);
return buf;
}

if (errnum <= 0) {
offset = 0;
errnum = -errnum;
Expand Down