diff options
author | Mattias Andrée <maandree@kth.se> | 2021-03-05 23:30:24 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-03-05 23:30:24 +0100 |
commit | 80a78b91c2905fcacf0236d63a9f4e3d7e576d7e (patch) | |
tree | 305419312ba536e041be28bb271118e0ccf6956b /libgamma_strerror_r.c | |
parent | m (diff) | |
download | libgamma-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.c | 21 |
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; } |