Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][unittests] Fix buffer underrun in LengthWithoutTrailingSpaces #84382

Merged
merged 3 commits into from
Mar 11, 2024

Conversation

kparzysz
Copy link
Contributor

@kparzysz kparzysz commented Mar 7, 2024

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

This was detected by address sanitizer.

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

This was detected by address sanitizer.
@kparzysz kparzysz requested a review from rovka March 7, 2024 21:37
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Mar 7, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 7, 2024

@llvm/pr-subscribers-flang-runtime

Author: Krzysztof Parzyszek (kparzysz)

Changes

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 --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,

@kparzysz kparzysz requested a review from klausler March 11, 2024 13:41
std::size_t s{d.ElementBytes() - 1};
while (*d.OffsetElement(s) == ' ') {
std::size_t s{d.ElementBytes()}; // This can be 0.
if (s == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement is unnecessary.

@kparzysz kparzysz merged commit 5b4c350 into llvm:main Mar 11, 2024
4 checks passed
@kparzysz kparzysz deleted the users/kparzysz/asan-03 branch March 11, 2024 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants