From: Bruno Haible <bruno@clisp.org>
Date: Thu, 30 Jul 2020 01:29:29 +0000 (+0200)
Subject: unicodeio: Fix wrong result on musl libc.
X-Git-Tag: v1.0~3842
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=0b273fff2f1bcec210669dad3a4eaaafe1089aba;p=gnulib.git

unicodeio: Fix wrong result on musl libc.

Reported by A. Wilcox <awilfox@adelielinux.org> in
<https://www.openwall.com/lists/musl/2020/07/29/2>.

* lib/unicodeio.c (unicode_to_mb): Handle asterisk fallback characters
on musl libc.
* m4/unicodeio.m4 (gl_UNICODEIO): Invoke gl_MUSL_LIBC.
* modules/unicodeio (Files): Add m4/musl.m4.
---

diff --git a/ChangeLog b/ChangeLog
index c5fd79351f..9b9f044f9b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2020-07-29  Bruno Haible  <bruno@clisp.org>
+
+	unicodeio: Fix wrong result on musl libc.
+	Reported by A. Wilcox <awilfox@adelielinux.org> in
+	<https://www.openwall.com/lists/musl/2020/07/29/2>.
+	* lib/unicodeio.c (unicode_to_mb): Handle asterisk fallback characters
+	on musl libc.
+	* m4/unicodeio.m4 (gl_UNICODEIO): Invoke gl_MUSL_LIBC.
+	* modules/unicodeio (Files): Add m4/musl.m4.
+
 2020-07-29  Paul Eggert  <eggert@cs.ucla.edu>
 
 	fsusage, regex, stat-size: remove Cray support
diff --git a/lib/unicodeio.c b/lib/unicodeio.c
index b616e3d61f..7d5e1fbcd2 100644
--- a/lib/unicodeio.c
+++ b/lib/unicodeio.c
@@ -130,7 +130,7 @@ unicode_to_mb (unsigned int code,
                    (ICONV_CONST char **)&inptr, &inbytesleft,
                    &outptr, &outbytesleft);
       if (inbytesleft > 0 || res == (size_t)(-1)
-          /* Irix iconv() inserts a NUL byte if it cannot convert. */
+          /* Irix iconv() inserts a NUL byte if it cannot convert.  */
 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
           || (res > 0 && code != 0 && outptr - outbuf == 1 && *outbuf == '\0')
 # endif
@@ -138,6 +138,10 @@ unicode_to_mb (unsigned int code,
              convert.  */
 # if !defined _LIBICONV_VERSION && (defined __NetBSD__ || defined __sun)
           || (res > 0 && outptr - outbuf == 1 && *outbuf == '?')
+# endif
+          /* musl libc iconv() inserts a '*' if it cannot convert.  */
+# if !defined _LIBICONV_VERSION && MUSL_LIBC
+          || (res > 0 && outptr - outbuf == 1 && *outbuf == '*')
 # endif
          )
         return failure (code, NULL, callback_arg);
diff --git a/m4/unicodeio.m4 b/m4/unicodeio.m4
index 11bcd180d1..a2e9bf0270 100644
--- a/m4/unicodeio.m4
+++ b/m4/unicodeio.m4
@@ -1,4 +1,4 @@
-# unicodeio.m4 serial 2
+# unicodeio.m4 serial 3
 dnl Copyright (C) 2002-2003, 2009-2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,6 +6,6 @@ dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_UNICODEIO],
 [
-  dnl No prerequisites of lib/unicodeio.c.
-  :
+  dnl Prerequisites of lib/unicodeio.c.
+  gl_MUSL_LIBC
 ])
diff --git a/modules/unicodeio b/modules/unicodeio
index 51721db72d..d7a3a4a95d 100644
--- a/modules/unicodeio
+++ b/modules/unicodeio
@@ -5,6 +5,7 @@ Files:
 lib/unicodeio.h
 lib/unicodeio.c
 m4/unicodeio.m4
+m4/musl.m4
 
 Depends-on:
 unistr/u8-uctomb