Skip to content

Commit a49994d

Browse files
committed
[Driver] Don’t crash if the output file map is incomplete
When using VS Code, it semi-frequently happens that sourcekitd is reading the output file map and the memory buffer containing the memory buffer containing the output file map is not complete and terminates at distinct powers of 2. I managed to attach a debugger once and the memory buffer containing the output file had size 0x3000 = 12288 while the actual output file map is 20319 bytes large. One hypothesis for this is that the read of output file map is racing with a write from a SwiftPM build but I haven’t been able to confirm it. In either case, the result is that the output file map buffer ends with an unterminated string literal, which causes `getKey()` or `getValue()` in the JSON parser to return `nullptr` and then consequently hits an assertion failure in `dyn_cast`. Add a check for nullptr before invoking `dyn_cast` just like we do it in the outer `for` loop. rdar://122364031
1 parent 11ef6e5 commit a49994d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/Basic/OutputFileMap.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ OutputFileMap::parse(std::unique_ptr<llvm::MemoryBuffer> Buffer,
209209
llvm::yaml::Node *Key = OutputPair.getKey();
210210
llvm::yaml::Node *Value = OutputPair.getValue();
211211

212+
if (!Key)
213+
return constructError("bad kind");
214+
215+
if (!Value)
216+
return constructError("bad path");
217+
212218
auto *KindNode = dyn_cast<llvm::yaml::ScalarNode>(Key);
213219
if (!KindNode)
214220
return constructError("kind not a ScalarNode");

test/Driver/malformed-ofm.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Ensure that an incomplete output-file-map path does not crash the driver,
2+
// but instead outputs a nice diagnostic.
3+
//
4+
// RUN: %empty-directory(%t)
5+
// RUN: split-file %s %t
6+
// RUN: not %swiftc_driver -c %S/../Inputs/empty.swift -output-file-map %t/malformed-output-file-map.json 2>&1 | %FileCheck %s
7+
//
8+
// CHECK: error: Expected quote at end of scalar
9+
// CHECK-NOT: Assertion failed
10+
11+
//--- malformed-output-file-map.json
12+
{
13+
"/path/to/some/file.swift": {
14+
"dependencies": "

0 commit comments

Comments
 (0)