1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:04:59 +00:00

LibC: Implement strerror_r()

This implements the XSI-compliant version of strerror_r() - as opposed
to the GNU-specific variant.

The function explicitly saves errno so as to not accidentally change it
with one of the calls to other functions.
This commit is contained in:
Gunnar Beutner 2021-05-24 16:43:12 +02:00 committed by Andreas Kling
parent 3526fbbc5f
commit c81b3e1ee3
4 changed files with 40 additions and 0 deletions

View file

@ -354,6 +354,26 @@ const char* const sys_errlist[] = {
int sys_nerr = EMAXERRNO;
int strerror_r(int errnum, char* buf, size_t buflen)
{
auto saved_errno = errno;
if (errnum >= EMAXERRNO) {
auto rc = strlcpy(buf, "unknown error", buflen);
if (rc >= buflen)
dbgln("strerror_r(): Invalid error number '{}' specified and the output buffer is too small.", errnum);
errno = saved_errno;
return EINVAL;
}
auto text = strerror(errnum);
auto rc = strlcpy(buf, text, buflen);
if (rc >= buflen) {
errno = saved_errno;
return ERANGE;
}
errno = saved_errno;
return 0;
}
char* strerror(int errnum)
{
if (errnum < 0 || errnum >= EMAXERRNO) {