Skip to content

[flang] fix VLA using malloc and avoid using std::vector, arg input type change to char * from std::byte #77911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flang/include/flang/Runtime/extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
16 changes: 9 additions & 7 deletions flang/runtime/extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,8 +48,7 @@ extern "C" {

namespace Fortran::runtime {

void GetUsernameEnvVar(
const char *envName, std::byte *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)};
Expand Down Expand Up @@ -98,7 +98,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
Expand All @@ -108,17 +108,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{static_cast<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);
Expand Down
21 changes: 10 additions & 11 deletions flang/unittests/Runtime/CommandTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +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 <cstddef>
#include <cstdlib>

Expand Down Expand Up @@ -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');
}
Expand All @@ -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

Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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 ");
}
Expand Down