aboutsummaryrefslogtreecommitdiff
path: root/flang
diff options
context:
space:
mode:
authorKrzysztof Parzyszek <Krzysztof.Parzyszek@amd.com>2024-03-11 13:39:47 -0500
committerGitHub <noreply@github.com>2024-03-11 13:39:47 -0500
commit5b4c35064760816e4c29921df8f7ff4f2621d4f9 (patch)
tree0c1b52fbf0744e78b6b48ab82cc1eea326e507c7 /flang
parent23be73208d63898611b81d4b93a0c254a40c879c (diff)
downloadllvm-5b4c35064760816e4c29921df8f7ff4f2621d4f9.zip
llvm-5b4c35064760816e4c29921df8f7ff4f2621d4f9.tar.gz
llvm-5b4c35064760816e4c29921df8f7ff4f2621d4f9.tar.bz2
[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.
Diffstat (limited to 'flang')
-rw-r--r--flang/runtime/command.cpp6
1 files changed, 3 insertions, 3 deletions
diff --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp
index 7c44890..fabfe60 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,