From 18bf8d6c07f086e49c75552e13e97e8895c2de00 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Mon, 27 May 2024 13:13:38 +0200
Subject: [PATCH] tests: Don't test on the broken NetBSD 10.0 zh_CN.GB18030
 locale.

* m4/locale-zh.m4 (gt_LOCALE_ZH_CN): Add a sanity check with mbrtowc.
---
 ChangeLog       |  5 +++++
 m4/locale-zh.m4 | 18 ++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 893510feb4..029d9786a9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2024-05-27  Bruno Haible  <bruno@clisp.org>
+
+	tests: Don't test on the broken NetBSD 10.0 zh_CN.GB18030 locale.
+	* m4/locale-zh.m4 (gt_LOCALE_ZH_CN): Add a sanity check with mbrtowc.
+
 2024-05-25  Bruno Haible  <bruno@clisp.org>
 
 	euidaccess: Fix test failure on native Windows.
diff --git a/m4/locale-zh.m4 b/m4/locale-zh.m4
index 94c545906f..aad647e6cd 100644
--- a/m4/locale-zh.m4
+++ b/m4/locale-zh.m4
@@ -1,5 +1,5 @@
-# locale-zh.m4 serial 16
-dnl Copyright (C) 2003, 2005-2023 Free Software Foundation, Inc.
+# locale-zh.m4 serial 16.1
+dnl Copyright (C) 2003, 2005-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
@@ -21,6 +21,7 @@ AC_DEFUN_ONCE([gt_LOCALE_ZH_CN],
 #endif
 #include <stdlib.h>
 #include <string.h>
+#include <wchar.h>
 struct tm t;
 char buf[16];
 int main ()
@@ -79,6 +80,19 @@ int main ()
      single wide character.  This excludes the GB2312 and GBK encodings.  */
   if (mblen ("\203\062\332\066", 5) != 4)
     return 1;
+  /* Check whether mbrtowc accept this character one byte at a time.
+     This excludes NetBSD 10.0.  */
+  if (sizeof (wchar_t) > 2)
+    {
+      wchar_t wc;
+      mbstate_t state;
+      memset (&state, 0, sizeof (state));
+      if (!(mbrtowc (&wc, "\203", 1, &state) == (size_t)(-2)
+            && mbrtowc (&wc, "\062", 1, &state) == (size_t)(-2)
+            && mbrtowc (&wc, "\332", 1, &state) == (size_t)(-2)
+            && mbrtowc (&wc, "\066", 1, &state) == 1))
+        return 1;
+    }
   return 0;
 #endif
 }
-- 
2.39.5