Skip to content

Commit

Permalink
Avoid possible NPE in CheckAccessControls
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=196267503
  • Loading branch information
concavelenz authored and blickly committed May 15, 2018
1 parent b39be1f commit ac08cf2
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/com/google/javascript/jscomp/CheckAccessControls.java
Expand Up @@ -612,6 +612,13 @@ static ObjectType getCanonicalInstance(ObjectType obj) {
return ctor == null ? obj : ctor.getInstanceType(); return ctor == null ? obj : ctor.getInstanceType();
} }


private JSType typeOrUnknown(JSType type) {
if (type == null) {
return typeRegistry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
}
return type;
}

/** /**
* Reports an error if the given property is not visible in the current * Reports an error if the given property is not visible in the current
* context. * context.
Expand All @@ -625,7 +632,8 @@ private void checkPropertyVisibility(NodeTraversal t,
return; return;
} }


ObjectType referenceType = castToObject(dereference(getprop.getFirstChild().getJSType())); JSType rawReferenceType = typeOrUnknown(getprop.getFirstChild().getJSType()).autobox();
ObjectType referenceType = castToObject(rawReferenceType);


String propertyName = getprop.getLastChild().getString(); String propertyName = getprop.getLastChild().getString();
boolean isPrivateByConvention = isPrivateByConvention(propertyName); boolean isPrivateByConvention = isPrivateByConvention(propertyName);
Expand Down Expand Up @@ -667,19 +675,19 @@ && propertyIsDeclaredButNotPrivate(getprop, parent)) {
} }
} }


JSType reportType = rawReferenceType;
if (objectType != null) { if (objectType != null) {
Node node = objectType.getOwnPropertyDefSite(propertyName); Node node = objectType.getOwnPropertyDefSite(propertyName);
if (node == null) { if (node == null) {
// Assume the property is public. // Assume the property is public.
return; return;
} }
reportType = objectType;
definingSource = node.getStaticSourceFile(); definingSource = node.getStaticSourceFile();
isClassType = objectType.getOwnPropertyJSDocInfo(propertyName).isConstructor(); isClassType = objectType.getOwnPropertyJSDocInfo(propertyName).isConstructor();
} else if (isPrivateByConvention) { } else if (!isPrivateByConvention && fileOverviewVisibility == null) {
// We can only check visibility references if we know what file // We can only check visibility references if we know what file
// it was defined in. // it was defined in.
objectType = referenceType;
} else if (fileOverviewVisibility == null) {
// Otherwise just assume the property is public. // Otherwise just assume the property is public.
return; return;
} }
Expand All @@ -695,7 +703,7 @@ && propertyIsDeclaredButNotPrivate(getprop, parent)) {
parent, parent,
visibility, visibility,
fileOverviewVisibility, fileOverviewVisibility,
objectType, reportType,
sameInput); sameInput);
} else { } else {
checkNonOverriddenPropertyVisibility( checkNonOverriddenPropertyVisibility(
Expand All @@ -704,7 +712,7 @@ && propertyIsDeclaredButNotPrivate(getprop, parent)) {
parent, parent,
visibility, visibility,
isClassType, isClassType,
objectType, reportType,
referenceSource, referenceSource,
definingSource); definingSource);
} }
Expand All @@ -731,7 +739,7 @@ private void checkOverriddenPropertyVisibility(
Node parent, Node parent,
Visibility visibility, Visibility visibility,
Visibility fileOverviewVisibility, Visibility fileOverviewVisibility,
ObjectType objectType, JSType objectType,
boolean sameInput) { boolean sameInput) {
// Check an ASSIGN statement that's trying to override a property // Check an ASSIGN statement that's trying to override a property
// on a superclass. // on a superclass.
Expand Down Expand Up @@ -766,7 +774,7 @@ private void checkNonOverriddenPropertyVisibility(
Node parent, Node parent,
Visibility visibility, Visibility visibility,
boolean isClassType, boolean isClassType,
ObjectType objectType, JSType objectType,
StaticSourceFile referenceSource, StaticSourceFile referenceSource,
StaticSourceFile definingSource) { StaticSourceFile definingSource) {
// private access is always allowed in the same file. // private access is always allowed in the same file.
Expand Down

0 comments on commit ac08cf2

Please sign in to comment.