Skip to content

[Type checker] Don't overflow array bounds with #fileLiteral #4915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2016
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
1 change: 1 addition & 0 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ getAlternativeLiteralTypes(KnownProtocolKind kind) {
case KnownProtocolKind::ExpressibleByImageLiteral: index = 11; break;
case KnownProtocolKind::ExpressibleByFileReferenceLiteral: index = 12; break;
}
static_assert(NumAlternativeLiteralTypes == 13, "Wrong # of literal types");

// If we already looked for alternative literal types, return those results.
if (AlternativeLiteralTypes[index])
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,8 @@ class ConstraintSystem {
MemberLookups;

/// Cached sets of "alternative" literal types.
Optional<ArrayRef<Type>> AlternativeLiteralTypes[12];
static const unsigned NumAlternativeLiteralTypes = 13;
Optional<ArrayRef<Type>> AlternativeLiteralTypes[NumAlternativeLiteralTypes];

/// \brief Folding set containing all of the locators used in this
/// constraint system.
Expand Down
22 changes: 12 additions & 10 deletions lib/Sema/TypeCheckExprObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,

// Local function to perform name lookup for the current index.
auto performLookup = [&](unsigned idx, Identifier componentName,
SourceLoc componentNameLoc) -> LookupResult {
SourceLoc componentNameLoc,
Type &lookupType) -> LookupResult {
if (state == Beginning)
return lookupUnqualified(dc, componentName, componentNameLoc);

Expand All @@ -170,7 +171,6 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
// Determine the type in which the lookup should occur. If we have
// a bridged value type, this will be the Objective-C class to
// which it is bridged.
Type lookupType;
if (auto bridgedClass = Context.getBridgedToObjC(dc, currentType))
lookupType = bridgedClass;
else
Expand Down Expand Up @@ -210,15 +210,17 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
}

// Look for this component.
LookupResult lookup = performLookup(idx, componentName, componentNameLoc);
Type lookupType;
LookupResult lookup = performLookup(idx, componentName, componentNameLoc,
lookupType);

// If we didn't find anything, try to apply typo-correction.
bool resultsAreFromTypoCorrection = false;
if (!lookup) {
performTypoCorrection(dc, DeclRefKind::Ordinary, currentType,
performTypoCorrection(dc, DeclRefKind::Ordinary, lookupType,
componentName, componentNameLoc,
(currentType ? defaultMemberTypeLookupOptions
: defaultUnqualifiedLookupOptions),
(lookupType ? defaultMemberTypeLookupOptions
: defaultUnqualifiedLookupOptions),
lookup);

if (currentType)
Expand Down Expand Up @@ -263,7 +265,7 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
if (resultsAreFromTypoCorrection)
break;

if (currentType)
if (lookupType)
diagnose(componentNameLoc, diag::ambiguous_member_overload_set,
componentName);
else
Expand Down Expand Up @@ -325,9 +327,9 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
}

Type newType;
if (currentType && !currentType->isAnyObject()) {
newType = currentType->getTypeOfMember(dc->getParentModule(), type,
this);
if (lookupType && !lookupType->isAnyObject()) {
newType = lookupType->getTypeOfMember(dc->getParentModule(), type,
this);
} else {
newType = type->getDeclaredInterfaceType();
}
Expand Down
2 changes: 2 additions & 0 deletions test/Sema/object_literals_osx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ struct Path: _ExpressibleByFileReferenceLiteral {

let p1: Path = #fileLiteral(resourceName: "what.txt")
let p2 = #fileLiteral(resourceName: "what.txt") // expected-error{{could not infer type of file reference literal}} expected-note{{import Foundation to use 'URL' as the default file reference literal type}}

let text = #fileLiteral(resourceName: "TextFile.txt").relativeString! // expected-error{{type of expression is ambiguous without more context}}
14 changes: 14 additions & 0 deletions test/expr/unary/keypath/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class C {
var nonObjC: String? // expected-note{{add '@objc' to expose this var to Objective-C}}{{3-3=@objc }}
}

extension NSArray {
@objc class Foo : NSObject {
@objc var propString: String = ""
}
}

extension Array {
typealias Foo = NSArray.Foo
}

func testKeyPath(a: A, b: B) {
// Property
let _: String = #keyPath(A.propB)
Expand Down Expand Up @@ -84,6 +94,10 @@ func testKeyPath(a: A, b: B) {

// Property with keyword name.
let _: String = #keyPath(A.repeat)

// Nested type of a bridged type (rdar://problem/28061409).
typealias IntArray = [Int]
let _: String = #keyPath(IntArray.Foo.propString)
}

func testAsStaticString() {
Expand Down