Unverified Commit 6be81ebf authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Be more resilient to bad data from VM (#18132)

Fixes https://github.com/flutter/flutter/issues/14517
Or at least, fixes the crash part of that.
parent e396cc79
...@@ -412,7 +412,24 @@ class HotRunner extends ResidentRunner { ...@@ -412,7 +412,24 @@ class HotRunner extends ResidentRunner {
/// Prints errors if [printErrors] is [true]. /// Prints errors if [printErrors] is [true].
static bool validateReloadReport(Map<String, dynamic> reloadReport, static bool validateReloadReport(Map<String, dynamic> reloadReport,
{ bool printErrors: true }) { { bool printErrors: true }) {
if (reloadReport['type'] != 'ReloadReport') { if (reloadReport == null) {
if (printErrors)
printError('Hot reload did not receive reload report.');
return false;
}
if (!(reloadReport['type'] == 'ReloadReport' &&
(reloadReport['success'] == true ||
(reloadReport['success'] == false &&
(reloadReport['details'] is Map<String, dynamic> &&
reloadReport['details']['notices'] is List<dynamic> &&
reloadReport['details']['notices'].isNotEmpty &&
reloadReport['details']['notices'].every(
(dynamic item) => item is Map<String, dynamic> && item['message'] is String
)
)
)
)
)) {
if (printErrors) if (printErrors)
printError('Hot reload received invalid response: $reloadReport'); printError('Hot reload received invalid response: $reloadReport');
return false; return false;
......
...@@ -11,9 +11,76 @@ void main() { ...@@ -11,9 +11,76 @@ void main() {
group('validateReloadReport', () { group('validateReloadReport', () {
testUsingContext('invalid', () async { testUsingContext('invalid', () async {
expect(HotRunner.validateReloadReport(<String, dynamic>{}), false); expect(HotRunner.validateReloadReport(<String, dynamic>{}), false);
expect(HotRunner.validateReloadReport( expect(HotRunner.validateReloadReport(<String, dynamic>{
<String, dynamic>{'type': 'ReloadReport', 'success': true, 'details': <String, dynamic>{}}), 'type': 'ReloadReport',
true); 'success': false,
'details': <String, dynamic>{},
}), false);
expect(HotRunner.validateReloadReport(<String, dynamic>{
'type': 'ReloadReport',
'success': false,
'details': <String, dynamic>{
'notices': <Map<String, dynamic>>[
],
},
}), false);
expect(HotRunner.validateReloadReport(<String, dynamic>{
'type': 'ReloadReport',
'success': false,
'details': <String, dynamic>{
'notices': <String, dynamic>{
'message': 'error',
},
},
}), false);
expect(HotRunner.validateReloadReport(<String, dynamic>{
'type': 'ReloadReport',
'success': false,
'details': <String, dynamic>{
'notices': <Map<String, dynamic>>[],
},
}), false);
expect(HotRunner.validateReloadReport(<String, dynamic>{
'type': 'ReloadReport',
'success': false,
'details': <String, dynamic>{
'notices': <Map<String, dynamic>>[
<String, dynamic>{ 'message': false, }
],
},
}), false);
expect(HotRunner.validateReloadReport(<String, dynamic>{
'type': 'ReloadReport',
'success': false,
'details': <String, dynamic>{
'notices': <Map<String, dynamic>>[
<String, dynamic>{ 'message': <String>['error'], },
],
},
}), false);
expect(HotRunner.validateReloadReport(<String, dynamic>{
'type': 'ReloadReport',
'success': false,
'details': <String, dynamic>{
'notices': <Map<String, dynamic>>[
<String, dynamic>{ 'message': 'error', },
<String, dynamic>{ 'message': <String>['error'], },
],
},
}), false);
expect(HotRunner.validateReloadReport(<String, dynamic>{
'type': 'ReloadReport',
'success': false,
'details': <String, dynamic>{
'notices': <Map<String, dynamic>>[
<String, dynamic>{ 'message': 'error', },
],
},
}), false);
expect(HotRunner.validateReloadReport(<String, dynamic>{
'type': 'ReloadReport',
'success': true,
}), true);
}); });
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment