From 7f1671260906663084ee65ac3bc5f85a2febcc5d Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Tue, 8 Feb 2022 13:07:45 -0800 Subject: [PATCH] Fix insecureAPI, NonLocalizedStringChecker, NewDeleteLeaks macOS clang errors --- fml/synchronization/waitable_event_unittest.cc | 6 +++--- .../framework/Source/AccessibilityBridgeMacDelegateTest.mm | 2 +- .../macos/framework/Source/FlutterEmbedderKeyResponder.mm | 2 +- .../Source/FlutterEmbedderKeyResponderUnittests.mm | 2 +- .../framework/Source/FlutterPlatformNodeDelegateMacTest.mm | 7 ++++++- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/fml/synchronization/waitable_event_unittest.cc b/fml/synchronization/waitable_event_unittest.cc index dde70ad598fe0..512c33599b492 100644 --- a/fml/synchronization/waitable_event_unittest.cc +++ b/fml/synchronization/waitable_event_unittest.cc @@ -31,7 +31,7 @@ void SleepFor(TimeDelta duration) { void EpsilonRandomSleep() { TimeDelta duration = - TimeDelta::FromMilliseconds(static_cast(rand()) % 20u); + TimeDelta::FromMilliseconds(static_cast(arc4random()) % 20u); SleepFor(duration); } @@ -73,7 +73,7 @@ TEST(AutoResetWaitableEventTest, MultipleWaiters) { std::vector threads; for (size_t j = 0u; j < 4u; j++) { threads.push_back(std::thread([&ev, &wake_count]() { - if (rand() % 2 == 0) { + if (arc4random() % 2 == 0) { ev.Wait(); } else { EXPECT_FALSE(ev.WaitWithTimeout(kActionTimeout)); @@ -158,7 +158,7 @@ TEST(ManualResetWaitableEventTest, SignalMultiple) { threads.push_back(std::thread([&ev]() { EpsilonRandomSleep(); - if (rand() % 2 == 0) { + if (arc4random() % 2 == 0) { ev.Wait(); } else { EXPECT_FALSE(ev.WaitWithTimeout(kActionTimeout)); diff --git a/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegateTest.mm b/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegateTest.mm index da0168be7915b..5f4a6d6a8d125 100644 --- a/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegateTest.mm +++ b/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegateTest.mm @@ -199,4 +199,4 @@ void DispatchMacOSNotification(gfx::NativeViewAccessible native_node, [engine shutDownEngine]; } -} // flutter::testing +} // namespace flutter::testing diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.mm b/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.mm index 1a0747b3b59be..cf3e397a84612 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.mm @@ -166,12 +166,12 @@ static uint64_t GetLogicalKeyForEvent(NSEvent* event, uint64_t physicalKey) { uint32_t* keyLabel = DecodeUtf16(keyLabelUtf16, &keyLabelLength); if (keyLabelLength == 1) { uint32_t keyLabelChar = *keyLabel; - delete[] keyLabel; NSCAssert(!IsControlCharacter(keyLabelChar) && !IsUnprintableKey(keyLabelChar), @"Unexpected control or unprintable keylabel 0x%x", keyLabelChar); NSCAssert(keyLabelChar <= 0x10FFFF, @"Out of range keylabel 0x%x", keyLabelChar); character = keyLabelChar; } + delete[] keyLabel; } if (character != 0) { return KeyOfPlane(toLower(character), kUnicodePlane); diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponderUnittests.mm b/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponderUnittests.mm index f61f1c99308b2..359bdc37e8012 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponderUnittests.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponderUnittests.mm @@ -31,7 +31,7 @@ - (instancetype)initWithEvent:(const FlutterKeyEvent*)event if (event->character != nullptr) { size_t len = strlen(event->character); char* character = new char[len + 1]; - strcpy(character, event->character); + strlcpy(character, event->character, sizeof(character)); _data->character = character; } _callback = callback; diff --git a/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMacTest.mm b/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMacTest.mm index c3ac3fcba397e..4486e250e0426 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMacTest.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMacTest.mm @@ -220,7 +220,12 @@ FlutterViewController* viewController = [[FlutterViewController alloc] initWithProject:project]; [viewController loadView]; [engine setViewController:viewController]; + + // Unit test localization is unnecessary. + // NOLINTBEGIN(clang-analyzer-optin.osx.cocoa.localizability.NonLocalizedStringChecker) viewController.textInputPlugin.string = @"textfield"; + // NOLINTEND(clang-analyzer-optin.osx.cocoa.localizability.NonLocalizedStringChecker) + // Creates a NSWindow so that the native text field can become first responder. NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600) styleMask:NSBorderlessWindowMask @@ -291,4 +296,4 @@ EXPECT_EQ([native_text_field.stringValue isEqualToString:@"textfield"], YES); } -} // flutter::testing +} // namespace flutter::testing