ialloc: Add comments

2023-03-27 Thread Bruno Haible
For the string-desc module, I need to make use of the 'ialloc' module.
Which is hard if its functions are not documented. I mean, as a user
of imalloc(), I'm not supposed to look into the module description
in order to understand that this function returns non-NULL for a
zero-sized allocation. Things like that should be stated as comments.

Done as follows:


2023-03-27  Bruno Haible  

ialloc: Add comments.
* lib/ialloc.h (imalloc, irealloc, icalloc, ireallocarray): Add
comments.

diff --git a/lib/ialloc.h b/lib/ialloc.h
index 1d43faf388..275237ccb1 100644
--- a/lib/ialloc.h
+++ b/lib/ialloc.h
@@ -43,6 +43,9 @@ _gl_alloc_nomem (void)
   return NULL;
 }
 
+/* imalloc (size) is like malloc (size).
+   It returns a non-NULL pointer to size bytes of memory.
+   Upon failure, it returns NULL with errno set.  */
 IALLOC_INLINE
 _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/
 void *
@@ -51,6 +54,9 @@ imalloc (idx_t s)
   return s <= SIZE_MAX ? malloc (s) : _gl_alloc_nomem ();
 }
 
+/* irealloc (ptr, size) is like realloc (ptr, size).
+   It returns a non-NULL pointer to size bytes of memory.
+   Upon failure, it returns NULL with errno set.  */
 IALLOC_INLINE
 /*_GL_ATTRIBUTE_DEALLOC_FREE*/
 void *
@@ -61,6 +67,9 @@ irealloc (void *p, idx_t s)
   return s <= SIZE_MAX ? realloc (p, s | !s) : _gl_alloc_nomem ();
 }
 
+/* icalloc (num, size) is like calloc (num, size).
+   It returns a non-NULL pointer to num * size bytes of memory.
+   Upon failure, it returns NULL with errno set.  */
 IALLOC_INLINE
 _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/
 void *
@@ -81,6 +90,9 @@ icalloc (idx_t n, idx_t s)
   return calloc (n, s);
 }
 
