Skip to content

Commit e80ce56

Browse files
committed
[Lex][Clang] Add checking to HeaderMapImpl::getString to make it more robust
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
1 parent 6dbe82f commit e80ce56

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

clang/lib/Lex/HeaderMap.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ std::optional<StringRef> HeaderMapImpl::getString(unsigned StrTabIdx) const {
157157
unsigned MaxLen = FileBuffer->getBufferSize() - StrTabIdx;
158158
unsigned Len = strnlen(Data, MaxLen);
159159

160+
if (MaxLen == 0)
161+
return std::nullopt;
162+
160163
// Check whether the buffer is null-terminated.
161164
if (Len == MaxLen && Data[Len - 1])
162165
return std::nullopt;

0 commit comments

Comments
 (0)