Skip to content

[Lex][Clang] Add checking to HeaderMapImpl::getString to make it more robust #131677

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

Merged
merged 2 commits into from
Mar 20, 2025

Conversation

shafik
Copy link
Collaborator

@shafik shafik commented Mar 17, 2025

Static analysis identified the Len - 1 expression in HeaderMapImpl::getString as problematic if Len is zero. After filing this issue:

#130185

Indeed we should be checking MaxLen and verify it is not zero as well.

Fixes: #130185

… robust

Static analysis identified the Len - 1 expression in HeaderMapImpl::getString
as problematic if Len is zero. After filing this issue:

llvm#130185

Indeed we should be checking MaxLen and verify it is not zero as well.

Fixes: llvm#130185
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-clang

Author: Shafik Yaghmour (shafik)

Changes

Static analysis identified the Len - 1 expression in HeaderMapImpl::getString as problematic if Len is zero. After filing this issue:

#130185

Indeed we should be checking MaxLen and verify it is not zero as well.

Fixes: #130185


Full diff: https://github.com/llvm/llvm-project/pull/131677.diff

1 Files Affected:

  • (modified) clang/lib/Lex/HeaderMap.cpp (+3)
diff --git a/clang/lib/Lex/HeaderMap.cpp b/clang/lib/Lex/HeaderMap.cpp
index 14731f29486c8..1b64ecb775da0 100644
--- a/clang/lib/Lex/HeaderMap.cpp
+++ b/clang/lib/Lex/HeaderMap.cpp
@@ -157,6 +157,9 @@ std::optional<StringRef> HeaderMapImpl::getString(unsigned StrTabIdx) const {
   unsigned MaxLen = FileBuffer->getBufferSize() - StrTabIdx;
   unsigned Len = strnlen(Data, MaxLen);
 
+  if (MaxLen == 0)
+    return std::nullopt;
+
   // Check whether the buffer is null-terminated.
   if (Len == MaxLen && Data[Len - 1])
     return std::nullopt;

Copy link
Contributor

@tahonermann tahonermann left a comment

Choose a reason for hiding this comment

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

I added a suggested change, but am approving on the basis that the code in the PR works as is.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM with nit addressed. Test coverage would be nice to add, but not strictly required if this is purely a defensive measure (which it seems to be).

@shafik
Copy link
Collaborator Author

shafik commented Mar 20, 2025

LGTM with nit addressed. Test coverage would be nice to add, but not strictly required if this is purely a defensive measure (which it seems to be).

Exactly.

@shafik shafik merged commit cebc4a1 into llvm:main Mar 20, 2025
11 checks passed
@shafik shafik deleted the len_minus_one_header_impl_fix branch March 22, 2025 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can Len - 1 in HeaderMapImpl::getString overflow?
4 participants