Skip to content

Commit 664be1d

Browse files
committed
[Flang][runtime] Fix RENAME intrinsic, remove trailing blanks
The RENAME intrinsic did not correctly remove trailing spaces from filenames. This PR introduces code to remove trailing blanks as documented by GFortran.
1 parent fba26bc commit 664be1d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

flang-rt/lib/runtime/misc-intrinsic.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,20 @@ void RTDEF(Rename)(const Descriptor &path1, const Descriptor &path2,
6565
char *pathDst{EnsureNullTerminated(
6666
path2.OffsetElement(), path2.ElementBytes(), terminator)};
6767

68+
// Trim trailing blanks
69+
auto srcTrimPos{TrimTrailingSpaces(pathSrc, path1.ElementBytes())};
70+
auto dstTrimPos{TrimTrailingSpaces(pathDst, path2.ElementBytes())};
71+
char *srcPathTrim{
72+
static_cast<char *>(alloca((srcTrimPos + 1) * sizeof(char)))};
73+
char *dstPathTrim{
74+
static_cast<char *>(alloca((dstTrimPos + 1) * sizeof(char)))};
75+
std::strncpy(srcPathTrim, pathSrc, srcTrimPos);
76+
std::strncpy(dstPathTrim, pathDst, dstTrimPos);
77+
srcPathTrim[srcTrimPos] = '\0';
78+
dstPathTrim[dstTrimPos] = '\0';
79+
6880
// We simply call rename(2) from POSIX
69-
int result{rename(pathSrc, pathDst)};
81+
int result{rename(srcPathTrim, dstPathTrim)};
7082
if (status) {
7183
// When an error has happened,
7284
int errorCode{0}; // Assume success

0 commit comments

Comments
 (0)