[llvm] [Flang][runtime] Fix RENAME intrinsic, remove trailing blanks (PR #159123)
    via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Wed Sep 17 02:20:37 PDT 2025
    
    
  
================
@@ -60,13 +62,34 @@ void RTDEF(Rename)(const Descriptor &path1, const Descriptor &path2,
     const Descriptor *status, const char *sourceFile, int line) {
   Terminator terminator{sourceFile, line};
 #if !defined(RT_DEVICE_COMPILATION)
+  // Get the raw strings (null-terminated)
   char *pathSrc{EnsureNullTerminated(
       path1.OffsetElement(), path1.ElementBytes(), terminator)};
   char *pathDst{EnsureNullTerminated(
       path2.OffsetElement(), path2.ElementBytes(), terminator)};
+  char *srcFilePath = pathSrc;
+  char *dstFilePath = pathDst;
+
+  // Trim trailing blanks (if string have not been null-terminated)
+  if (!IsNullTerminated(path1.OffsetElement(), path1.ElementBytes())) {
+    auto srcTrimPos{TrimTrailingSpaces(pathSrc, path1.ElementBytes())};
+    char *srcPathTrim{
+        static_cast<char *>(alloca((srcTrimPos + 1)))};
+    std::memcpy(srcPathTrim, pathSrc, srcTrimPos);
+    srcPathTrim[srcTrimPos] = '\0';
+    srcFilePath = srcPathTrim;
+  }
+  if (!IsNullTerminated(path2.OffsetElement(), path2.ElementBytes())) {
+    auto dstTrimPos{TrimTrailingSpaces(pathDst, path2.ElementBytes())};
+    char *dstPathTrim{
+        static_cast<char *>(alloca((dstTrimPos + 1)))};
+    std::memcpy(dstPathTrim, pathDst, dstTrimPos);
+    dstPathTrim[dstTrimPos] = '\0';
+    dstFilePath = dstPathTrim;
+  }
----------------
jeanPerier wrote:
```suggestion
auto srcFilePath{SaveDefaultCharacter(path1.OffsetElement(), TrimTrailingSpaces(path1.OffsetElement(), path1.ElementBytes()), terminator)};
auto dstFilePath{SaveDefaultCharacter(path2.OffsetElement(), TrimTrailingSpaces(path2.OffsetElement(), path2.ElementBytes()), terminator)};
```
(then use srcFilePath.get() and  dstFilePath.get() in rename call below).
Unformatted/untested You should get the same behavior because `CHAR(0)` is not a blank, so trimming will have no effect when this is the last character and no special handling for this case is needed. 
https://github.com/llvm/llvm-project/pull/159123
    
    
More information about the llvm-commits
mailing list