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 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.