Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ad36c9e

Browse files
committed
opt: add cursor validity check
1 parent 7ced8d9 commit ad36c9e

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

shell/platform/windows/cursor_handler.cc

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ static constexpr char kActivateSystemCursorMethod[] = "activateSystemCursor";
1414
static constexpr char kSetSystemCursorMethod[] = "setSystemCursor";
1515

1616
static constexpr char kKindKey[] = "kind";
17+
static constexpr char kCustomCursorBufferKey[] = "buffer";
18+
static constexpr char kCustomCursorHotxKey[] = "hotx";
19+
static constexpr char kCustomCursorHotyKey[] = "hoty";
20+
static constexpr char kCustomCursorWidthKey[] = "width";
21+
static constexpr char kCustomCursorHeightKey[] = "height";
1722

1823
namespace flutter {
1924

@@ -47,18 +52,48 @@ void CursorHandler::HandleMethodCall(
4752
delegate_->UpdateFlutterCursor(kind);
4853
result->Success();
4954
} else if (method.compare(kSetSystemCursorMethod) == 0) {
50-
const auto& map = std::get<EncodableMap>(*method_call.arguments());
55+
const auto& arguments = std::get<EncodableMap>(*method_call.arguments());
56+
auto buffer_iter = arguments.find(EncodableValue(std::string(kCustomCursorBufferKey)));
57+
if (buffer_iter == arguments.end()) {
58+
result->Error("Argument error",
59+
"Missing argument buffer while trying to customize system cursor");
60+
return;
61+
}
5162
auto buffer = std::get<std::vector<uint8_t>>(
52-
map.at(flutter::EncodableValue("buffer")));
53-
auto width = std::get<int>(map.at(flutter::EncodableValue("width")));
54-
auto height = std::get<int>(map.at(flutter::EncodableValue("height")));
55-
auto hotx = std::get<double>(map.at(flutter::EncodableValue("hotx")));
56-
auto hoty = std::get<double>(map.at(flutter::EncodableValue("hoty")));
63+
arguments.at(flutter::EncodableValue("buffer")));
64+
auto width_iter = arguments.find(EncodableValue(std::string(kCustomCursorWidthKey)));
65+
if (width_iter == arguments.end()) {
66+
result->Error("Argument error",
67+
"Missing argument width while trying to customize system cursor");
68+
return;
69+
}
70+
auto width = std::get<int>(arguments.at(flutter::EncodableValue("width")));
71+
auto height_iter = arguments.find(EncodableValue(std::string(kCustomCursorHeightKey)));
72+
if (height_iter == arguments.end()) {
73+
result->Error("Argument error",
74+
"Missing argument height while trying to customize system cursor");
75+
return;
76+
}
77+
auto height = std::get<int>(arguments.at(flutter::EncodableValue("height")));
78+
auto hotx_iter = arguments.find(EncodableValue(std::string(kCustomCursorHotxKey)));
79+
if (hotx_iter == arguments.end()) {
80+
result->Error("Argument error",
81+
"Missing argument hotx while trying to customize system cursor");
82+
return;
83+
}
84+
auto hotx = std::get<double>(arguments.at(flutter::EncodableValue("hotx")));
85+
auto hoty_iter = arguments.find(EncodableValue(std::string(kCustomCursorHotyKey)));
86+
if (hoty_iter == arguments.end()) {
87+
result->Error("Argument error",
88+
"Missing argument hoty while trying to customize system cursor");
89+
return;
90+
}
91+
auto hoty = std::get<double>(arguments.at(flutter::EncodableValue("hoty")));
5792
HCURSOR cursor = nullptr;
5893
// Flutter returns rawRgba, which has 8bits*4channels
5994
auto bitmap = CreateBitmap(width, height, 1, 32, &buffer[0]);
6095
if (bitmap == nullptr) {
61-
result->Error("Argument error", "Invalid rawRgba bitmap from flutter");
96+
result->Error("Argument error", "Argument buffer must contain valid rawRgba bitmap");
6297
return;
6398
}
6499
ICONINFO icon_info;
@@ -69,6 +104,10 @@ void CursorHandler::HandleMethodCall(
69104
icon_info.hbmColor = bitmap;
70105
cursor = CreateIconIndirect(&icon_info);
71106
DeleteObject(bitmap);
107+
if (cursor == nullptr) {
108+
result->Error("Argument error", "Create Icon failed, argument buffer must contain valid rawRgba bitmap");
109+
return;
110+
}
72111
delegate_->SetFlutterCursor(cursor);
73112
result->Success();
74113
} else {

0 commit comments

Comments
 (0)