From 93f9caca1f3de30ba7cbc53a7005dede1b94b895 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Wed, 10 Aug 2022 11:40:11 -0700 Subject: [PATCH] [Windows] Replace memset with C++ alternatives (flutter/engine#35312) Eliminate a few uses of memset in the code, replacing them with C++ alternatives, like using initialiser list syntax and std::array + fill. This avoids some double-hardcoding of array length in the code. --- .../shell/platform/windows/flutter_window.cc | 3 +-- .../windows/flutter_windows_unittests.cc | 4 +-- .../platform/windows/window_unittests.cc | 27 ++++++++++--------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/flutter_window.cc b/engine/src/flutter/shell/platform/windows/flutter_window.cc index a4c66be29d..c25f433d93 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window.cc @@ -250,8 +250,7 @@ bool FlutterWindow::OnBitmapSurfaceUpdated(const void* allocation, size_t row_bytes, size_t height) { HDC dc = ::GetDC(GetWindowHandle()); - BITMAPINFO bmi; - memset(&bmi, 0, sizeof(bmi)); + BITMAPINFO bmi = {}; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = row_bytes / 4; bmi.bmiHeader.biHeight = -height; diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc index 00520426c1..ff3d00d0e6 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc @@ -4,7 +4,6 @@ #include "flutter/shell/platform/windows/public/flutter_windows.h" -#include #include #include "flutter/shell/platform/windows/testing/windows_test.h" @@ -16,8 +15,7 @@ namespace flutter { namespace testing { TEST(WindowsNoFixtureTest, GetTextureRegistrar) { - FlutterDesktopEngineProperties properties; - memset(&properties, 0, sizeof(FlutterDesktopEngineProperties)); + FlutterDesktopEngineProperties properties = {}; properties.assets_path = L""; properties.icu_data_path = L"icudtl.dat"; auto engine = FlutterDesktopEngineCreate(&properties); diff --git a/engine/src/flutter/shell/platform/windows/window_unittests.cc b/engine/src/flutter/shell/platform/windows/window_unittests.cc index 182dc0918d..22750e2755 100644 --- a/engine/src/flutter/shell/platform/windows/window_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/window_unittests.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include + #include "flutter/shell/platform/windows/testing/mock_text_input_manager.h" #include "flutter/shell/platform/windows/testing/mock_window.h" #include "gtest/gtest.h" @@ -207,19 +209,19 @@ TEST(MockWindow, KeyDownPrintable) { .Times(1) .WillOnce(respond_false); EXPECT_CALL(window, OnText(_)).Times(1); - Win32Message messages[] = {{WM_KEYDOWN, 65, lparam, kWmResultDontCheck}, - {WM_CHAR, 65, lparam, kWmResultDontCheck}}; - window.InjectMessageList(2, messages); + std::array messages = { + Win32Message{WM_KEYDOWN, 65, lparam, kWmResultDontCheck}, + Win32Message{WM_CHAR, 65, lparam, kWmResultDontCheck}}; + window.InjectMessageList(2, messages.data()); } TEST(MockWindow, KeyDownWithCtrl) { MockWindow window; // Simulate CONTROL pressed - BYTE keyboard_state[256]; - memset(keyboard_state, 0, 256); + std::array keyboard_state; keyboard_state[VK_CONTROL] = -1; - SetKeyboardState(keyboard_state); + SetKeyboardState(keyboard_state.data()); LPARAM lparam = CreateKeyEventLparam(30, false, false); @@ -230,8 +232,8 @@ TEST(MockWindow, KeyDownWithCtrl) { window.InjectWindowMessage(WM_KEYDOWN, 65, lparam); - memset(keyboard_state, 0, 256); - SetKeyboardState(keyboard_state); + keyboard_state.fill(0); + SetKeyboardState(keyboard_state.data()); } TEST(MockWindow, KeyDownWithCtrlToggled) { @@ -244,10 +246,9 @@ TEST(MockWindow, KeyDownWithCtrlToggled) { }; // Simulate CONTROL toggled - BYTE keyboard_state[256]; - memset(keyboard_state, 0, 256); + std::array keyboard_state; keyboard_state[VK_CONTROL] = 1; - SetKeyboardState(keyboard_state); + SetKeyboardState(keyboard_state.data()); LPARAM lparam = CreateKeyEventLparam(30, false, false); @@ -261,8 +262,8 @@ TEST(MockWindow, KeyDownWithCtrlToggled) { {WM_CHAR, 65, lparam, kWmResultDontCheck}}; window.InjectMessageList(2, messages); - memset(keyboard_state, 0, 256); - SetKeyboardState(keyboard_state); + keyboard_state.fill(0); + SetKeyboardState(keyboard_state.data()); } TEST(MockWindow, Paint) {