Skip to content

Commit

Permalink
fix: make zfs_strerror really thread-safe and portable
Browse files Browse the repository at this point in the history
  • Loading branch information
rkojedzinszky committed Jan 3, 2025
1 parent d35f9f2 commit 675724c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/user.m4
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AC_DEFUN([ZFS_AC_CONFIG_USER], [
ZFS_AC_CONFIG_USER_MAKEDEV_IN_MKDEV
ZFS_AC_CONFIG_USER_ZFSEXEC
AC_CHECK_FUNCS([execvpe issetugid mlockall strerror_l strlcat strlcpy gettid])
AC_CHECK_FUNCS([execvpe issetugid mlockall strlcat strlcpy gettid])
AC_SUBST(RM)
])
15 changes: 9 additions & 6 deletions include/libzutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#define _LIBZUTIL_H extern __attribute__((visibility("default")))

#include <string.h>
#include <locale.h>
#include <pthread.h>
#include <sys/nvpair.h>
#include <sys/fs/zfs.h>

Expand Down Expand Up @@ -276,11 +276,14 @@ _LIBZUTIL_H void update_vdev_config_dev_sysfs_path(nvlist_t *nv,
* Thread-safe strerror() for use in ZFS libraries
*/
static inline char *zfs_strerror(int errnum) {
#ifdef HAVE_STRERROR_L
return (strerror_l(errnum, uselocale(0)));
#else
return (strerror(errnum));
#endif
static __thread char errbuf[2048];
static __thread pthread_mutex_t zfs_strerror_lock = PTHREAD_MUTEX_INITIALIZER;

Check failure on line 280 in include/libzutil.h

View workflow job for this annotation

GitHub Actions / checkstyle

line > 80 characters

pthread_mutex_lock(&zfs_strerror_lock);
strlcpy(errbuf, strerror(errnum), sizeof(errbuf));

Check failure on line 283 in include/libzutil.h

View workflow job for this annotation

GitHub Actions / checkstyle

missing space between keyword and paren
pthread_mutex_unlock(&zfs_strerror_lock);

return errbuf;

Check failure on line 286 in include/libzutil.h

View workflow job for this annotation

GitHub Actions / checkstyle

unparenthesized return expression
}

#ifdef __cplusplus
Expand Down

0 comments on commit 675724c

Please sign in to comment.