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: fix compilation on LLVM #20483

Merged
merged 1 commit into from
Mar 20, 2024
Merged
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
sys/tiny_strerror: fix compilation on LLVM
Mixing address spaces is something LLVM doesn't like (for good
reason). This re-organized the code a bit so that this does not
happen anymore, even on AVR.

Split out of #16924
  • Loading branch information
maribu committed Mar 19, 2024
commit 7775fa7de57e1f030319752978367102f2cd794e
34 changes: 16 additions & 18 deletions sys/tiny_strerror/tiny_strerror.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,37 +215,35 @@ static FLASH_ATTR const char * FLASH_ATTR const lookup[] = {

const char *tiny_strerror(int errnum)
{
/* dark magic: All error strings start with a "-". For positive error codes
* an offset of 1 is added to the address of the string, jumping one char
* behind the "-". This way the strings do not have to be allocated twice
* (once with and once without minus char).
*/
const char *retval = "-unknown";
Teufelchen1 marked this conversation as resolved.
Show resolved Hide resolved
unsigned offset = 1;

if (IS_USED(MODULE_TINY_STRERROR_MINIMAL)) {
static char buf[4];
snprintf(buf, sizeof(buf), "%d", errnum);
return buf;
}

/* dark magic: All error strings start with a "-". For positive error codes
* an offset of 1 is added to the address of the string, jumping one char
* behind the "-". This way the strings do not have to be allocated twice
* (once with and once without minus char).
*/
unsigned offset = 1;
if (errnum <= 0) {
offset = 0;
errnum = -errnum;
}

if (((unsigned)errnum < ARRAY_SIZE(lookup))
&& (lookup[(unsigned)errnum] != NULL)) {
retval = lookup[(unsigned)errnum];
if (((unsigned)errnum >= ARRAY_SIZE(lookup))
|| (lookup[(unsigned)errnum] == NULL)) {
return "unknown";
}

if (IS_ACTIVE(HAS_FLASH_UTILS_ARCH)) {
static char buf[16];
flash_strncpy(buf, retval + offset, sizeof(buf));
return buf;
}

return retval + offset;
#if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
static char buf[16];
flash_strncpy(buf, lookup[(unsigned)errnum] + offset, sizeof(buf));
return buf;
#else
return lookup[(unsigned)errnum] + offset;
#endif
}

#if IS_USED(MODULE_TINY_STRERROR_AS_STRERROR)
Expand Down
Loading