]> granicus.if.org Git - file/commitdiff
don't use setlocale(3); it is not thread-safe.
authorChristos Zoulas <christos@zoulas.com>
Wed, 10 Sep 2014 18:41:51 +0000 (18:41 +0000)
committerChristos Zoulas <christos@zoulas.com>
Wed, 10 Sep 2014 18:41:51 +0000 (18:41 +0000)
configure.ac
src/file.c
src/file.h
src/funcs.c
src/readcdf.c
src/softmagic.c

index 3d55c38c22dd8c6c3bc77556b9dfa775d00f44dc..9483bddfc81cb82e6bcd3e7486165fda33bae804 100644 (file)
@@ -138,7 +138,7 @@ else
 fi])
 
 dnl Checks for functions
-AC_CHECK_FUNCS(strerror strndup strtoul mkstemp mkostemp utimes utime wcwidth strtof)
+AC_CHECK_FUNCS(strerror strndup strtoul mkstemp mkostemp utimes utime wcwidth strtof newlocale uselocale freelocale)
 
 dnl Provide implementation of some required functions if necessary
 AC_REPLACE_FUNCS(getopt_long asprintf vasprintf strlcpy strlcat getline ctime_r asctime_r pread strcasestr fmtcheck)
index 370da91462e163b3fd360d69c5bbd0225694052f..b18fcbbd12cde1223b3bfaf49d71386992ba88f8 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.153 2014/02/11 15:41:04 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.154 2014/09/10 18:41:51 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -54,9 +54,6 @@ FILE_RCSID("@(#)$File: file.c,v 1.153 2014/02/11 15:41:04 christos Exp $")
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>    /* for read() */
 #endif
-#ifdef HAVE_LOCALE_H
-#include <locale.h>
-#endif
 #ifdef HAVE_WCHAR_H
 #include <wchar.h>
 #endif
index 06c7abef0e3033aa37af7d418737324089126926..b8056f030000825a27bc80166f31c20a5327e49c 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.153 2014/08/04 06:19:44 christos Exp $
+ * @(#)$File: file.h,v 1.154 2014/09/10 18:41:51 christos Exp $
  */
 
 #ifndef __file_h__
@@ -471,9 +471,17 @@ protected int file_os2_apptype(struct magic_set *, const char *, const void *,
     size_t);
 #endif /* __EMX__ */
 
+#if defined(HAVE_LOCALE_H)
+#include <locale.h>
+#endif
+
 typedef struct {
        const char *pat;
-       char *old_lc_ctype;
+#if defined(HAVE_NEWLOCALE) && defined(HAVE_USELOCALE) && defined(HAVE_FREELOCALE)
+#define USE_C_LOCALE
+       locale_t old_lc_ctype;
+       locale_t c_lc_ctype;
+#endif
        int rc;
        regex_t rx;
 } file_regex_t;
index 3a2f67c4dab8a2908fabc95faa9ffe1023165f6c..d7cce55ff4f6a026e61e2b78ee865ddcb30fe535 100644 (file)
@@ -27,7 +27,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.72 2014/05/14 23:15:42 christos Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.73 2014/09/10 18:41:51 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -45,9 +45,6 @@ FILE_RCSID("@(#)$File: funcs.c,v 1.72 2014/05/14 23:15:42 christos Exp $")
 #if defined(HAVE_LIMITS_H)
 #include <limits.h>
 #endif
-#if defined(HAVE_LOCALE_H)
-#include <locale.h>
-#endif
 
 #ifndef SIZE_MAX
 #define SIZE_MAX       ((size_t)~0)
@@ -455,13 +452,14 @@ out:
 protected int
 file_regcomp(file_regex_t *rx, const char *pat, int flags)
 {
-       rx->old_lc_ctype = setlocale(LC_CTYPE, NULL);
-       assert(rx->old_lc_ctype != NULL);
-       rx->old_lc_ctype = strdup(rx->old_lc_ctype);
+#ifdef USE_C_LOCALE
+       rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
+       assert(rx->c_lc_ctype != NULL);
+       rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
        assert(rx->old_lc_ctype != NULL);
+#endif
        rx->pat = pat;
 
-       (void)setlocale(LC_CTYPE, "C");
        return rx->rc = regcomp(&rx->rx, pat, flags);
 }
 
@@ -478,8 +476,10 @@ file_regfree(file_regex_t *rx)
 {
        if (rx->rc == 0)
                regfree(&rx->rx);
-       (void)setlocale(LC_CTYPE, rx->old_lc_ctype);
-       free(rx->old_lc_ctype);
+#ifdef USE_C_LOCALE
+       (void)uselocale(rx->old_lc_ctype);
+       freelocale(rx->c_lc_ctype);
+#endif
 }
 
 protected void
index 5d9b672f6263f20f190dc7501a0bf7e827b09c6f..0681acab02eaefff837d4182690261099bfbc80d 100644 (file)
@@ -26,7 +26,7 @@
 #include "file.h"
 
 #ifndef lint
-FILE_RCSID("@(#)$File: readcdf.c,v 1.47 2014/08/27 13:00:37 christos Exp $")
+FILE_RCSID("@(#)$File: readcdf.c,v 1.48 2014/09/10 18:41:51 christos Exp $")
 #endif
 
 #include <assert.h>
@@ -35,9 +35,6 @@ FILE_RCSID("@(#)$File: readcdf.c,v 1.47 2014/08/27 13:00:37 christos Exp $")
 #include <string.h>
 #include <time.h>
 #include <ctype.h>
-#if defined(HAVE_LOCALE_H)
-#include <locale.h>
-#endif
 
 #include "cdf.h"
 #include "magic.h"
@@ -107,20 +104,23 @@ cdf_app_to_mime(const char *vbuf, const struct nv *nv)
 {
        size_t i;
        const char *rv = NULL;
-       char *old_lc_ctype;
+#ifdef USE_C_LOCALE
+       locale_t old_lc_ctype, c_lc_ctype;
 
-       old_lc_ctype = setlocale(LC_CTYPE, NULL);
-       assert(old_lc_ctype != NULL);
-       old_lc_ctype = strdup(old_lc_ctype);
+       c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
+       assert(c_lc_ctype != NULL);
+       old_lc_ctype = uselocale(c_lc_ctype);
        assert(old_lc_ctype != NULL);
-       (void)setlocale(LC_CTYPE, "C");
+#endif
        for (i = 0; nv[i].pattern != NULL; i++)
                if (strcasestr(vbuf, nv[i].pattern) != NULL) {
                        rv = nv[i].mime;
                        break;
                }
-       (void)setlocale(LC_CTYPE, old_lc_ctype);
-       free(old_lc_ctype);
+#ifdef USE_C_LOCALE
+       (void)uselocale(old_lc_ctype);
+       freelocale(c_lc_ctype);
+#endif
        return rv;
 }
 
index afbe90844c2bb61ff397742327d5fbacfcce029a..0c60e80f5dd2805f0fc52c25143f406f9212b539 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.192 2014/08/30 15:30:11 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.193 2014/09/10 18:41:51 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -41,10 +41,6 @@ FILE_RCSID("@(#)$File: softmagic.c,v 1.192 2014/08/30 15:30:11 christos Exp $")
 #include <ctype.h>
 #include <stdlib.h>
 #include <time.h>
-#if defined(HAVE_LOCALE_H)
-#include <locale.h>
-#endif
-
 
 private int match(struct magic_set *, struct magic *, uint32_t,
     const unsigned char *, size_t, size_t, int, int, int, int, int *, int *,