Fix: a potential crash on failure to setlocale
authorlixiaokeng <lixiaokeng@huawei.com>
Wed, 16 Jun 2021 08:57:16 +0000 (16:57 +0800)
committerNikolaus Rath <Nikolaus@rath.org>
Wed, 16 Jun 2021 17:25:14 +0000 (18:25 +0100)
setlocale() can fail, returning NULL, which will lead
to a crash in iconv_new(). Fix it like in iconv_help().

Signed-off-by: Lixiaokeng <lixiaokeng@huawei.com>
lib/modules/iconv.c

index 76e2f7d0e21d566b3525c714f3233016af7753d8..0ec3c2bd1a34228dfbb788575dedfef41c1f3eca 100755 (executable)
@@ -672,7 +672,7 @@ static struct fuse_fs *iconv_new(struct fuse_args *args,
 {
        struct fuse_fs *fs;
        struct iconv *ic;
-       char *old = NULL;
+       const char *old = NULL;
        const char *from;
        const char *to;
 
@@ -694,7 +694,7 @@ static struct fuse_fs *iconv_new(struct fuse_args *args,
        to = ic->to_code ? ic->to_code : "";
        /* FIXME: detect charset equivalence? */
        if (!to[0])
-               old = strdup(setlocale(LC_CTYPE, ""));
+               old = setlocale(LC_CTYPE, "");
        ic->tofs = iconv_open(from, to);
        if (ic->tofs == (iconv_t) -1) {
                fuse_log(FUSE_LOG_ERR, "fuse-iconv: cannot convert from %s to %s\n",
@@ -709,7 +709,6 @@ static struct fuse_fs *iconv_new(struct fuse_args *args,
        }
        if (old) {
                setlocale(LC_CTYPE, old);
-               free(old);
                old = NULL;
        }
 
@@ -730,7 +729,6 @@ out_free:
        free(ic);
        if (old) {
                setlocale(LC_CTYPE, old);
-               free(old);
        }
        return NULL;
 }