Skip to content

Commit 99117e4

Browse files
committed
better tests and use min length for memcpy
1 parent 85d42d4 commit 99117e4

File tree

6 files changed

+55
-16
lines changed

6 files changed

+55
-16
lines changed

flang/include/flang/Runtime/character.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
#ifndef CHARACTER_H
1111
#define CHARACTER_H
1212

13-
#include <cstddef>
1413
#include <algorithm>
14+
#include <cstddef>
15+
#include <cstring>
1516

1617
template <typename TO, typename FROM>
1718
void CopyAndPad(
@@ -27,7 +28,7 @@ void CopyAndPad(
2728
} else if (toChars <= fromChars) {
2829
std::memcpy(to, from, toChars * sizeof(TO));
2930
} else {
30-
std::memcpy(to, from, fromChars * sizeof(TO));
31+
std::memcpy(to, from, std::min(toChars, fromChars) * sizeof(TO));
3132
for (std::size_t j{fromChars}; j < toChars; ++j) {
3233
to[j] = static_cast<TO>(' ');
3334
}

flang/include/flang/Runtime/extensions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ std::int32_t FORTRAN_PROCEDURE_NAME(iargc)();
2828
void FORTRAN_PROCEDURE_NAME(getarg)(
2929
std::int32_t &n, std::int8_t *arg, std::int64_t length);
3030

31+
// GNU extension subroutine GETLOG(C).
3132
void FORTRAN_PROCEDURE_NAME(getlog)(std::int8_t *name, std::int64_t length);
3233

3334
} // extern "C"

flang/runtime/extensions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
8181
}
8282

8383
void FORTRAN_PROCEDURE_NAME(getlog)(std::int8_t *arg, std::int64_t length) {
84-
const int nameMaxLen = LOGIN_NAME_MAX + 1;
84+
const int nameMaxLen{LOGIN_NAME_MAX + 1};
8585
char str[nameMaxLen];
8686

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

flang/unittests/Runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_flang_unittest(FlangRuntimeTests
77
Complex.cpp
88
CrashHandlerFixture.cpp
99
Derived.cpp
10+
ExtensionTest.cpp
1011
ExternalIOTest.cpp
1112
Format.cpp
1213
Inquiry.cpp

flang/unittests/Runtime/CommandTest.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "gmock/gmock.h"
1111
#include "gtest/gtest.h"
1212
#include "flang/Runtime/descriptor.h"
13+
#include "flang/Runtime/extensions.h"
1314
#include "flang/Runtime/main.h"
1415
#include <cstdlib>
1516

@@ -225,12 +226,6 @@ TEST_F(ZeroArguments, GetCommandArgument) {
225226
CheckMissingArgumentValue(1);
226227
}
227228

228-
TEST_F(ZeroArguments, GetLog) {
229-
CheckMissingArgumentValue(-1);
230-
CheckArgumentValue(commandOnlyArgv[0], 0);
231-
CheckMissingArgumentValue(1);
232-
}
233-
234229
TEST_F(ZeroArguments, GetCommand) { CheckCommandValue(commandOnlyArgv, 1); }
235230

236231
static const char *oneArgArgv[]{"aProgram", "anArgumentOfLength20"};
@@ -248,13 +243,6 @@ TEST_F(OneArgument, GetCommandArgument) {
248243
CheckMissingArgumentValue(2);
249244
}
250245

251-
TEST_F(OneArgument, GetLog) {
252-
CheckMissingArgumentValue(-1);
253-
CheckArgumentValue(oneArgArgv[0], 0);
254-
CheckArgumentValue(oneArgArgv[1], 1);
255-
CheckMissingArgumentValue(2);
256-
}
257-
258246
TEST_F(OneArgument, GetCommand) { CheckCommandValue(oneArgArgv, 2); }
259247

260248
static const char *severalArgsArgv[]{
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===-- flang/unittests/Runtime/ExtensionTest.cpp
2+
//---------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "CrashHandlerFixture.h"
11+
#include "gmock/gmock.h"
12+
#include "gtest/gtest.h"
13+
#include "flang/Runtime/descriptor.h"
14+
#include "flang/Runtime/extensions.h"
15+
#include "flang/Runtime/main.h"
16+
#include <cstdlib>
17+
18+
#ifdef _WIN32
19+
#include <lmcons.h> // UNLEN=256
20+
#define LOGIN_NAME_MAX UNLEN
21+
#else
22+
#include <limits.h>
23+
#endif
24+
25+
using namespace Fortran::runtime;
26+
27+
struct ExtensionTests : CrashHandlerFixture {};
28+
29+
TEST_F(ExtensionTests, GetlogGetName) {
30+
const int charLen{3};
31+
char input[charLen]{"\0\0"};
32+
33+
FORTRAN_PROCEDURE_NAME(getlog)
34+
(reinterpret_cast<std::int8_t *>(input), charLen);
35+
36+
EXPECT_NE(input[0], '\0');
37+
}
38+
39+
TEST_F(ExtensionTests, GetlogPadSpace) {
40+
const int charLen{LOGIN_NAME_MAX +
41+
2}; // guarantee 1 char longer than max, last char should be pad space
42+
char input[charLen];
43+
44+
FORTRAN_PROCEDURE_NAME(getlog)
45+
(reinterpret_cast<std::int8_t *>(input), charLen);
46+
47+
EXPECT_EQ(input[charLen - 1], ' ');
48+
}

0 commit comments

Comments
 (0)