From 5671641a5beb594244e6fbf9317b0937644daad3 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Mon, 3 Jun 2024 15:34:21 +0200
Subject: [PATCH] dprintf-posix tests: Skip the memory leak test on macOS.

* tests/test-dprintf-posix2.c: Skip the test on macOS.
(main): Return a different exit code at each point. Allow 100 KB extra
memory consumption.
* tests/test-dprintf-posix2.sh: Update.
---
 ChangeLog                    |  8 ++++++++
 tests/test-dprintf-posix2.c  | 21 ++++++++++++++++++---
 tests/test-dprintf-posix2.sh |  6 +++++-
 3 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3e1e553dc4..0408c7728a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-06-03  Bruno Haible  <bruno@clisp.org>
+
+	dprintf-posix tests: Skip the memory leak test on macOS.
+	* tests/test-dprintf-posix2.c: Skip the test on macOS.
+	(main): Return a different exit code at each point. Allow 100 KB extra
+	memory consumption.
+	* tests/test-dprintf-posix2.sh: Update.
+
 2024-06-03  Collin Funk  <collin.funk1@gmail.com>
 
 	gnulib-tool.py: Use a set to optimize.
diff --git a/tests/test-dprintf-posix2.c b/tests/test-dprintf-posix2.c
index 3263072ebd..6e01d7e782 100644
--- a/tests/test-dprintf-posix2.c
+++ b/tests/test-dprintf-posix2.c
@@ -20,6 +20,20 @@
 
 #include <stdio.h>
 
+#if defined __APPLE__ && defined __MACH__                            /* macOS */
+
+/* On macOS 11, 12, 13, 14, this test sometimes fails.  Probably for the same
+   reason as mentioned in the comment in test-fprintf-posix3.c.  */
+
+int
+main ()
+{
+  fprintf (stderr, "Skipping test: cannot trust address space size on this platform\n");
+  return 78;
+}
+
+#else
+
 /* Skip this test when an address sanitizer is in use, since it would fail.  */
 #ifndef __has_feature
 # define __has_feature(a) 0
@@ -121,16 +135,17 @@ main (int argc, char *argv[])
              but should not result in a permanent memory allocation.  */
           if (dprintf (STDOUT_FILENO, "%011000d\n", 17) == -1
               && errno == ENOMEM)
-            return 1;
+            return 2;
         }
 
       result = 0;
     }
 
-  if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL)
-    return 1;
+  if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL + 100000)
+    return 3;
 
   return result;
 }
 
 #endif /* ! address sanitizer enabled */
+#endif /* !macOS */
diff --git a/tests/test-dprintf-posix2.sh b/tests/test-dprintf-posix2.sh
index 12e25bc318..2827f4a209 100755
--- a/tests/test-dprintf-posix2.sh
+++ b/tests/test-dprintf-posix2.sh
@@ -4,7 +4,7 @@
 
 (${CHECKER} ./test-dprintf-posix2${EXEEXT} 0
  result=$?
- if test $result != 77 && test $result != 79 && test $result != 80 && test $result != 81; then result=1; fi
+ if test $result != 77 && test $result != 78 && test $result != 79 && test $result != 80 && test $result != 81; then result=1; fi
  exit $result
 ) 2>/dev/null
 malloc_result=$?
@@ -12,6 +12,10 @@ if test $malloc_result = 77; then
   echo "Skipping test: no way to determine address space size"
   exit 77
 fi
+if test $malloc_result = 78; then
+  echo "Skipping test: cannot trust address space size on this platform"
+  exit 77
+fi
 if test $malloc_result = 79; then
   echo "Skipping test: cannot trust address space size when running under QEMU"
   exit 77
-- 
2.39.5