[flang-commits] [flang] [flang][unittests] Fix buffer underrun in LengthWithoutTrailingSpaces (PR #84382)
Krzysztof Parzyszek via flang-commits
flang-commits at lists.llvm.org
Mon Mar 11 09:18:38 PDT 2024
https://github.com/kparzysz updated https://github.com/llvm/llvm-project/pull/84382
>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 1/2] [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,
>From 58c8af45278df0b4859c15d736ac67769d8ad450 Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Mon, 11 Mar 2024 11:18:00 -0500
Subject: [PATCH 2/2] Remove unnecessary check
---
flang/runtime/command.cpp | 3 ---
1 file changed, 3 deletions(-)
diff --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp
index 141518f324bbcc..fabfe601688bbf 100644
--- a/flang/runtime/command.cpp
+++ b/flang/runtime/command.cpp
@@ -197,9 +197,6 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
static std::size_t LengthWithoutTrailingSpaces(const Descriptor &d) {
std::size_t s{d.ElementBytes()}; // This can be 0.
- if (s == 0) {
- return 0;
- }
while (s != 0 && *d.OffsetElement(s - 1) == ' ') {
--s;
}
More information about the flang-commits
mailing list