From 134cfb88c06b0a427f3b4c60acb664c9cc5ec2d9 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 22 Jun 2024 12:17:10 +0200
Subject: [PATCH] vasprintf: Make return convention consistent with other
 modules.

* lib/vasprintf.c: Include <stdint.h>.
(vasprintf): If the length is > PTRDIFF_MAX, fail with ENOMEM, not
EOVERFLOW.
* modules/vasprintf (Depends-on): Add stdint.
---
 ChangeLog         |  6 ++++++
 lib/vasprintf.c   | 12 +++++++++++-
 modules/vasprintf |  1 +
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index b38ebee8a3..7c720ba885 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2024-06-22  Bruno Haible  <bruno@clisp.org>
 
+	vasprintf: Make return convention consistent with other modules.
+	* lib/vasprintf.c: Include <stdint.h>.
+	(vasprintf): If the length is > PTRDIFF_MAX, fail with ENOMEM, not
+	EOVERFLOW.
+	* modules/vasprintf (Depends-on): Add stdint.
+
 	vazsprintf: New module.
 	* lib/stdio.in.h (azsprintf, vazsprintf): New declarations.
 	* lib/vazsprintf.c: New file, based on lib/vasprintf.c.
diff --git a/lib/vasprintf.c b/lib/vasprintf.c
index e52aaca586..757d1c740a 100644
--- a/lib/vasprintf.c
+++ b/lib/vasprintf.c
@@ -25,6 +25,7 @@
 
 #include <errno.h>
 #include <limits.h>
+#include <stdint.h>
 #include <stdlib.h>
 
 #include "vasnprintf.h"
@@ -37,12 +38,21 @@ vasprintf (char **resultp, const char *format, va_list args)
   if (result == NULL)
     return -1;
 
+#if PTRDIFF_MAX > INT_MAX
   if (length > INT_MAX)
     {
       free (result);
-      errno = EOVERFLOW;
+      errno = (length > PTRDIFF_MAX ? ENOMEM : EOVERFLOW);
       return -1;
     }
+#else
+  if (length > PTRDIFF_MAX)
+    {
+      free (result);
+      errno = ENOMEM;
+      return -1;
+    }
+#endif
 
   *resultp = result;
   /* Return the number of resulting bytes, excluding the trailing NUL.  */
diff --git a/modules/vasprintf b/modules/vasprintf
index 91d082bd27..46bfcc51ba 100644
--- a/modules/vasprintf
+++ b/modules/vasprintf
@@ -11,6 +11,7 @@ stdio
 extensions
 vasnprintf      [test $HAVE_VASPRINTF = 0 || test $REPLACE_VASPRINTF = 1]
 errno           [test $HAVE_VASPRINTF = 0 || test $REPLACE_VASPRINTF = 1]
+stdint          [test $HAVE_VASPRINTF = 0 || test $REPLACE_VASPRINTF = 1]
 
 configure.ac:
 gl_FUNC_VASPRINTF
-- 
2.39.5