Skip to content

Commit cebc4a1

Browse files
authored
[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: #130185 Indeed we should be checking MaxLen and verify it is not zero as well. Fixes: #130185
1 parent 392b8f3 commit cebc4a1

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

clang/lib/Lex/HeaderMap.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ std::optional<StringRef> HeaderMapImpl::getString(unsigned StrTabIdx) const {
155155

156156
const char *Data = FileBuffer->getBufferStart() + StrTabIdx;
157157
unsigned MaxLen = FileBuffer->getBufferSize() - StrTabIdx;
158+
159+
if (MaxLen == 0)
160+
return std::nullopt;
161+
158162
unsigned Len = strnlen(Data, MaxLen);
159163

160164
// Check whether the buffer is null-terminated.

0 commit comments

Comments
 (0)