Skip to content

Commit 731b295

Browse files
authored
[flang] Handle missing LOGIN_NAME_MAX definition in runtime (#77775)
18af032 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`.
1 parent 18473eb commit 731b295

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

flang/runtime/extensions.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
100100
// CALL GETLOG(USRNAME)
101101
void FORTRAN_PROCEDURE_NAME(getlog)(std::byte *arg, std::int64_t length) {
102102
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L
103-
const int nameMaxLen{LOGIN_NAME_MAX + 1};
103+
int nameMaxLen;
104+
#ifdef LOGIN_NAME_MAX
105+
nameMaxLen = LOGIN_NAME_MAX + 1;
106+
#else
107+
nameMaxLen = sysconf(_SC_LOGIN_NAME_MAX) + 1;
108+
if (nameMaxLen == -1)
109+
nameMaxLen = _POSIX_LOGIN_NAME_MAX + 1;
110+
#endif
104111
char str[nameMaxLen];
105112

106113
int error{getlogin_r(str, nameMaxLen)};

flang/unittests/Runtime/CommandTest.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,14 @@ TEST_F(EnvironmentVariables, GetlogGetName) {
691691
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L
692692
TEST_F(EnvironmentVariables, GetlogPadSpace) {
693693
// guarantee 1 char longer than max, last char should be pad space
694-
const int charLen{LOGIN_NAME_MAX + 2};
694+
int charLen;
695+
#ifdef LOGIN_NAME_MAX
696+
charLen = LOGIN_NAME_MAX + 2;
697+
#else
698+
charLen = sysconf(_SC_LOGIN_NAME_MAX) + 2;
699+
if (charLen == -1)
700+
charLen = _POSIX_LOGIN_NAME_MAX + 2;
701+
#endif
695702
char input[charLen];
696703

697704
FORTRAN_PROCEDURE_NAME(getlog)

0 commit comments

Comments
 (0)