+/* ireallocarray (ptr, num, size) is like reallocarray (ptr, num, size).
+   It returns a non-NULL pointer to num * size bytes of memory.
+   Upon failure, it returns NULL with errno set.  */
 IALLOC_INLINE void *
 ireallocarray (void *p, idx_t n, idx_t s)
 {






Re: _Noreturn and draft C23

2023-03-27 Thread Bruno Haible
Hi Paul,

> That reminds me, we're in a sticky situation about _Noreturn for another 
> reason.
> 
> Draft C23 requires [[noreturn]] before "extern" and states that 
> _Noreturn is obsolescent. It's plausible that a future C version will 
> drop the requirement to support the _Noreturn keyword. If that happens, 
> "#define _Noreturn [[noreturn]]" won't be a valid workaround unless 
> people write "_Noreturn extern" instead of "extern _Noreturn", and 
> similarly for "_Noreturn static" etc.
> 
> Should we change Gnulib code and put _Noreturn first in its declarations 
> now, proactively? This might help future-proof the code, and get people 
> used to putting directives first.

For the .h files, we have already started doing so, in order to
maintain C++ compatibility:
  

For the other files, I suggest to wait until it becomes clear how ISO C
will evolve in this aspect. They could un-obsolete the '_Noreturn' keyword.
They could allow [[noreturn]] to be used before _or_ after the 'extern'
or 'static' keyword. They could make other changes that I can't imagine.
We have seen (with the K C declaration removal, as well as with %b and %B
just last week) that the way the standard evolves is just unpredictable.
When a new ISO C draft is in ballot phase, we still have a year or more
of time to adapt our code. There's no pressure on us to do these things
2 or 10 years in advance.

Bruno






_Noreturn and draft C23

2023-03-27 Thread Paul Eggert
That reminds me, we're in a sticky situation about _Noreturn for another 
reason.


Draft C23 requires [[noreturn]] before "extern" and states that 
_Noreturn is obsolescent. It's plausible that a future C version will 
drop the requirement to support the _Noreturn keyword. If that happens, 
"#define _Noreturn [[noreturn]]" won't be a valid workaround unless 
people write "_Noreturn extern" instead of "extern _Noreturn", and 
similarly for "_Noreturn static" etc.


Should we change Gnulib code and put _Noreturn first in its declarations 
now, proactively? This might help future-proof the code, and get people 
used to putting directives first. (But of course it would be annoying to 
churn in this way.)




Re: Support FALLTHROUGH macro better in glibc+clang

2023-03-27 Thread Bruno Haible
Paul Eggert wrote:
> On 2023-03-25 14:24, Paul Eggert wrote:
> > # if __GNUC_PREREQ (7,0) || __glibc_has_attribute (__fallthrough__)
> 
> Come to think of it this could be simplified further, to just:
> 
># if __glibc_has_attribute (__fallthrough__)
> 
> since GCC started supporting __has_attribute in GCC 5.

Good point. I'm thus committing this in your name:


2023-03-27  Paul Eggert  

Support FALLTHROUGH macro better in glibc+clang.
* lib/fnmatch.c (FALLTHROUGH): Use __attribute__ ((__fallthrough__))
also in clang >= 10.
* lib/fts.c (FALLTHROUGH): Likewise.
* lib/regex_internal.h (FALLTHROUGH): Likewise.

diff --git a/lib/fnmatch.c b/lib/fnmatch.c
index 7c9c4e0f24..32cfb48d0f 100644
--- a/lib/fnmatch.c
+++ b/lib/fnmatch.c
@@ -64,7 +64,7 @@ extern int fnmatch (const char *pattern, const char *string, 
int flags);
 #endif
 
 #ifdef _LIBC
-# if __GNUC__ >= 7
+# if __glibc_has_attribute (__fallthrough__)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)
diff --git a/lib/fts.c b/lib/fts.c
index 794a4f75d7..3fffb45d70 100644
--- a/lib/fts.c
+++ b/lib/fts.c
@@ -203,7 +203,7 @@ enum Fts_stat
 #endif
 
 #ifdef _LIBC
-# if __GNUC__ >= 7
+# if __glibc_has_attribute (__fallthrough__)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)
diff --git a/lib/regex_internal.h b/lib/regex_internal.h
index 149ec2e868..ae9257eac0 100644
--- a/lib/regex_internal.h
+++ b/lib/regex_internal.h
@@ -822,7 +822,7 @@ re_string_elem_size_at (const re_string_t *pstr, Idx idx)
 }
 
 #ifdef _LIBC
-# if __GNUC__ >= 7
+# if __glibc_has_attribute (__fallthrough__)
 #  define FALLTHROUGH __attribute__ ((__fallthrough__))
 # else
 #  define FALLTHROUGH ((void) 0)






Re: m4 porting to z/OS - gnulib fix - obstack.h

2023-03-27 Thread Bruno Haible
Hello Harithamma,

Harithamma D wrote:
> I am porting m4 to z/OS and came across following compilation error related 
> to gnulib:
> 
> In file included from ./builtin.c:25:
> In file included from ./m4.h:50:
> ../lib/obstack.h:229:8: error: unknown attribute '_Noreturn' ignored 
> [-Werror,-Wunknown-attributes]
> extern __attribute_noreturn__ void (*obstack_alloc_failed_handler) (void);
>^
> ../lib/obstack.h:156:50: note: expanded from macro '__attribute_noreturn__'
> #  define __attribute_noreturn__ __attribute__ ((__noreturn__))
>  ^
> /usr/include/le/features.h:791:26: note: expanded from macro '__noreturn__'
> #define __noreturn__ _Noreturn
> 
> 
> The compile command used is:
> 
> source='builtin.c' object='builtin.o' libtool=no \
> DEPDIR=.deps depmode=hp2 /bin/sh ../build-aux/depcomp \
> xlclang-I../lib -I../lib -DNSIG=42 -D_XOPEN_SOURCE=600 -D_ALL_SOURCE 
> -D_OPEN_SYS_FILE_EXT=1 -D_AE_BIMODAL=1 -D_ENHANCED_ASCII_EXT=0x  
> -fanalyzer -fno-common -Wall -Wbad-function-cast -Wdate-time 
> -Wdisabled-optimization -Wdouble-promotion -Wextra -Winit-self -Winline 
> -Winvalid-pch -Wmissing-declarations -Wmissing-include-dirs 
> -Wmissing-prototypes -Wnested-externs -Wnull-dereference 
> -Wold-style-definition -Woverlength-strings -Wpacked -Wpointer-arith -Wshadow 
> -Wstack-protector -Wstrict-overflow -Wstrict-prototypes -Wuninitialized 
> -Wunknown-pragmas -Wvariadic-macros -Wwrite-strings -Wformat=2 
> -Wno-missing-field-initializers -Werror -qascii -std=gnu11 -qnocsect 
> -qenum=int -qgonumber -O3 -std=c11 -fgnu89-inline 
> -I/home/haritha/code/m4port/m4/lib 
> -I/home/haritha/code/m4port/patches/PR1/include -c -o builtin.o builtin.c
> 
> The above error got fixed when I added the ! defined (__MVS__) condition as 
> shown in diff file attached.
> I would like to upstream these changes. Please let me know if you have any 
> concerns or questions.

