aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Lex/HeaderMap.cpp
diff options
context:
space:
mode:
authorShafik Yaghmour <shafik.yaghmour@intel.com>2025-03-20 08:17:11 -0700
committerGitHub <noreply@github.com>2025-03-20 08:17:11 -0700
commitcebc4a167c518d83bb95fddd1c0dd6eebed3d505 (patch)
treed6c43b175cc999080997f8b4b1c89d2d33caeee1 /clang/lib/Lex/HeaderMap.cpp
parent392b8f3e6353a1bcb980b73491a26cd5d04020bd (diff)
downloadllvm-cebc4a167c518d83bb95fddd1c0dd6eebed3d505.zip
llvm-cebc4a167c518d83bb95fddd1c0dd6eebed3d505.tar.gz
llvm-cebc4a167c518d83bb95fddd1c0dd6eebed3d505.tar.bz2
[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
Diffstat (limited to 'clang/lib/Lex/HeaderMap.cpp')
-rw-r--r--clang/lib/Lex/HeaderMap.cpp4
1 files changed, 4 insertions, 0 deletions
diff --git a/clang/lib/Lex/HeaderMap.cpp b/clang/lib/Lex/HeaderMap.cpp
index 14731f29..588b32e 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.