wcsdup() is required by POSIX to set errno upon failure. When I wrote
this code in 1999, this function was not in POSIX, and when I added
it to Gnulib in 2011, I forgot to look up what POSIX:2008 said.

This patch fixes it.


2025-11-19  Bruno Haible  <[email protected]>

        wcsdup: Make POSIX compliant.
        * lib/wcsdup.c: Include <errno.h>.
        * lib/wcsdup-impl.h (wcsdup): Set errno before returning NULL.

diff --git a/lib/wcsdup-impl.h b/lib/wcsdup-impl.h
index 4f53ebda18..b16d86bd16 100644
--- a/lib/wcsdup-impl.h
+++ b/lib/wcsdup-impl.h
@@ -23,7 +23,8 @@ wcsdup (const wchar_t *s)
   if (copy != NULL)
     return wmemcpy (copy, s, n);
   else
-    /* The glibc documentation does not say that errno should be set to ENOMEM
-       here.  */
-    return NULL;
+    {
+      errno = ENOMEM;
+      return NULL;
+    }
 }
diff --git a/lib/wcsdup.c b/lib/wcsdup.c
index 54f148c154..7ff9dcf237 100644
--- a/lib/wcsdup.c
+++ b/lib/wcsdup.c
@@ -20,6 +20,7 @@
 /* Specification.  */
 #include <wchar.h>
 
+#include <errno.h>
 #include <stdlib.h>
 
 #include "wcsdup-impl.h"




Reply via email to