Before doing that:

Since xlclang is, AFAIU, a copy of clang modified by IBM, and clang supports
__attribute__((__noreturn__)),
see https://clang.llvm.org/docs/AttributeReference.html#id20 , 
it would be more future-oriented if xlclang would support this attribute.
Can you please check with the relevant people in your company whether this
is a possible course of action?

I would prefer to deal with this in Gnulib only if the answer is "no".

Bruno






m4 porting to z/OS - gnulib fix - obstack.h

2023-03-27 Thread Harithamma D
Hi Team,

I am porting m4 to z/OS and came across following compilation error related to 
gnulib:

In file included from ./builtin.c:25:
In file included from ./m4.h:50:
../lib/obstack.h:229:8: error: unknown attribute '_Noreturn' ignored 
[-Werror,-Wunknown-attributes]
extern __attribute_noreturn__ void (*obstack_alloc_failed_handler) (void);
   ^
../lib/obstack.h:156:50: note: expanded from macro '__attribute_noreturn__'
#  define __attribute_noreturn__ __attribute__ ((__noreturn__))
 ^
/usr/include/le/features.h:791:26: note: expanded from macro '__noreturn__'
#define __noreturn__ _Noreturn


The compile command used is:

source='builtin.c' object='builtin.o' libtool=no \
DEPDIR=.deps depmode=hp2 /bin/sh ../build-aux/depcomp \
xlclang-I../lib -I../lib -DNSIG=42 -D_XOPEN_SOURCE=600 -D_ALL_SOURCE 
-D_OPEN_SYS_FILE_EXT=1 -D_AE_BIMODAL=1 -D_ENHANCED_ASCII_EXT=0x  
-fanalyzer -fno-common -Wall -Wbad-function-cast -Wdate-time 
-Wdisabled-optimization -Wdouble-promotion -Wextra -Winit-self -Winline 
-Winvalid-pch -Wmissing-declarations -Wmissing-include-dirs 
-Wmissing-prototypes -Wnested-externs -Wnull-dereference -Wold-style-definition 
-Woverlength-strings -Wpacked -Wpointer-arith -Wshadow -Wstack-protector 
-Wstrict-overflow -Wstrict-prototypes -Wuninitialized -Wunknown-pragmas 
-Wvariadic-macros -Wwrite-strings -Wformat=2 -Wno-missing-field-initializers 
-Werror -qascii -std=gnu11 -qnocsect -qenum=int -qgonumber -O3 -std=c11 
-fgnu89-inline -I/home/haritha/code/m4port/m4/lib 
-I/home/haritha/code/m4port/patches/PR1/include -c -o builtin.o builtin.c

The above error got fixed when I added the ! defined (__MVS__) condition as 
shown in diff file attached.
I would like to upstream these changes. Please let me know if you have any 
concerns or questions.

Regards
Haritha




obstack.h.patch
Description: obstack.h.patch


wcsstr: Ensure worst-case linear execution time

2023-03-27 Thread Bruno Haible
The same two-way algorithm
  
that provides worst-case linear execution time for strstr() and memmem()
also provides worst-case linear execution time for wcsstr(). Only the
"shift table" optimization that Eric Blake added into it doesn't apply,
because the shift table would be way too large.

Since, so far, only musl libc has a worst-case linear wcsstr() function
— also based on the two-way algorithm —, it makes sense to add this fast
algorithm to wcsstr(), like we already did for strstr() and strcasestr()
many years ago.


