[flang-commits] [flang] [flang][unittests] Fix buffer underrun in LengthWithoutTrailingSpaces (PR #84382)

via flang-commits flang-commits at lists.llvm.org
Thu Mar 7 13:38:28 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-runtime

Author: Krzysztof Parzyszek (kparzysz)

<details>
<summary>Changes</summary>

Account for the descriptor containing a zero-length string. Also, avoid iterating backwards too far.

This was detected by address sanitizer.

---
Full diff: https://github.com/llvm/llvm-project/pull/84382.diff


1 Files Affected:

- (modified) flang/runtime/command.cpp (+6-3) 


``````````diff
diff --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp
index 7c44890545bd3f..141518f324bbcc 100644
--- a/flang/runtime/command.cpp
+++ b/flang/runtime/command.cpp
@@ -196,11 +196,14 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
 }
 
 static std::size_t LengthWithoutTrailingSpaces(const Descriptor &d) {
-  std::size_t s{d.ElementBytes() - 1};
-  while (*d.OffsetElement(s) == ' ') {
+  std::size_t s{d.ElementBytes()}; // This can be 0.
+  if (s == 0) {
+    return 0;
+  }
+  while (s != 0 && *d.OffsetElement(s - 1) == ' ') {
     --s;
   }
-  return s + 1;
+  return s;
 }
 
 std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,

``````````

</details>


https://github.com/llvm/llvm-project/pull/84382


More information about the flang-commits mailing list