Lex: Never overflow the file in HeaderMap::lookupFilename()
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Sun, 21 Feb 2016 00:14:36 +0000 (00:14 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Sun, 21 Feb 2016 00:14:36 +0000 (00:14 +0000)
commit96a01fa04671d7c554007d256294f4ddd96d3eee
tree866023e74213cde3ce35aef41cf94a21222803f6
parent471efd244a7f970948cb0d189ec41d1840e9607b
Lex: Never overflow the file in HeaderMap::lookupFilename()

If a header map file is corrupt, the strings in the string table may not
be null-terminated.  The logic here previously relied on `MemoryBuffer`
always being null-terminated, but this isn't actually guaranteed by the
class AFAICT.  Moreover, we're seeing a lot of crash traces at calls to
`strlen()` inside of `lookupFilename()`, so something is going wrong
there.

Instead, use `strnlen()` to get the length, and check for corruption.

Also remove code paths that could call `StringRef(nullptr)`.  r261459
made these rather obvious (although they'd been there all along).

llvm-svn: 261461
clang/lib/Lex/HeaderMap.cpp
clang/unittests/Lex/HeaderMapTest.cpp