2023-03-27  Bruno Haible  

wcsstr: Ensure worst-case linear execution time.
* lib/wchar.in.h (wcsstr): Consider REPLACE_WCSSTR.
* lib/wcs-two-way.h: New file, based on lib/str-two-way.h.
* lib/wcsstr-impl.h: If requested, use the two-way algorithm. New code
based on lib/strstr.c.
* m4/wcsstr.m4 (gl_FUNC_WCSSTR_SIMPLE): Renamed from gl_FUNC_WCSSTR.
(gl_FUNC_WCSSTR): New macro, based on gl_FUNC_STRSTR in m4/strstr.m4.
* m4/wchar_h.m4 (gl_WCHAR_H_DEFAULTS): Initialize REPLACE_WCSSTR.
* modules/wchar (Makefile.am): Substitute REPLACE_WCSSTR.
* modules/wcsstr-simple: New file, based on modules/wcsstr.
* modules/wcsstr (Description): Document that this module now provides
an efficient implementation.
(Files): Add lib/wcs-two-way.h.
(Depends-on): Depend on wcsstr-simple and the dependencies of the
two-way implementation.
(configure.ac): Use AC_LIBOBJ instead of a conditional. Don't invoke
gl_WCHAR_MODULE_INDICATOR.
(Makefile.am): Don't augment lib_SOURCES.
* tests/test-wcsstr.c: New file, based on tests/test-strstr.c.
* modules/wcsstr-tests: New file, based on modules/strstr-tests.
* doc/posix-functions/wcsstr.texi: Mention the worst-case complexity.
Mention the new 'wcsstr-simple' module.
* doc/posix-functions/strstr.texi: Fix typo.

diff --git a/doc/posix-functions/strstr.texi b/doc/posix-functions/strstr.texi
index 1b124a0e53..3a36cfdeed 100644
--- a/doc/posix-functions/strstr.texi
+++ b/doc/posix-functions/strstr.texi
@@ -21,7 +21,7 @@
 glibc 2.28.
 @end itemize
 
-Portability problems fixed by Gnulib @code{strstr}:
+Portability problems fixed by Gnulib module @code{strstr}:
 @itemize
 @item
 This function has quadratic instead of linear worst-case complexity on some
diff --git a/doc/posix-functions/wcsstr.texi b/doc/posix-functions/wcsstr.texi
index c5711d0821..e69a036658 100644
--- a/doc/posix-functions/wcsstr.texi
+++ b/doc/posix-functions/wcsstr.texi
@@ -4,15 +4,23 @@
 
 POSIX specification:@* 
