[flang-commits] [flang] [flang] fix VLA using malloc and avoid using std::vector, arg input type change to char * from std::byte (PR #77911)
Yi Wu via flang-commits
flang-commits at lists.llvm.org
Fri Jan 12 09:07:58 PST 2024
https://github.com/yi-wu-arm updated https://github.com/llvm/llvm-project/pull/77911
>From 9d0aea77a5bbccf1e0b0e6c0f259af22ad4793e8 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 12 Jan 2024 12:02:40 +0000
Subject: [PATCH 1/3] [flang] fix VLA using malloc and avoid using std::vector,
arg input type change to char * from std::byte
---
flang/include/flang/Runtime/extensions.h | 2 +-
flang/runtime/extensions.cpp | 15 +++++++++------
flang/unittests/Runtime/CommandTest.cpp | 21 ++++++++++-----------
3 files changed, 20 insertions(+), 18 deletions(-)
diff --git a/flang/include/flang/Runtime/extensions.h b/flang/include/flang/Runtime/extensions.h
index b34edb94ada43a..d8ddd8069e202d 100644
--- a/flang/include/flang/Runtime/extensions.h
+++ b/flang/include/flang/Runtime/extensions.h
@@ -33,7 +33,7 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
std::int32_t &n, std::int8_t *arg, std::int64_t length);
// GNU extension subroutine GETLOG(C).
-void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *name, std::int64_t length);
+void FORTRAN_PROCEDURE_NAME(getlog)(char *name, std::int64_t length);
} // extern "C"
#endif // FORTRAN_RUNTIME_EXTENSIONS_H_
diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 352da8f11d9d56..7340895744dbe0 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -15,6 +15,7 @@
#include "flang/Runtime/command.h"
#include "flang/Runtime/descriptor.h"
#include "flang/Runtime/io-api.h"
+#include "flang/Runtime/memory.h"
#include <ctime>
#ifdef _WIN32
@@ -48,7 +49,7 @@ extern "C" {
namespace Fortran::runtime {
void GetUsernameEnvVar(
- const char *envName, std::byte *arg, std::int64_t length) {
+ const char *envName, char *arg, std::int64_t length) {
Descriptor name{*Descriptor::Create(
1, std::strlen(envName) + 1, const_cast<char *>(envName), 0)};
Descriptor value{*Descriptor::Create(1, length, arg, 0)};
@@ -98,7 +99,7 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
}
// CALL GETLOG(USRNAME)
-void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *arg, std::int64_t length) {
+void FORTRAN_PROCEDURE_NAME(getlog)(char *arg, std::int64_t length) {
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L
int nameMaxLen;
#ifdef LOGIN_NAME_MAX
@@ -108,17 +109,19 @@ void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *arg, std::int64_t length) {
if (nameMaxLen == -1)
nameMaxLen = _POSIX_LOGIN_NAME_MAX + 1;
#endif
- std::vector<char> str(nameMaxLen);
+ Terminator terminator{__FILE__, __LINE__};
+ char *str{(char *)AllocateMemoryOrCrash(terminator, nameMaxLen)};
+ str[nameMaxLen] = '\0';
- int error{getlogin_r(str.data(), nameMaxLen)};
+ int error{getlogin_r(str, nameMaxLen)};
if (error == 0) {
// no error: find first \0 in string then pad from there
- CopyAndPad(reinterpret_cast<char *>(arg), str.data(), length,
- std::strlen(str.data()));
+ CopyAndPad(arg, str, length, std::strlen(str));
} else {
// error occur: get username from environment variable
GetUsernameEnvVar("LOGNAME", arg, length);
}
+ FreeMemory((void*)str);
#elif _WIN32
// Get username from environment to avoid link to Advapi32.lib
GetUsernameEnvVar("USERNAME", arg, length);
diff --git a/flang/unittests/Runtime/CommandTest.cpp b/flang/unittests/Runtime/CommandTest.cpp
index a0f14c519412a4..fda5f00c57d2ca 100644
--- a/flang/unittests/Runtime/CommandTest.cpp
+++ b/flang/unittests/Runtime/CommandTest.cpp
@@ -13,6 +13,8 @@
#include "flang/Runtime/execute.h"
#include "flang/Runtime/extensions.h"
#include "flang/Runtime/main.h"
+#include "flang/Runtime/memory.h"
+#include "../../runtime/terminator.h"
#include <cstddef>
#include <cstdlib>
@@ -682,8 +684,7 @@ TEST_F(EnvironmentVariables, GetlogGetName) {
const int charLen{3};
char input[charLen]{"\0\0"};
- FORTRAN_PROCEDURE_NAME(getlog)
- (reinterpret_cast<std::byte *>(input), charLen);
+ FORTRAN_PROCEDURE_NAME(getlog)(input, charLen);
EXPECT_NE(input[0], '\0');
}
@@ -699,12 +700,13 @@ TEST_F(EnvironmentVariables, GetlogPadSpace) {
if (charLen == -1)
charLen = _POSIX_LOGIN_NAME_MAX + 2;
#endif
- std::vector<char> input(charLen);
+ Terminator terminator{__FILE__, __LINE__};
+ char *input{(char *)AllocateMemoryOrCrash(terminator, charLen)};
- FORTRAN_PROCEDURE_NAME(getlog)
- (reinterpret_cast<std::byte *>(input.data()), charLen);
+ FORTRAN_PROCEDURE_NAME(getlog)(input, charLen);
EXPECT_EQ(input[charLen - 1], ' ');
+ FreeMemory((void *)input);
}
#endif
@@ -715,8 +717,7 @@ TEST_F(EnvironmentVariables, GetlogEnvGetName) {
<< "Environment variable USERNAME does not exist";
char input[]{"XXXXXXXXX"};
- FORTRAN_PROCEDURE_NAME(getlog)
- (reinterpret_cast<std::byte *>(input), sizeof(input));
+ FORTRAN_PROCEDURE_NAME(getlog)(input, sizeof(input));
CheckCharEqStr(input, "loginName");
}
@@ -728,8 +729,7 @@ TEST_F(EnvironmentVariables, GetlogEnvBufferShort) {
<< "Environment variable USERNAME does not exist";
char input[]{"XXXXXX"};
- FORTRAN_PROCEDURE_NAME(getlog)
- (reinterpret_cast<std::byte *>(input), sizeof(input));
+ FORTRAN_PROCEDURE_NAME(getlog)(input, sizeof(input));
CheckCharEqStr(input, "loginN");
}
@@ -741,8 +741,7 @@ TEST_F(EnvironmentVariables, GetlogEnvPadSpace) {
<< "Environment variable USERNAME does not exist";
char input[]{"XXXXXXXXXX"};
- FORTRAN_PROCEDURE_NAME(getlog)
- (reinterpret_cast<std::byte *>(input), sizeof(input));
+ FORTRAN_PROCEDURE_NAME(getlog)(input, sizeof(input));
CheckCharEqStr(input, "loginName ");
}
>From 1a809f8a354a59ff72ce35653ac5d2e94bc849dc Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 12 Jan 2024 12:14:27 +0000
Subject: [PATCH 2/3] clang format
---
flang/runtime/extensions.cpp | 5 ++---
flang/unittests/Runtime/CommandTest.cpp | 2 +-
2 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 7340895744dbe0..ef6f0aba46103c 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -48,8 +48,7 @@ extern "C" {
namespace Fortran::runtime {
-void GetUsernameEnvVar(
- const char *envName, char *arg, std::int64_t length) {
+void GetUsernameEnvVar(const char *envName, char *arg, std::int64_t length) {
Descriptor name{*Descriptor::Create(
1, std::strlen(envName) + 1, const_cast<char *>(envName), 0)};
Descriptor value{*Descriptor::Create(1, length, arg, 0)};
@@ -121,7 +120,7 @@ void FORTRAN_PROCEDURE_NAME(getlog)(char *arg, std::int64_t length) {
// error occur: get username from environment variable
GetUsernameEnvVar("LOGNAME", arg, length);
}
- FreeMemory((void*)str);
+ FreeMemory((void *)str);
#elif _WIN32
// Get username from environment to avoid link to Advapi32.lib
GetUsernameEnvVar("USERNAME", arg, length);
diff --git a/flang/unittests/Runtime/CommandTest.cpp b/flang/unittests/Runtime/CommandTest.cpp
index fda5f00c57d2ca..c0c0b21556e824 100644
--- a/flang/unittests/Runtime/CommandTest.cpp
+++ b/flang/unittests/Runtime/CommandTest.cpp
@@ -9,12 +9,12 @@
#include "flang/Runtime/command.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
+#include "../../runtime/terminator.h"
#include "flang/Runtime/descriptor.h"
#include "flang/Runtime/execute.h"
#include "flang/Runtime/extensions.h"
#include "flang/Runtime/main.h"
#include "flang/Runtime/memory.h"
-#include "../../runtime/terminator.h"
#include <cstddef>
#include <cstdlib>
>From ccd62acb4d9214e07c43188e007fc9deb0471219 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 12 Jan 2024 17:06:11 +0000
Subject: [PATCH 3/3] use cpp cast `static_cast<char*>` instead of c cast
`(char *)`
---
flang/runtime/extensions.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index ef6f0aba46103c..1b3ccc6c298ab0 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -109,7 +109,7 @@ void FORTRAN_PROCEDURE_NAME(getlog)(char *arg, std::int64_t length) {
nameMaxLen = _POSIX_LOGIN_NAME_MAX + 1;
#endif
Terminator terminator{__FILE__, __LINE__};
- char *str{(char *)AllocateMemoryOrCrash(terminator, nameMaxLen)};
+ char *str{static_cast<char *>(AllocateMemoryOrCrash(terminator, nameMaxLen))};
str[nameMaxLen] = '\0';
int error{getlogin_r(str, nameMaxLen)};
More information about the flang-commits
mailing list