https://gcc.gnu.org/g:0e3e2cc03e6a9d8d48fa5269070976f3d7d0a750

commit r14-10950-g0e3e2cc03e6a9d8d48fa5269070976f3d7d0a750
Author: Gaius Mulley <gaiusm...@gmail.com>
Date:   Wed Nov 20 09:22:53 2024 +0000

    [PATCH] PR modula2/115276 bugfix libgm2 wraptime.InitTM returns NIL
    
    This patch fixes libgm2/libm2iso/wraptime.cc:InitTM so that
    it does not always return NULL.  The incorrect autoconf macro
    was used (inside InitTM) and the function short circuited
    to return NULL.  The fix is to use HAVE_SYS_TIME_H and use
    AC_HEADER_TIME in libgm2/configure.ac.
    
    libgm2/ChangeLog:
    
            PR modula2/115276
            * config.h.in: Regenerate.
            * configure: Regenerate.
            * configure.ac: Use AC_HEADER_TIME.
            * libm2iso/wraptime.cc (InitTM): Check HAVE_SYS_TIME_H
            before using struct tm to obtain the size.
    
    gcc/testsuite/ChangeLog:
    
            PR modula2/115276
            * gm2/isolib/run/pass/testinittm.mod: New test.
    
    (cherry picked from commit d1a1f7e9f0bedea55c558ab95127679bc3e9ff72)
    
    Signed-off-by: Gaius Mulley <gaiusm...@gmail.com>

Diff:
---
 gcc/testsuite/gm2/isolib/run/pass/testinittm.mod | 17 +++++++++++
 libgm2/config.h.in                               |  3 ++
 libgm2/configure                                 | 39 ++++++++++++++++++++++--
 libgm2/configure.ac                              |  1 +
 libgm2/libm2iso/wraptime.cc                      |  2 +-
 5 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/gm2/isolib/run/pass/testinittm.mod 
b/gcc/testsuite/gm2/isolib/run/pass/testinittm.mod
new file mode 100644
index 000000000000..dfe041140f17
--- /dev/null
+++ b/gcc/testsuite/gm2/isolib/run/pass/testinittm.mod
@@ -0,0 +1,17 @@
+MODULE testinittm ;
+
+FROM wraptime IMPORT InitTM, tm ;
+FROM libc IMPORT printf, exit ;
+
+VAR
+   m: tm ;
+BEGIN
+   m := InitTM () ;
+   IF m = NIL
+   THEN
+      printf ("InitTM failed\n");
+      exit (1)
+   ELSE
+      printf ("InitTM passed\n")
+   END
+END testinittm.
diff --git a/libgm2/config.h.in b/libgm2/config.h.in
index 7426cb26cf88..321ef3b807f7 100644
--- a/libgm2/config.h.in
+++ b/libgm2/config.h.in
@@ -335,6 +335,9 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
+#undef TIME_WITH_SYS_TIME
+
 /* Enable extensions on AIX 3, Interix.  */
 #ifndef _ALL_SOURCE
 # undef _ALL_SOURCE
diff --git a/libgm2/configure b/libgm2/configure
index 13861f0ff93e..c36fd7d4cac5 100755
--- a/libgm2/configure
+++ b/libgm2/configure
@@ -6837,6 +6837,41 @@ $as_echo "#define HAVE_SYS_WAIT_H 1" >>confdefs.h
 
 fi
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether time.h and 
sys/time.h may both be included" >&5
+$as_echo_n "checking whether time.h and sys/time.h may both be included... " 
>&6; }
+if ${ac_cv_header_time+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <sys/types.h>
+#include <sys/time.h>
+#include <time.h>
+
+int
+main ()
+{
+if ((struct tm *) 0)
+return 0;
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  ac_cv_header_time=yes
+else
+  ac_cv_header_time=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_header_time" >&5
+$as_echo "$ac_cv_header_time" >&6; }
+if test $ac_cv_header_time = yes; then
+
+$as_echo "#define TIME_WITH_SYS_TIME 1" >>confdefs.h
+
+fi
+
 ac_fn_c_check_header_mongrel "$LINENO" "math.h" "ac_cv_header_math_h" 
"$ac_includes_default"
 if test "x$ac_cv_header_math_h" = xyes; then :
 
@@ -14544,7 +14579,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 14547 "configure"
+#line 14582 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -14650,7 +14685,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 14653 "configure"
+#line 14688 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
diff --git a/libgm2/configure.ac b/libgm2/configure.ac
index 9563831ddc5f..1e6b82305ffc 100644
--- a/libgm2/configure.ac
+++ b/libgm2/configure.ac
@@ -88,6 +88,7 @@ AC_ARG_WITH(cross-host,
 # Checks for header files.
 AC_HEADER_STDC
 AC_HEADER_SYS_WAIT
+AC_HEADER_TIME
 AC_CHECK_HEADER([math.h],
               [AC_DEFINE([HAVE_MATH_H], [1], [have math.h])])
 
diff --git a/libgm2/libm2iso/wraptime.cc b/libgm2/libm2iso/wraptime.cc
index 158086b75cc0..4bbd5f9701d7 100644
--- a/libgm2/libm2iso/wraptime.cc
+++ b/libgm2/libm2iso/wraptime.cc
@@ -113,7 +113,7 @@ EXPORT(KillTimezone) (struct timezone *tv)
 
 /* InitTM - returns a newly created opaque type.  */
 
-#if defined(HAVE_STRUCT_TM) && defined(HAVE_MALLOC_H)
+#if defined(HAVE_SYS_TIME_H) && defined(HAVE_MALLOC_H)
 extern "C" struct tm *
 EXPORT(InitTM) (void)
 {

Reply via email to