Skip to content

Commit c9854e3

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Flow analysis is not available outside of function bodies and initializers.
This fixes a group of crashes during fuzz testing. FAILURE: z({=?[ [NoSuchMethodError: The getter 'flow' was called on null. Receiver: null Tried calling: flow #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) flutter#1 ResolverVisitor.startNullAwareIndexExpression (package:analyzer/src/generated/resolver.dart:743:21) flutter#2 ResolverVisitor.visitIndexExpression (package:analyzer/src/generated/resolver.dart:1552:5) flutter#3 IndexExpressionImpl.accept (package:analyzer/src/dart/ast/ast.dart:5993:49) flutter#4 AstResolver.resolve (package:analyzer/src/summary2/ast_resolver.dart:85:10) #5 DefaultValueResolver._parameter (package:analyzer/src/summary2/default_value_resolver.dart:107:18) flutter#6 DefaultValueResolver._parameters (package:analyzer/src/summary2/default_value_resolver.dart:121:7) flutter#7 DefaultValueResolver._function (package:analyzer/src/summary2/default_value_resolver.dart:89:5) flutter#8 DefaultValueResolver.resolve (package:analyzer/src/summary2/default_value_resolver.dart:47:9) flutter#9 LibraryBuilder.resolveDefaultValues (package:analyzer/src/summary2/library_builder.dart:313:43) flutter#10 Linker._resolveDefaultValues (package:analyzer/src/summary2/link.dart:230:15) flutter#11 Linker._buildOutlines (package:analyzer/src/summary2/link.dart:89:5) flutter#12 Linker.link (package:analyzer/src/summary2/link.dart:69:5) flutter#13 link (package:analyzer/src/summary2/link.dart:33:10) Change-Id: I6145a1f728d432be8d0fa55235858e091030461c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175187 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent ce74d6e commit c9854e3

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

pkg/analyzer/lib/src/generated/resolver.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ class ResolverVisitor extends ScopedVisitor {
739739

740740
void startNullAwareIndexExpression(IndexExpression node) {
741741
if (_migratableAstInfoProvider.isIndexExpressionNullAware(node) &&
742-
_isNonNullableByDefault) {
742+
_flowAnalysis != null) {
743743
_flowAnalysis.flow.nullAwareAccess_rightBegin(
744744
node.target, node.realTarget.staticType ?? typeProvider.dynamicType);
745745
_unfinishedNullShorts.add(node.nullShortingTermination);
@@ -750,7 +750,7 @@ class ResolverVisitor extends ScopedVisitor {
750750
PropertyAccess node,
751751
) {
752752
if (_migratableAstInfoProvider.isPropertyAccessNullAware(node) &&
753-
_isNonNullableByDefault) {
753+
_flowAnalysis != null) {
754754
var target = node.target;
755755
if (target is SimpleIdentifier && target.staticElement is ClassElement) {
756756
// `?.` to access static methods is equivalent to `.`, so do nothing.
@@ -948,7 +948,7 @@ class ResolverVisitor extends ScopedVisitor {
948948
InferenceContext.setTypeFromNode(node.target, node);
949949
node.target.accept(this);
950950

951-
if (node.isNullAware && _isNonNullableByDefault) {
951+
if (node.isNullAware && _flowAnalysis != null) {
952952
_flowAnalysis.flow.nullAwareAccess_rightBegin(
953953
node.target, node.target.staticType ?? typeProvider.dynamicType);
954954
_unfinishedNullShorts.add(node.nullShortingTermination);
@@ -1652,7 +1652,7 @@ class ResolverVisitor extends ScopedVisitor {
16521652
target?.accept(this);
16531653

16541654
if (_migratableAstInfoProvider.isMethodInvocationNullAware(node) &&
1655-
_isNonNullableByDefault) {
1655+
_flowAnalysis != null) {
16561656
if (target is SimpleIdentifier && target.staticElement is ClassElement) {
16571657
// `?.` to access static methods is equivalent to `.`, so do nothing.
16581658
} else {

pkg/analyzer/test/src/dart/resolution/index_expression_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ main() {
1616

1717
@reflectiveTest
1818
class IndexExpressionTest extends PubPackageResolutionTest {
19+
test_invalid_inDefaultValue_nullAware() async {
20+
await assertInvalidTestCode(r'''
21+
void f({a = b?[0]}) {}
22+
''');
23+
24+
assertIndexExpression(
25+
findNode.index('[0]'),
26+
readElement: null,
27+
writeElement: null,
28+
type: 'dynamic',
29+
);
30+
}
31+
1932
test_read() async {
2033
await assertNoErrorsInCode(r'''
2134
class A {

pkg/analyzer/test/src/dart/resolution/method_invocation_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,20 @@ class B extends A {
17031703
assertSuperExpression(invocation.target);
17041704
}
17051705

1706+
test_invalid_inDefaultValue_nullAware() async {
1707+
await assertInvalidTestCode('''
1708+
void f({a = b?.foo()}) {}
1709+
''');
1710+
1711+
assertMethodInvocation2(
1712+
findNode.methodInvocation('?.foo()'),
1713+
element: null,
1714+
typeArgumentTypes: [],
1715+
invokeType: 'dynamic',
1716+
type: 'dynamic',
1717+
);
1718+
}
1719+
17061720
test_namedArgument() async {
17071721
var question = typeToStringWithNullability ? '?' : '';
17081722
await assertNoErrorsInCode('''

pkg/analyzer/test/src/dart/resolution/property_access_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,30 @@ void f() {
238238
}
239239
}
240240

241+
test_invalid_inDefaultValue_nullAware() async {
242+
await assertInvalidTestCode('''
243+
void f({a = b?.foo}) {}
244+
''');
245+
246+
assertPropertyAccess2(
247+
findNode.propertyAccess('?.foo'),
248+
element: null,
249+
type: 'dynamic',
250+
);
251+
}
252+
253+
test_invalid_inDefaultValue_nullAware_cascade() async {
254+
await assertInvalidTestCode('''
255+
void f({a = b?..foo}) {}
256+
''');
257+
258+
assertPropertyAccess2(
259+
findNode.propertyAccess('?..foo'),
260+
element: null,
261+
type: 'dynamic',
262+
);
263+
}
264+
241265
test_ofExtension_read() async {
242266
await assertNoErrorsInCode('''
243267
class A {}

pkg/analyzer/test/src/dart/resolution/resolution.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,13 @@ mixin ResolutionTest implements ResourceProviderMixin {
448448
expectedPrefix: expectedPrefix);
449449
}
450450

451+
/// Resolve the [code], and ensure that it can be resolved without a crash,
452+
/// and is invalid, i.e. produces a diagnostic.
453+
Future<void> assertInvalidTestCode(String code) async {
454+
await resolveTestCode(code);
455+
assertHasTestErrors();
456+
}
457+
451458
void assertInvokeType(Expression node, String expected) {
452459
DartType actual;
453460
if (node is BinaryExpression) {

0 commit comments

Comments
 (0)