@url{https://pubs.opengroup.org/onlinepubs/9699919799/functions/wcsstr.html}
 
-Gnulib module: wcsstr
+Gnulib module: wcsstr or wcsstr-simple
 
-Portability problems fixed by Gnulib:
+Portability problems fixed by either Gnulib module @code{wcsstr-simple} or 
@code{wcsstr}:
 @itemize
 @item
 This function is missing on some platforms:
 HP-UX 11.00.
 @end itemize
 
+Portability problems fixed by Gnulib module @code{wcsstr}:
+@itemize
+@item
+This function has quadratic instead of linear worst-case complexity on some
+platforms:
+glibc 2.37, macOS 12.5, FreeBSD 13.1, NetBSD 9.0, OpenBSD 7.2, AIX 7.2, HP-UX 
11, IRIX 6.5, Solaris 11.4, Cygwin 2.9.0, mingw, MSVC 14.
+@end itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @item
diff --git a/lib/wchar.in.h b/lib/wchar.in.h
index 2beddd780f..194a1c6723 100644
--- a/lib/wchar.in.h
+++ b/lib/wchar.in.h
@@ -1234,12 +1234,25 @@ _GL_WARN_ON_USE (wcspbrk, "wcspbrk is unportable - "
 
 /* Find the first occurrence of NEEDLE in HAYSTACK.  */
 #if @GNULIB_WCSSTR@
-# if !@HAVE_WCSSTR@
+# if @REPLACE_WCSSTR@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef wcsstr
+#   define wcsstr rpl_wcsstr
+#  endif
+_GL_FUNCDECL_RPL (wcsstr, wchar_t *,
+  (const wchar_t *restrict haystack,
+   const wchar_t *restrict needle)
+  _GL_ATTRIBUTE_PURE);
+_GL_CXXALIAS_RPL (wcsstr, wchar_t *,
+  (const wchar_t *restrict haystack,
+   const wchar_t *restrict needle));
+# else
+#  if !@HAVE_WCSSTR@
 _GL_FUNCDECL_SYS (wcsstr, wchar_t *,
   (const wchar_t *restrict haystack,
const wchar_t *restrict needle)
   _GL_ATTRIBUTE_PURE);
-# endif
+#  endif
   /* On some systems, this function is defined as an overloaded function:
extern "C++" {
  const wchar_t * std::wcsstr (const wchar_t *, const wchar_t *);
@@ -1250,6 +1263,7 @@ _GL_CXXALIAS_SYS_CAST2 (wcsstr,
 (const wchar_t *restrict, const wchar_t *restrict),
 const wchar_t *,
 (const wchar_t *restrict, const wchar_t *restrict));
+# endif
 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
  && (__GNUC__ > 4 || 

Add test case from a past musl libc bug

2023-03-27 Thread Bruno Haible
There was a bug in the 'strstr' in musl libc, fixed in 2014. I'm adding
the test case to the test suite here, just in case someone copied the old
musl libc code and is still using it somewhere.


2023-03-27  Bruno Haible  

Add test case from a past musl libc bug.
* tests/test-strstr.c (main): Add test of periodic needle.
* tests/test-strcasestr.c (main): Likewise.
* tests/test-c-strstr.c (main): Likewise.
* tests/test-c-strcasestr.c (main): Likewise.
* tests/test-memmem.c (main): Likewise.

diff --git a/tests/test-c-strcasestr.c b/tests/test-c-strcasestr.c
index 818f5a994b..4f0a6c262a 100644
--- a/tests/test-c-strcasestr.c
+++ b/tests/test-c-strcasestr.c
@@ -238,6 +238,14 @@ main ()
 free (haystack);
   }
 
+  /* Test case from Yves Bastide.
+   */
+  {
+const char input[] = "playing PLAY play PLAY always";
+const char *result = c_strcasestr (input, "play PLAY play");
+ASSERT (result == input + 8);
+  }
+
   /* Test long needles.  */
   {
 size_t m = 1024;
diff --git a/tests/test-c-strstr.c b/tests/test-c-strstr.c
index d8f8465b29..30070d70a4 100644
--- a/tests/test-c-strstr.c
+++ b/tests/test-c-strstr.c
@@ -212,6 +212,14 @@ main ()
 free (haystack);
   }
 
+  /* Test case from Yves Bastide.
+   */
+  {
+const char input[] = "playing play play play always";
+const char *result = c_strstr (input, "play play play");
+ASSERT (result == input + 8);
+  }
+
   /* Test long needles.  */
   {
 size_t m = 1024;
diff --git a/tests/test-memmem.c b/tests/test-memmem.c
index 0daf3b07e7..589210f4a2 100644
--- a/tests/test-memmem.c
+++ b/tests/test-memmem.c
@@ -292,6 +292,14 @@ main (int argc, char *argv[])
 free (haystack);
   }
 
+  /* Test case from Yves Bastide.
+   */
+  {
+const char input[] = "playing play play play always";
+const char *result = memmem (input, strlen (input), "play play play", 14);
+ASSERT (result == input + 8);
+  }
+
   /* Test long needles.  */
   {
 size_t m = 1024;
diff --git a/tests/test-strcasestr.c b/tests/test-strcasestr.c
index 192e1569f8..c0f5111b4a 100644
--- a/tests/test-strcasestr.c
+++ b/tests/test-strcasestr.c
@@ -252,6 +252,14 @@ main ()
 free (haystack);
   }
 
+  /* Test case from Yves Bastide.
+   */
+  {
+const char input[] = "playing PLAY play PLAY always";
+const char *result = strcasestr (input, "play PLAY play");
+ASSERT (result == input + 8);
+  }
+
   /* Test long needles.  */
   {
 size_t m = 1024;
diff --git a/tests/test-strstr.c b/tests/test-strstr.c
index b2e00c526c..1ff9953d31 100644
--- a/tests/test-strstr.c
+++ b/tests/test-strstr.c
@@ -275,6 +275,14 @@ main (int argc, char *argv[])
 free (haystack);
   }
 
+  /* Test case from Yves Bastide.
+   */
+  {
+const char input[] = "playing play play play always";
+const char *result = strstr (input, "play play play");
+ASSERT (result == input + 8);
+  }
+
   /* Test long needles.  */
   {
 size_t m = 1024;






uchar: ISO C 23: Define char8_t

2023-03-27 Thread Bruno Haible
ISO C 23 specifies a new type, to be defined by .

This patch adds it.


2023-03-27  Bruno Haible  

uchar: ISO C 23: Define char8_t.
* lib/uchar.in.h (char8_t): New type or macro.
* m4/uchar_h.m4 (gl_TYPE_CHAR8_T): New macro.
(gl_UCHAR_H): Invoke it. Set CXX_HAS_CHAR8_TYPE.
* modules/uchar (Makefile.am): Substitute CXX_HAS_CHAR8_TYPE,
GNULIBHEADERS_OVERRIDE_CHAR8_T.
* tests/test-uchar.c: Add tests for char8_t.

diff --git a/lib/uchar.in.h b/lib/uchar.in.h
index 115ae1e84b..4d5f07fcce 100644
--- a/lib/uchar.in.h
+++ b/lib/uchar.in.h
@@ -17,7 +17,7 @@
 /* Written by Bruno Haible , 2019.  */
 
 /*
- * ISO C 11  for platforms that lack it.
+ * ISO C 23  for platforms that lack it.
  */
 
 #ifndef _@GUARD_PREFIX@_UCHAR_H
@@ -58,11 +58,26 @@
 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
 
 
+#if !(@HAVE_UCHAR_H@ || (defined __cplusplus && @CXX_HAS_CHAR8_TYPE@))
+
+/* An 8-bit variant of wchar_t.
+   Note: This type is only mandated by ISO C 23 or newer, and it does
+   denote UTF-8 units.  */
+typedef unsigned char char8_t;
+
+#elif @GNULIBHEADERS_OVERRIDE_CHAR8_T@
+
+typedef unsigned char gl_char8_t;
+# define char8_t gl_char8_t
+
+#endif
+
 #if !(@HAVE_UCHAR_H@ || (defined __cplusplus && @CXX_HAS_UCHAR_TYPES@))
 
 /* A 16-bit variant of wchar_t.
-   Note: This type does *NOT* denote UTF-16 units.  (Only on platforms
-   on which __STDC_UTF_16__ is defined.)  */
+   Note: This type is only mandated by ISO C 11 or newer.  In ISO C 23
+   and newer, it denotes UTF-16 units; in older versions of ISO C it did
+   so only on platforms on which __STDC_UTF_16__ was defined.  */
 typedef uint_least16_t char16_t;
 
 #elif @GNULIBHEADERS_OVERRIDE_CHAR16_T@
@@ -75,8 +90,9 @@ typedef uint_least16_t gl_char16_t;
 #if !(@HAVE_UCHAR_H@ || (defined __cplusplus && @CXX_HAS_UCHAR_TYPES@))
 
 /* A 32-bit variant of wchar_t.
-   Note: This type does *NOT* denote UTF-32 code points.  (Only on platforms
-   on which __STDC_UTF_32__ is defined.)  */
+   Note: This type is only mandated by ISO C 11 or newer.  In ISO C 23
+   and newer, it denotes UTF-32 code points; in older versions of ISO C
+   it did so only on platforms on which __STDC_UTF_32__ was defined.  */
 typedef uint_least32_t char32_t;
 
 #elif @GNULIBHEADERS_OVERRIDE_CHAR32_T@
diff --git a/m4/uchar_h.m4 b/m4/uchar_h.m4
index 2d1869a293..6df3056b32 100644
--- a/m4/uchar_h.m4
+++ b/m4/uchar_h.m4
@@ -1,4 +1,4 @@
-# uchar_h.m4 serial 20
+# uchar_h.m4 serial 21
 dnl Copyright (C) 2019-2023 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -19,6 +19,7 @@ AC_DEFUN_ONCE([gl_UCHAR_H]
   fi
   AC_SUBST([HAVE_UCHAR_H])
 
+  gl_TYPE_CHAR8_T
   gl_TYPE_CHAR16_T
   gl_TYPE_CHAR32_T
 
@@ -26,6 +27,7 @@ AC_DEFUN_ONCE([gl_UCHAR_H]
   dnl on some platforms (e.g. OpenBSD 6.7), and as types defined by many
   dnl header files (, , , , 
   dnl and others) on some platforms (e.g. Mac OS X 10.13).
+  dnl The same thing may also happen for 'char8_t'; so, be prepared for it.
   m4_ifdef([gl_ANSI_CXX], [AC_REQUIRE([gl_ANSI_CXX])])
   CXX_HAS_UCHAR_TYPES=0
   if test $HAVE_UCHAR_H = 0; then
@@ -53,6 +55,31 @@ AC_DEFUN_ONCE([gl_UCHAR_H]
 fi
   fi
   AC_SUBST([CXX_HAS_UCHAR_TYPES])
+  CXX_HAS_CHAR8_TYPE=0
+  if test $HAVE_UCHAR_H = 0; then
+if test "$CXX" != no; then
+  AC_CACHE_CHECK([whether the C++ compiler predefines the char8_t types],
+[gl_cv_cxx_has_char8_type],
+[dnl We can't use AC_LANG_PUSH([C++]) and AC_LANG_POP([C++]) here, due 
to
+ dnl an autoconf bug .
+ cat > conftest.cpp <<\EOF
+#include 
+char8_t a;
+EOF
+ gl_command="$CXX $CXXFLAGS $CPPFLAGS -c conftest.cpp"
+ if AC_TRY_EVAL([gl_command]); then
+   gl_cv_cxx_has_char8_type=yes
+ else
+   gl_cv_cxx_has_char8_type=no
+ fi
+ rm -fr conftest*
+])
+  if test $gl_cv_cxx_has_char8_type = yes; then
+CXX_HAS_CHAR8_TYPE=1
+  fi
+fi
+  fi
+  AC_SUBST([CXX_HAS_CHAR8_TYPE])
 
   dnl Test whether a 'char32_t' can hold more characters than a 'wchar_t'.
   gl_STDINT_BITSIZEOF([wchar_t], [gl_STDINT_INCLUDES])
@@ -71,6 +98,28 @@ AC_DEFUN_ONCE([gl_UCHAR_H]
 ]], [c32rtomb mbrtoc32])
 ])
 
+AC_DEFUN_ONCE([gl_TYPE_CHAR8_T],
+[
+  dnl Determine whether gnulib's  would, if present, override char8_t.
+  AC_CACHE_CHECK([whether char8_t is correctly defined],
+[gl_cv_type_char8_t_works],
+[AC_COMPILE_IFELSE(
+   [AC_LANG_PROGRAM([[
+  #include 
+  int verify[(char8_t)(-1) >= 0 && sizeof (char8_t) == sizeof 
(unsigned char) ? 1 : -1];
+  ]])
+   ],
+   [gl_cv_type_char8_t_works=yes],
+   [gl_cv_type_char8_t_works=no])
+])
+  if test $gl_cv_type_char8_t_works = no; then
+GNULIBHEADERS_OVERRIDE_CHAR8_T=1
+  else
+GNULIBHEADERS_OVERRIDE_CHAR8_T=0
+  

Re: RFC: add a string-desc module

2023-03-27 Thread Simon Josefsson via Gnulib discussion list
Bruno Haible  writes:

>   struct
>   {
> size_t nbytes;
> char * data;
>   }
>
> I propose to add a module that adds such a type, together with elementary
> functions that work on them.

I think this is a useful contribution, however I see two deal-breakers
for having it in gnulib -- both related to use in libraries.  I think
string helpers types/functions like this is useful not only in
applications but also in libraries.  Thus:

 1) License - there really isn't much novelty here, how about making
 this public domain or LGPLv2+?

 2) Applicability to use in a library - using x*alloc and abort is
 frowned upon in libraries.  Libraries should return error codes on
 expected errors (and I argue memory allocation failure is an expected
 error), and not cause application exits.

What do you think?

One way to resolve 2) is to have two variants of this functionality: one
low-level variant that doesn't abort the application on errors, and one
high-level variant that behaves like your implementation.  The
high-level variant could depend on the low-level variant, but that's not
essential.

/Simon


signature.asc
Description: PGP signature