From 254c6ff7656bb0d17868f3fd17585200c80fc6c1 Mon Sep 17 00:00:00 2001 From: Ben Barham Date: Thu, 8 Oct 2020 08:14:58 +1000 Subject: [PATCH] [clang][Index] Add guard to IndexUnitReader module name reading Check that ModuleNamesBuffer is valid before attempting to read strings from it to avoid possible segfaults. Wasn't able to come up with a test case, but this does happen based on reported crashes. Also use StringRef.substr, which returns an empty string if the index is out of bounds, for even further safety. Resolves rdar://69809414 --- clang/lib/Index/IndexUnitReader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/Index/IndexUnitReader.cpp b/clang/lib/Index/IndexUnitReader.cpp index 30e474d6d6165..a1f8ca7798407 100644 --- a/clang/lib/Index/IndexUnitReader.cpp +++ b/clang/lib/Index/IndexUnitReader.cpp @@ -378,10 +378,10 @@ void IndexUnitReaderImpl::constructFilePath(SmallVectorImpl &PathBuf, } StringRef IndexUnitReaderImpl::getModuleName(int ModuleIndex) { - if (ModuleIndex < 0) + if (ModuleIndex < 0 || ModuleNamesBuffer.empty()) return StringRef(); auto &ModInfo = Modules[ModuleIndex]; - return StringRef(ModuleNamesBuffer.data()+ModInfo.NameOffset, ModInfo.NameSize); + return ModuleNamesBuffer.substr(ModInfo.NameOffset, ModInfo.NameSize); }