aboutsummaryrefslogtreecommitdiffstats
path: root/libgamma_strerror_r.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2021-03-05 23:30:24 +0100
committerMattias Andrée <maandree@kth.se>2021-03-05 23:30:24 +0100
commit80a78b91c2905fcacf0236d63a9f4e3d7e576d7e (patch)
tree305419312ba536e041be28bb271118e0ccf6956b /libgamma_strerror_r.c
parentm (diff)
downloadlibgamma-80a78b91c2905fcacf0236d63a9f4e3d7e576d7e.tar.gz
libgamma-80a78b91c2905fcacf0236d63a9f4e3d7e576d7e.tar.bz2
libgamma-80a78b91c2905fcacf0236d63a9f4e3d7e576d7e.tar.xz
minor fixes + add tests
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libgamma_strerror_r.c')
-rw-r--r--libgamma_strerror_r.c21
1 files changed, 10 insertions, 11 deletions
diff --git a/libgamma_strerror_r.c b/libgamma_strerror_r.c
index 781558c..c95d8a9 100644
--- a/libgamma_strerror_r.c
+++ b/libgamma_strerror_r.c
@@ -26,16 +26,14 @@ libgamma_strerror_r(int error_code, char buf[], size_t bufsize)
switch (error_code) {
#define X(NAME, DESC)\
case NAME:\
- desc = DESC;\
- break;
+ return DESC;
LIST_ERRORS(X)
#undef X
default:
- desc = NULL;
break;
}
- if (desc)
- return desc;
+ if (bufsize)
+ *buf = '\0';
if (error_code >= 0) {
saved_errno = errno;
desc = _Generic(strerror_r(error_code, buf, bufsize),
@@ -43,19 +41,20 @@ libgamma_strerror_r(int error_code, char buf[], size_t bufsize)
int: (errno = (int)(intptr_t)strerror_r(error_code, buf, bufsize)) ? NULL : buf,
/* GNU strerror_r */
char *: (char *)(intptr_t)strerror_r(error_code, buf, bufsize));
+ if (desc == buf && (!bufsize || !*buf))
+ desc = NULL;
if (desc) {
errno = saved_errno;
- if (!buf || strcmp(buf, "No error information"))
- return buf;
+ if (!desc || strcmp(desc, "No error information"))
+ return desc;
} else if (errno == ERANGE) {
return buf;
} else {
errno = saved_errno;
}
}
- if (!buf)
- return buf;
- *buf = '\0'; /* TODO what happended here? */
- snprintf(buf, bufsize, "Unknown error #%i\n", error_code);
+ if (!bufsize)
+ return NULL;
+ snprintf(buf, bufsize, "Unknown error #%i", error_code);
return buf;
}