Skip to content

Commit 254c6ff

Browse files
committed
[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
1 parent 9cf602f commit 254c6ff

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

clang/lib/Index/IndexUnitReader.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,10 @@ void IndexUnitReaderImpl::constructFilePath(SmallVectorImpl<char> &PathBuf,
378378
}
379379

380380
StringRef IndexUnitReaderImpl::getModuleName(int ModuleIndex) {
381-
if (ModuleIndex < 0)
381+
if (ModuleIndex < 0 || ModuleNamesBuffer.empty())
382382
return StringRef();
383383
auto &ModInfo = Modules[ModuleIndex];
384-
return StringRef(ModuleNamesBuffer.data()+ModInfo.NameOffset, ModInfo.NameSize);
384+
return ModuleNamesBuffer.substr(ModInfo.NameOffset, ModInfo.NameSize);
385385
}
386386

387387

0 commit comments

Comments
 (0)