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

via flang-commits flang-commits at lists.llvm.org
Mon Mar 11 11:39:51 PDT 2024


Author: Krzysztof Parzyszek
Date: 2024-03-11T13:39:47-05:00
New Revision: 5b4c35064760816e4c29921df8f7ff4f2621d4f9

URL: https://github.com/llvm/llvm-project/commit/5b4c35064760816e4c29921df8f7ff4f2621d4f9
DIFF: https://github.com/llvm/llvm-project/commit/5b4c35064760816e4c29921df8f7ff4f2621d4f9.diff

LOG: [flang][unittests] Fix buffer underrun in LengthWithoutTrailingSpaces (#84382)

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

This was detected by address sanitizer.

Added: 
    

Modified: 
    flang/runtime/command.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp
index 7c44890545bd3f..fabfe601688bbf 100644
--- a/flang/runtime/command.cpp
+++ b/flang/runtime/command.cpp
@@ -196,11 +196,11 @@ 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.
+  while (s != 0 && *d.OffsetElement(s - 1) == ' ') {
     --s;
   }
-  return s + 1;
+  return s;
 }
 
 std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,


        


More information about the flang-commits mailing list