From 52ae9c9f87fcf33df917c986ed937bd5672e6911 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Mon, 6 Jan 2025 01:16:11 +0100
Subject: [PATCH] sigsegv tests: Work around a longjmp bug on GNU/Hurd.

* tests/test-sigsegv-catch-stackoverflow1.c (_FORTIFY_SOURCE,
__USE_FORTIFY_LEVEL): Undefine, as a workaround to the Hurd longjmp bug.
* tests/test-sigsegv-catch-stackoverflow2.c (_FORTIFY_SOURCE,
__USE_FORTIFY_LEVEL): Likewise.
* doc/posix-functions/longjmp.texi: Document the Hurd bug.
---
 ChangeLog                                 |  9 +++++++++
 doc/posix-functions/longjmp.texi          | 10 ++++++++--
 tests/test-sigsegv-catch-stackoverflow1.c | 10 +++++++++-
 tests/test-sigsegv-catch-stackoverflow2.c | 10 +++++++++-
 4 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2710a9a489..ac8d573988 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2025-01-05  Bruno Haible  <bruno@clisp.org>
+
+	sigsegv tests: Work around a longjmp bug on GNU/Hurd.
+	* tests/test-sigsegv-catch-stackoverflow1.c (_FORTIFY_SOURCE,
+	__USE_FORTIFY_LEVEL): Undefine, as a workaround to the Hurd longjmp bug.
+	* tests/test-sigsegv-catch-stackoverflow2.c (_FORTIFY_SOURCE,
+	__USE_FORTIFY_LEVEL): Likewise.
+	* doc/posix-functions/longjmp.texi: Document the Hurd bug.
+
 2025-01-05  Bruno Haible  <bruno@clisp.org>
 
 	tests: Don't use module 'getcwd' as dependency.
diff --git a/doc/posix-functions/longjmp.texi b/doc/posix-functions/longjmp.texi
index 420a8cc44f..ac5dd20d54 100644
--- a/doc/posix-functions/longjmp.texi
+++ b/doc/posix-functions/longjmp.texi
@@ -18,7 +18,13 @@ since it restores the contents of register-allocated variables but not
 the contents of stack-allocated variables.
 @item
 When longjumping out of a signal handler that was being executed on an
-alternate stack (installed through @code{sigaltstack}), on FreeBSD, NetBSD,
-OpenBSD, you need to clear the @code{SS_ONSTACK} flag in the @code{stack_t}
+alternate stack (installed through @code{sigaltstack}),
+on Hurd, FreeBSD, NetBSD, OpenBSD,
+you need to clear the @code{SS_ONSTACK} flag in the @code{stack_t}
 structure managed by the kernel.
+@item
+This function may crash when the invoker code was compiled with option
+@code{-D_FORTIFY_SOURCE=2},
+@c https://sourceware.org/bugzilla/show_bug.cgi?id=32522
+on Hurd.
 @end itemize
diff --git a/tests/test-sigsegv-catch-stackoverflow1.c b/tests/test-sigsegv-catch-stackoverflow1.c
index 7e58bbb762..cb20bceb8e 100644
--- a/tests/test-sigsegv-catch-stackoverflow1.c
+++ b/tests/test-sigsegv-catch-stackoverflow1.c
@@ -1,5 +1,5 @@
 /* Test the stack overflow handler.
-   Copyright (C) 2002-2024 Free Software Foundation, Inc.
+   Copyright (C) 2002-2025 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -18,6 +18,14 @@
 
 #include <config.h>
 
+/* On GNU/Hurd, when compiling with -D_FORTIFY_SOURCE=2, avoid an error
+   "*** longjmp causes uninitialized stack frame ***: terminated".
+   Cf. <https://sourceware.org/bugzilla/show_bug.cgi?id=32522>  */
+#ifdef __GNU__
+# undef _FORTIFY_SOURCE
+# undef __USE_FORTIFY_LEVEL
+#endif
+
 /* Specification.  */
 #include "sigsegv.h"
 
diff --git a/tests/test-sigsegv-catch-stackoverflow2.c b/tests/test-sigsegv-catch-stackoverflow2.c
index 133b616af2..34ea491740 100644
--- a/tests/test-sigsegv-catch-stackoverflow2.c
+++ b/tests/test-sigsegv-catch-stackoverflow2.c
@@ -1,5 +1,5 @@
 /* Test that stack overflow and SIGSEGV are correctly distinguished.
-   Copyright (C) 2002-2024 Free Software Foundation, Inc.
+   Copyright (C) 2002-2025 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -18,6 +18,14 @@
 
 #include <config.h>
 
+/* On GNU/Hurd, when compiling with -D_FORTIFY_SOURCE=2, avoid an error
+   "*** longjmp causes uninitialized stack frame ***: terminated".
+   Cf. <https://sourceware.org/bugzilla/show_bug.cgi?id=32522>  */
+#ifdef __GNU__
+# undef _FORTIFY_SOURCE
+# undef __USE_FORTIFY_LEVEL
+#endif
+
 /* Specification.  */
 #include "sigsegv.h"
 
-- 
2.39.5