[clang] cebc4a1 - [Lex][Clang] Add checking to HeaderMapImpl::getString to make it more robust (#131677)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 20 08:17:14 PDT 2025
Author: Shafik Yaghmour
Date: 2025-03-20T08:17:11-07:00
New Revision: cebc4a167c518d83bb95fddd1c0dd6eebed3d505
URL: https://github.com/llvm/llvm-project/commit/cebc4a167c518d83bb95fddd1c0dd6eebed3d505
DIFF: https://github.com/llvm/llvm-project/commit/cebc4a167c518d83bb95fddd1c0dd6eebed3d505.diff
LOG: [Lex][Clang] Add checking to HeaderMapImpl::getString to make it more robust (#131677)
Static analysis identified the Len - 1 expression in
HeaderMapImpl::getString as problematic if Len is zero. After filing
this issue:
https://github.com/llvm/llvm-project/issues/130185
Indeed we should be checking MaxLen and verify it is not zero as well.
Fixes: https://github.com/llvm/llvm-project/issues/130185
Added:
Modified:
clang/lib/Lex/HeaderMap.cpp
Removed:
################################################################################
diff --git a/clang/lib/Lex/HeaderMap.cpp b/clang/lib/Lex/HeaderMap.cpp
index 14731f29486c8..588b32ee9ca8e 100644
--- a/clang/lib/Lex/HeaderMap.cpp
+++ b/clang/lib/Lex/HeaderMap.cpp
@@ -155,6 +155,10 @@ std::optional<StringRef> HeaderMapImpl::getString(unsigned StrTabIdx) const {
const char *Data = FileBuffer->getBufferStart() + StrTabIdx;
unsigned MaxLen = FileBuffer->getBufferSize() - StrTabIdx;
+
+ if (MaxLen == 0)
+ return std::nullopt;
+
unsigned Len = strnlen(Data, MaxLen);
// Check whether the buffer is null-terminated.
More information about the cfe-commits
mailing list