[flang-commits] [flang] [Flang] malloc(1) on AIX as malloc(0) returns nullptr (PR #73878)

via flang-commits flang-commits at lists.llvm.org
Tue Dec 5 06:48:36 PST 2023


https://github.com/madanial0 updated https://github.com/llvm/llvm-project/pull/73878

>From 7e70674a3d047d96876d34b1828d61dbc4e64f49 Mon Sep 17 00:00:00 2001
From: Mark Danial <madanial at dixon.rtp.raleigh.ibm.com>
Date: Tue, 28 Nov 2023 22:14:54 -0500
Subject: [PATCH 1/3] [Flang] malloc(1) on AIX as malloc(0) returns nullptr

---
 flang/runtime/ISO_Fortran_binding.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/flang/runtime/ISO_Fortran_binding.cpp b/flang/runtime/ISO_Fortran_binding.cpp
index ce146844533a0..20bd42adbd801 100644
--- a/flang/runtime/ISO_Fortran_binding.cpp
+++ b/flang/runtime/ISO_Fortran_binding.cpp
@@ -75,7 +75,14 @@ RT_API_ATTRS int CFI_allocate(CFI_cdesc_t *descriptor,
     dim->sm = byteSize;
     byteSize *= extent;
   }
+#ifdef _AIX
+  if (!byteSize)
+    void *p{std::malloc(1)};
+  else
+    void *p{std::malloc(byteSize)};
+#else
   void *p{std::malloc(byteSize)};
+#endif
   if (!p && byteSize) {
     return CFI_ERROR_MEM_ALLOCATION;
   }

>From 35947c32a4cea73e27a1f980016feca010ee5d75 Mon Sep 17 00:00:00 2001
From: Mark Danial <madanial at dixon.rtp.raleigh.ibm.com>
Date: Mon, 4 Dec 2023 15:19:53 -0500
Subject: [PATCH 2/3] Address malloc(0) for all platforms

---
 flang/runtime/ISO_Fortran_binding.cpp | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/flang/runtime/ISO_Fortran_binding.cpp b/flang/runtime/ISO_Fortran_binding.cpp
index 20bd42adbd801..13ae2b9eabdeb 100644
--- a/flang/runtime/ISO_Fortran_binding.cpp
+++ b/flang/runtime/ISO_Fortran_binding.cpp
@@ -75,14 +75,7 @@ RT_API_ATTRS int CFI_allocate(CFI_cdesc_t *descriptor,
     dim->sm = byteSize;
     byteSize *= extent;
   }
-#ifdef _AIX
-  if (!byteSize)
-    void *p{std::malloc(1)};
-  else
-    void *p{std::malloc(byteSize)};
-#else
-  void *p{std::malloc(byteSize)};
-#endif
+  void *p = byteSize ? std::malloc(byteSize) : std::malloc(1);
   if (!p && byteSize) {
     return CFI_ERROR_MEM_ALLOCATION;
   }

>From d1ac07247d1a914167117b3e539daff1ecee31e1 Mon Sep 17 00:00:00 2001
From: Mark Danial <madanial at dixon.rtp.raleigh.ibm.com>
Date: Tue, 5 Dec 2023 09:47:49 -0500
Subject: [PATCH 3/3] use braces for initialization

---
 flang/runtime/ISO_Fortran_binding.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/runtime/ISO_Fortran_binding.cpp b/flang/runtime/ISO_Fortran_binding.cpp
index 13ae2b9eabdeb..99ba3aa56feee 100644
--- a/flang/runtime/ISO_Fortran_binding.cpp
+++ b/flang/runtime/ISO_Fortran_binding.cpp
@@ -75,7 +75,7 @@ RT_API_ATTRS int CFI_allocate(CFI_cdesc_t *descriptor,
     dim->sm = byteSize;
     byteSize *= extent;
   }
-  void *p = byteSize ? std::malloc(byteSize) : std::malloc(1);
+  void *p{byteSize ? std::malloc(byteSize) : std::malloc(1)};
   if (!p && byteSize) {
     return CFI_ERROR_MEM_ALLOCATION;
   }



More information about the flang-commits mailing list