diff --git a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.cc b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.cc index 38b9127f41f..ef872f249bd 100644 --- a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.cc +++ b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.cc @@ -221,7 +221,7 @@ void FlutterStandardCodecHelperWriteUTF8(CFMutableDataRef data, // UTF16 length times 3 will fit all UTF8. CFIndex buffer_length = length * 3; std::vector buffer; - buffer.reserve(buffer_length); + buffer.resize(buffer_length); CFStringGetBytes(value, CFRangeMake(0, length), kCFStringEncodingUTF8, 0, false, buffer.data(), buffer_length, &used_length); FlutterStandardCodecHelperWriteSize(data, used_length); diff --git a/engine/src/flutter/shell/platform/darwin/common/framework/Source/flutter_standard_codec_unittest.mm b/engine/src/flutter/shell/platform/darwin/common/framework/Source/flutter_standard_codec_unittest.mm index f4ce192e800..3489cec7c00 100644 --- a/engine/src/flutter/shell/platform/darwin/common/framework/Source/flutter_standard_codec_unittest.mm +++ b/engine/src/flutter/shell/platform/darwin/common/framework/Source/flutter_standard_codec_unittest.mm @@ -4,6 +4,8 @@ #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h" +#include + #include "gtest/gtest.h" FLUTTER_ASSERT_ARC @@ -215,6 +217,19 @@ TEST(FlutterStandardCodec, CanEncodeAndDecodeStringWithNonBMPCodePoint) { CheckEncodeDecode(@"h\U0001F602w", [NSData dataWithBytes:bytes length:8]); } +TEST(FlutterStandardCodec, CanEncodeAndDecodeIndirectString) { + // This test ensures that an indirect NSString, whose internal string buffer + // can't be simply returned by `CFStringGetCStringPtr`, can be encoded without + // violating the memory sanitizer. This test only works with `--asan` flag. + // See https://github.com/flutter/flutter/issues/142101 + uint8_t bytes[7] = {0x07, 0x05, 0x68, 0xe2, 0x98, 0xba, 0x77}; + NSString* target = @"h\u263Aw"; + // Ensures that this is an indirect string so that this test makes sense. + ASSERT_TRUE(CFStringGetCStringPtr((__bridge CFStringRef)target, kCFStringEncodingUTF8) == + nullptr); + CheckEncodeDecode(target, [NSData dataWithBytes:bytes length:7]); +} + TEST(FlutterStandardCodec, CanEncodeAndDecodeArray) { NSArray* value = @[ [NSNull null], @"hello", @3.14, @47, @{@42 : @"nested"} ]; CheckEncodeDecode(value);