Skip to content

Commit e8497a3

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[analyzer/ffi] Fix null exceptions
The analyzer has not been migrated, so this is based on manual inspecation of the API about what can be `null` for now. Note with the analyzer from master (running in nnbd by default) this file contains many nullable to non-null assignments. However, we cannot migrate this file to nnbd yet because of the language version it's used in. Closes: #44763 Change-Id: If0432341e7fefb973b0229bcc325eeefed84f7cb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/180821 Reviewed-by: Vyacheslav Egorov <[email protected]> Commit-Queue: Daco Harkes <[email protected]>
1 parent a828308 commit e8497a3

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
8585
// No classes from the FFI may be explicitly implemented.
8686
void checkSupertype(TypeName typename, FfiCode subtypeOfFfiCode,
8787
FfiCode subtypeOfStructCode) {
88-
final superName = typename.name.staticElement?.name;
88+
final superName = typename.name?.staticElement?.name;
8989
if (superName == _allocatorClassName) {
9090
return;
9191
}
@@ -139,9 +139,9 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
139139

140140
@override
141141
void visitFunctionExpressionInvocation(FunctionExpressionInvocation node) {
142-
Element element = node.staticElement;
142+
Element element = node?.staticElement;
143143
if (element is MethodElement) {
144-
Element enclosingElement = element.enclosingElement;
144+
Element enclosingElement = element?.enclosingElement;
145145
if (enclosingElement is ExtensionElement) {
146146
if (_isAllocatorExtension(enclosingElement) &&
147147
element.name == _allocateExtensionMethodName) {
@@ -154,7 +154,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
154154

155155
@override
156156
void visitIndexExpression(IndexExpression node) {
157-
Element element = node.staticElement;
157+
Element element = node?.staticElement;
158158
Element enclosingElement = element?.enclosingElement;
159159
if (enclosingElement is ExtensionElement) {
160160
if (_isNativeStructPointerExtension(enclosingElement)) {
@@ -167,7 +167,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
167167

168168
@override
169169
void visitMethodInvocation(MethodInvocation node) {
170-
Element element = node.methodName.staticElement;
170+
Element element = node.methodName?.staticElement;
171171
if (element is MethodElement) {
172172
Element enclosingElement = element.enclosingElement;
173173
if (enclosingElement is ClassElement) {
@@ -190,7 +190,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
190190
}
191191
}
192192
} else if (element is FunctionElement) {
193-
Element enclosingElement = element.enclosingElement;
193+
Element enclosingElement = element?.enclosingElement;
194194
if (enclosingElement is CompilationUnitElement) {
195195
if (element.library.name == 'dart.ffi') {
196196
if (element.name == 'sizeOf') {
@@ -468,6 +468,9 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
468468
}
469469

470470
void _validateAllocate(FunctionExpressionInvocation node) {
471+
if (node.typeArgumentTypes.length != 1) {
472+
return;
473+
}
471474
final DartType dartType = node.typeArgumentTypes[0];
472475
if (!_isValidFfiNativeType(dartType, true, true)) {
473476
final AstNode errorNode = node;
@@ -631,7 +634,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
631634

632635
void _validateElementAt(MethodInvocation node) {
633636
Expression target = node.realTarget;
634-
DartType targetType = target.staticType;
637+
DartType targetType = target?.staticType;
635638
if (targetType is InterfaceType &&
636639
_isPointer(targetType.element) &&
637640
targetType.typeArguments.length == 1) {
@@ -772,7 +775,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
772775
}
773776

774777
void _validateRefIndexed(IndexExpression node) {
775-
DartType targetType = node.target.staticType;
778+
DartType targetType = node.target?.staticType;
776779
if (!_isValidFfiNativeType(targetType, false, true)) {
777780
final AstNode errorNode = node;
778781
_errorReporter.reportErrorForNode(
@@ -792,7 +795,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
792795
}
793796

794797
void _validateRefPropertyAccess(PropertyAccess node) {
795-
DartType targetType = node.target.staticType;
798+
DartType targetType = node.target?.staticType;
796799
if (!_isValidFfiNativeType(targetType, false, true)) {
797800
final AstNode errorNode = node;
798801
_errorReporter.reportErrorForNode(
@@ -801,6 +804,9 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
801804
}
802805

803806
void _validateSizeOf(MethodInvocation node) {
807+
if (node.typeArgumentTypes.length != 1) {
808+
return;
809+
}
804810
final DartType T = node.typeArgumentTypes[0];
805811
if (!_isValidFfiNativeType(T, true, true)) {
806812
final AstNode errorNode = node;

0 commit comments

Comments
 (0)