Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions test/SourceKit/CursorInfo/invalid_offset.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let a = 12

// rdar://problem/30346106
// Invalid offset should trigger a crash.

// RUN: %sourcekitd-test \
// RUN: -req=open %s -- %s == \
// RUN: -req=edit -async -offset=0 -length=200 -replace='' %s -- %s == \
// RUN: -req=cursor -offset=250 %s -- %s \
// RUN: | %FileCheck %s

// CHECK: <empty cursor info>
5 changes: 5 additions & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,11 @@ void walkRelatedDecls(const ValueDecl *VD, const FnTy &Fn) {
static StringRef getSourceToken(unsigned Offset,
ImmutableTextSnapshotRef Snap) {
auto MemBuf = Snap->getBuffer()->getInternalBuffer();

// FIXME: Invalid offset shouldn't reach here.
if (Offset >= MemBuf->getBufferSize())
return StringRef();

SourceManager SM;
auto MemBufRef = llvm::MemoryBuffer::getMemBuffer(MemBuf->getBuffer(),
MemBuf->getBufferIdentifier());
Expand Down