@@ -87,6 +87,19 @@ Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
8787 return Error::success ();
8888}
8989
90+ static Error validatePdbMagic (StringRef PdbPath) {
91+ file_magic Magic;
92+ if (auto EC = identify_magic (PdbPath, Magic))
93+ return make_error<RawError>(EC);
94+
95+ if (Magic != file_magic::pdb)
96+ return make_error<RawError>(
97+ raw_error_code::invalid_format,
98+ " The input file did not contain the pdb file magic." );
99+
100+ return Error::success ();
101+ }
102+
90103static Expected<std::unique_ptr<PDBFile>>
91104loadPdbFile (StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
92105 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
@@ -97,10 +110,8 @@ loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
97110 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move (*ErrorOrBuffer);
98111
99112 PdbPath = Buffer->getBufferIdentifier ();
100- file_magic Magic;
101- auto EC = identify_magic (PdbPath, Magic);
102- if (EC || Magic != file_magic::pdb)
103- return make_error<RawError>(EC);
113+ if (auto EC = validatePdbMagic (PdbPath))
114+ return std::move (EC);
104115
105116 auto Stream = std::make_unique<MemoryBufferByteStream>(
106117 std::move (Buffer), llvm::endianness::little);
@@ -152,10 +163,8 @@ Error NativeSession::createFromExe(StringRef ExePath,
152163 if (!PdbPath)
153164 return PdbPath.takeError ();
154165
155- file_magic Magic;
156- auto EC = identify_magic (PdbPath.get (), Magic);
157- if (EC || Magic != file_magic::pdb)
158- return make_error<RawError>(EC);
166+ if (auto EC = validatePdbMagic (PdbPath.get ()))
167+ return EC;
159168
160169 auto Allocator = std::make_unique<BumpPtrAllocator>();
161170 auto File = loadPdbFile (PdbPath.get (), Allocator);
0 commit comments