[Windows] Replace memset with C++ alternatives (#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.
diff --git a/shell/platform/windows/flutter_window.cc b/shell/platform/windows/flutter_window.cc
index a4c66be..c25f433 100644
--- a/shell/platform/windows/flutter_window.cc
+++ b/shell/platform/windows/flutter_window.cc
@@ -250,8 +250,7 @@
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/shell/platform/windows/flutter_windows_unittests.cc b/shell/platform/windows/flutter_windows_unittests.cc
index 0052042..ff3d00d 100644
--- a/shell/platform/windows/flutter_windows_unittests.cc
+++ b/shell/platform/windows/flutter_windows_unittests.cc
@@ -4,7 +4,6 @@
#include "flutter/shell/platform/windows/public/flutter_windows.h"
-#include <cstring>
#include <thread>
#include "flutter/shell/platform/windows/testing/windows_test.h"
@@ -16,8 +15,7 @@
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/shell/platform/windows/window_unittests.cc b/shell/platform/windows/window_unittests.cc
index 182dc09..22750e2 100644
--- a/shell/platform/windows/window_unittests.cc
+++ b/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 <array>
+
#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 @@
.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<Win32Message, 2> 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<BYTE, 256> keyboard_state;
keyboard_state[VK_CONTROL] = -1;
- SetKeyboardState(keyboard_state);
+ SetKeyboardState(keyboard_state.data());
LPARAM lparam = CreateKeyEventLparam(30, false, false);
@@ -230,8 +232,8 @@
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 @@
};
// Simulate CONTROL toggled
- BYTE keyboard_state[256];
- memset(keyboard_state, 0, 256);
+ std::array<BYTE, 256> keyboard_state;
keyboard_state[VK_CONTROL] = 1;
- SetKeyboardState(keyboard_state);
+ SetKeyboardState(keyboard_state.data());
LPARAM lparam = CreateKeyEventLparam(30, false, false);
@@ -261,8 +262,8 @@
{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) {