diff --git a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm index 57ef1eb752..a8653477e2 100644 --- a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm +++ b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm @@ -89,6 +89,7 @@ FLUTTER_ASSERT_ARC } BOOL isSimpleValue = NO; id decoded = nil; + NSError* error; if (0 < message.length) { UInt8 first; [message getBytes:&first length:1]; @@ -105,9 +106,9 @@ FLUTTER_ASSERT_ARC [expandedMessage replaceBytesInRange:NSMakeRange(message.length + 1, 1) withBytes:&end]; message = expandedMessage; } - decoded = [NSJSONSerialization JSONObjectWithData:message options:0 error:nil]; + decoded = [NSJSONSerialization JSONObjectWithData:message options:0 error:&error]; } - NSAssert(decoded, @"Invalid JSON message, decoding failed"); + NSAssert(decoded, @"Invalid JSON message, decoding failed: %@", error); return isSimpleValue ? ((NSArray*)decoded)[0] : decoded; } @end diff --git a/engine/src/flutter/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm b/engine/src/flutter/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm index f0298a0c26..c9ab8fdaaa 100644 --- a/engine/src/flutter/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm +++ b/engine/src/flutter/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm @@ -44,11 +44,26 @@ TEST(FlutterStringCodec, CanEncodeAndDecodeNonBMPString) { ASSERT_TRUE([value isEqualTo:decoded]); } +TEST(FlutterJSONCodec, ThrowsOnInvalidEncode) { + NSString* value = [[NSString alloc] initWithBytes:"\xdf\xff" + length:2 + encoding:NSUTF16StringEncoding]; + FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance]; + EXPECT_EXIT([codec encode:value], testing::KilledBySignal(SIGABRT), "failed to convert to UTF8"); +} + TEST(FlutterJSONCodec, CanDecodeZeroLength) { FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance]; ASSERT_TRUE([codec decode:[NSData data]] == nil); } +TEST(FlutterJSONCodec, ThrowsOnInvalidDecode) { + NSString* value = @"{{{"; + FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance]; + EXPECT_EXIT([codec decode:[value dataUsingEncoding:value.fastestEncoding]], + testing::KilledBySignal(SIGABRT), "No string key for value in object around line 1"); +} + TEST(FlutterJSONCodec, CanEncodeAndDecodeNil) { FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance]; ASSERT_TRUE([codec encode:nil] == nil); diff --git a/engine/src/flutter/testing/run_all_unittests.cc b/engine/src/flutter/testing/run_all_unittests.cc index 4213a9c754..48f4c0d5eb 100644 --- a/engine/src/flutter/testing/run_all_unittests.cc +++ b/engine/src/flutter/testing/run_all_unittests.cc @@ -49,6 +49,7 @@ int main(int argc, char** argv) { #endif // FML_OS_IOS ::testing::InitGoogleTest(&argc, argv); + GTEST_FLAG_SET(death_test_style, "threadsafe"); // Check if the user has specified a timeout. const auto timeout = GetTestTimeout();