From 1eb0bb52e1c445dcc48fc8d43fa77144a8f3d420 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 13 Jan 2021 14:25:16 -0800 Subject: [PATCH] [flutter_tools] dont crash on invalid utf8 in pubspec (#73891) --- packages/flutter_tools/lib/src/project.dart | 6 ++++++ .../test/general.shard/project_test.dart | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/flutter_tools/lib/src/project.dart b/packages/flutter_tools/lib/src/project.dart index e400365d3b8..2621b0bebed 100644 --- a/packages/flutter_tools/lib/src/project.dart +++ b/packages/flutter_tools/lib/src/project.dart @@ -221,6 +221,12 @@ class FlutterProject { } on YamlException catch (e) { logger.printStatus('Error detected in pubspec.yaml:', emphasis: true); logger.printError('$e'); + } on FormatException catch (e) { + logger.printError('Error detected while parsing pubspec.yaml:', emphasis: true); + logger.printError('$e'); + } on FileSystemException catch (e) { + logger.printError('Error detected while reading pubspec.yaml:', emphasis: true); + logger.printError('$e'); } if (manifest == null) { throwToolExit('Please correct the pubspec.yaml file at $path'); diff --git a/packages/flutter_tools/test/general.shard/project_test.dart b/packages/flutter_tools/test/general.shard/project_test.dart index d8d83dc95a3..1644368825e 100644 --- a/packages/flutter_tools/test/general.shard/project_test.dart +++ b/packages/flutter_tools/test/general.shard/project_test.dart @@ -37,6 +37,22 @@ void main() { ); }); + testWithoutContext('invalid utf8 throws a tool exit', () { + final FileSystem fileSystem = MemoryFileSystem.test(); + final FlutterProjectFactory projectFactory = FlutterProjectFactory( + fileSystem: fileSystem, + logger: BufferLogger.test(), + ); + fileSystem.file('pubspec.yaml').writeAsBytesSync([0xFFFE]); + + /// Technically this should throw a FileSystemException but this is + /// currently a bug in package:file. + expect( + () => projectFactory.fromDirectory(fileSystem.currentDirectory), + throwsToolExit(), + ); + }); + _testInMemory('fails on invalid pubspec.yaml', () async { final Directory directory = globals.fs.directory('myproject'); directory.childFile('pubspec.yaml')