[flang-commits] [flang] [flang] Handle missing LOGIN_NAME_MAX definition in runtime (PR #77775)
Rainer Orth via flang-commits
flang-commits at lists.llvm.org
Thu Jan 11 06:47:48 PST 2024
https://github.com/rorth created https://github.com/llvm/llvm-project/pull/77775
18af032c0e16252effeb6dfd02113812388f1d31 broke the Solaris build:
```
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:24: error: use of undeclared identifier 'LOGIN_NAME_MAX'
60 | const int nameMaxLen{LOGIN_NAME_MAX + 1};
| ^
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
61 | char str[nameMaxLen];
| ^~~~~~~~~~
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: note: initializer of 'nameMaxLen' is unknown
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:13: note: declared here
60 | const int nameMaxLen{LOGIN_NAME_MAX + 1};
| ^
```
`flang/unittests/Runtime/CommandTest.cpp` has the same issue.
As documented in Solaris 11.4 `limits.h(3HEAD)`, `LOGIN_NAME_MAX` can be undefined. To determine the value, `sysconf(3C)` needs to be used instead.
Beside that portable method, Solaris also provides a non-standard `LOGNAME_MAX` which could be used, but I've preferred the standard route instead which would support other targets with the same issue.
Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.
>From 948efeed9967e07e10ddba0d24a4465579cb409b Mon Sep 17 00:00:00 2001
From: Rainer Orth <ro at gcc.gnu.org>
Date: Thu, 11 Jan 2024 15:46:16 +0100
Subject: [PATCH] [flang] Handle missing LOGIN_NAME_MAX definition in runtime
18af032c0e16252effeb6dfd02113812388f1d31 broke the Solaris build:
```
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:24: error: use of undeclared identifier 'LOGIN_NAME_MAX'
60 | const int nameMaxLen{LOGIN_NAME_MAX + 1};
| ^
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
61 | char str[nameMaxLen];
| ^~~~~~~~~~
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:61:12: note: initializer of 'nameMaxLen' is unknown
/vol/llvm/src/llvm-project/dist/flang/runtime/extensions.cpp:60:13: note: declared here
60 | const int nameMaxLen{LOGIN_NAME_MAX + 1};
| ^
```
`flang/unittests/Runtime/CommandTest.cpp` has the same issue.
As documented in Solaris 11.4 `limits.h(3HEAD)`, `LOGIN_NAME_MAX` can be
undefined. To determine the value, `sysconf(3C)` needs to be used instead.
Beside that portable method, Solaris also provides a non-standard
`LOGNAME_MAX` which could be used, but I've preferred the standard route
instead which would support other targets with the same issue.
Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.
---
flang/runtime/extensions.cpp | 9 ++++++++-
flang/unittests/Runtime/CommandTest.cpp | 9 ++++++++-
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 1c025d40b39524..d8fd2dff27742f 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -57,7 +57,14 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
// CALL GETLOG(USRNAME)
void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *arg, std::int64_t length) {
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L
- const int nameMaxLen{LOGIN_NAME_MAX + 1};
+ int nameMaxLen;
+#ifdef LOGIN_NAME_MAX
+ nameMaxLen = LOGIN_NAME_MAX + 1;
+#else
+ nameMaxLen = sysconf(_SC_LOGIN_NAME_MAX) + 1;
+ if (nameMaxLen == -1)
+ nameMaxLen = _POSIX_LOGIN_NAME_MAX + 1;
+#endif
char str[nameMaxLen];
int error{getlogin_r(str, nameMaxLen)};
diff --git a/flang/unittests/Runtime/CommandTest.cpp b/flang/unittests/Runtime/CommandTest.cpp
index 50b11d7fe8a0d5..0085d42edd7076 100644
--- a/flang/unittests/Runtime/CommandTest.cpp
+++ b/flang/unittests/Runtime/CommandTest.cpp
@@ -635,7 +635,14 @@ TEST_F(EnvironmentVariables, GetlogGetName) {
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L
TEST_F(EnvironmentVariables, GetlogPadSpace) {
// guarantee 1 char longer than max, last char should be pad space
- const int charLen{LOGIN_NAME_MAX + 2};
+ int charLen;
+#ifdef LOGIN_NAME_MAX
+ charLen = LOGIN_NAME_MAX + 2;
+#else
+ charLen = sysconf(_SC_LOGIN_NAME_MAX) + 2;
+ if (charLen == -1)
+ charLen = _POSIX_LOGIN_NAME_MAX + 2;
+#endif
char input[charLen];
FORTRAN_PROCEDURE_NAME(getlog)
More information about the flang-commits
mailing list