Skip to content

Commit 67432c2

Browse files
kewdewcandillon
andauthored
fix: harden vertices (#3065)
Co-authored-by: William Candillon <[email protected]>
1 parent d3a1cd0 commit 67432c2

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

packages/skia/cpp/api/JsiSkVertices.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class JsiSkVertices : public JsiSkWrappingSkPtrHostObject<SkVertices> {
7070
if (count >= 3 && !arguments[2].isNull() && !arguments[2].isUndefined()) {
7171
auto jsiTexs = arguments[2].asObject(runtime).asArray(runtime);
7272
auto texsSize = jsiTexs.size(runtime);
73+
if (texsSize != positionsSize) {
74+
throw jsi::JSError(runtime, "The number of texture coordinates must "
75+
"match the number of positions");
76+
}
7377
texs.reserve(texsSize);
7478
for (int i = 0; i < texsSize; i++) {
7579
auto point = JsiSkPoint::fromValue(
@@ -81,6 +85,11 @@ class JsiSkVertices : public JsiSkWrappingSkPtrHostObject<SkVertices> {
8185
if (count >= 4 && !arguments[3].isNull() && !arguments[3].isUndefined()) {
8286
auto jsiColors = arguments[3].asObject(runtime).asArray(runtime);
8387
auto colorsSize = jsiColors.size(runtime);
88+
if (colorsSize != positionsSize) {
89+
throw jsi::JSError(
90+
runtime,
91+
"The number of colors must match the number of positions");
92+
}
8493
colors.reserve(colorsSize);
8594
for (int i = 0; i < colorsSize; i++) {
8695
SkColor color = JsiSkColor::fromValue(
@@ -132,9 +141,11 @@ class JsiSkVertices : public JsiSkWrappingSkPtrHostObject<SkVertices> {
132141
// builder.indices());
133142
// }
134143
// auto vertices = builder.detach();
135-
auto vertices = SkVertices::MakeCopy(
136-
mode, positionsSize, positions.data(), texs.data(), colors.data(),
137-
indicesSize, indices.data());
144+
auto vertices =
145+
SkVertices::MakeCopy(mode, positionsSize, positions.data(),
146+
texs.size() > 0 ? texs.data() : nullptr,
147+
colors.size() > 0 ? colors.data() : nullptr,
148+
indicesSize, indices.data());
138149
return jsi::Object::createFromHostObject(
139150
runtime,
140151
std::make_shared<JsiSkVertices>(context, std::move(vertices)));

0 commit comments

Comments
 (0)