[flang-commits] [flang] [flang][unittests] Fix buffer underrun in LengthWithoutTrailingSpaces (PR #84382)
Krzysztof Parzyszek via flang-commits
flang-commits at lists.llvm.org
Thu Mar 7 13:37:58 PST 2024
https://github.com/kparzysz created https://github.com/llvm/llvm-project/pull/84382
Account for the descriptor containing a zero-length string. Also, avoid iterating backwards too far.
This was detected by address sanitizer.
>From fecccb8e27c808d9b7812fe86b50667543875fc8 Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Thu, 7 Mar 2024 15:34:49 -0600
Subject: [PATCH] [flang][unittests] Fix buffer underrun in
LengthWithoutTrailingSpaces
Account for the descriptor containing a zero-length string. Also, avoid
iterating backwards too far.
This was detected by address sanitizer.
---
flang/runtime/command.cpp | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
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,
More information about the flang-commits
mailing list