@@ -243,6 +243,12 @@ class JsiSkCanvas : public JsiSkHostObject {
243
243
244
244
auto jsiPoints = arguments[1 ].asObject (runtime).asArray (runtime);
245
245
auto pointsSize = jsiPoints.size (runtime);
246
+
247
+ // Check if we have at least one point
248
+ if (pointsSize == 0 ) {
249
+ throw std::invalid_argument (" Points array must not be empty" );
250
+ }
251
+
246
252
points.reserve (pointsSize);
247
253
248
254
for (int i = 0 ; i < pointsSize; i++) {
@@ -274,6 +280,12 @@ class JsiSkCanvas : public JsiSkHostObject {
274
280
275
281
auto jsiCubics = arguments[0 ].asObject (runtime).asArray (runtime);
276
282
auto cubicsSize = jsiCubics.size (runtime);
283
+
284
+ // Validate cubic points - must be exactly 12 points
285
+ if (cubicsSize != 12 ) {
286
+ throw std::invalid_argument (" Cubic points array must contain exactly 12 points" );
287
+ }
288
+
277
289
cubics.reserve (cubicsSize);
278
290
for (int i = 0 ; i < cubicsSize; i++) {
279
291
std::shared_ptr<SkPoint> point = JsiSkPoint::fromValue (
@@ -284,6 +296,12 @@ class JsiSkCanvas : public JsiSkHostObject {
284
296
if (count >= 2 && !arguments[1 ].isNull () && !arguments[1 ].isUndefined ()) {
285
297
auto jsiColors = arguments[1 ].asObject (runtime).asArray (runtime);
286
298
auto colorsSize = jsiColors.size (runtime);
299
+
300
+ // Validate colors array - must be exactly 4 colors
301
+ if (colorsSize != 4 ) {
302
+ throw std::invalid_argument (" Colors array must contain exactly 4 colors" );
303
+ }
304
+
287
305
colors.reserve (colorsSize);
288
306
for (int i = 0 ; i < colorsSize; i++) {
289
307
SkColor color = JsiSkColor::fromValue (
@@ -295,6 +313,12 @@ class JsiSkCanvas : public JsiSkHostObject {
295
313
if (count >= 3 && !arguments[2 ].isNull () && !arguments[2 ].isUndefined ()) {
296
314
auto jsiTexs = arguments[2 ].asObject (runtime).asArray (runtime);
297
315
auto texsSize = jsiTexs.size (runtime);
316
+
317
+ // Validate textures array - must be exactly 4 points
318
+ if (texsSize != 4 ) {
319
+ throw std::invalid_argument (" Texture coordinates array must contain exactly 4 points" );
320
+ }
321
+
298
322
texs.reserve (texsSize);
299
323
for (int i = 0 ; i < texsSize; i++) {
300
324
auto point = JsiSkPoint::fromValue (
@@ -306,7 +330,8 @@ class JsiSkCanvas : public JsiSkHostObject {
306
330
auto paint =
307
331
count >= 4 ? JsiSkPaint::fromValue (runtime, arguments[4 ]) : nullptr ;
308
332
auto blendMode = static_cast <SkBlendMode>(arguments[3 ].asNumber ());
309
- _canvas->drawPatch (cubics.data (), colors.data (), texs.data (), blendMode,
333
+ _canvas->drawPatch (cubics.data (), colors.empty () ? nullptr : colors.data (),
334
+ texs.empty () ? nullptr : texs.data (), blendMode,
310
335
*paint);
311
336
return jsi::Value::undefined ();
312
337
}
@@ -364,6 +389,12 @@ class JsiSkCanvas : public JsiSkHostObject {
364
389
365
390
std::vector<SkGlyphID> glyphs;
366
391
int glyphsSize = static_cast <int >(jsiGlyphs.size (runtime));
392
+
393
+ // Validate that glyphs and positions arrays have the same size
394
+ if (glyphsSize != pointsSize) {
395
+ throw std::invalid_argument (" Glyphs and positions arrays must have the same length" );
396
+ }
397
+
367
398
glyphs.reserve (glyphsSize);
368
399
for (int i = 0 ; i < glyphsSize; i++) {
369
400
glyphs.push_back (jsiGlyphs.getValueAtIndex (runtime, i).asNumber ());
@@ -522,11 +553,22 @@ class JsiSkCanvas : public JsiSkHostObject {
522
553
runtime, rects.getValueAtIndex (runtime, i).asObject (runtime));
523
554
skRects.push_back (*rect.get ());
524
555
}
556
+
557
+ // Validate transforms and rects have the same size
558
+ if (xformsSize != rectsSize) {
559
+ throw std::invalid_argument (" Transforms and rects arrays must have the same length" );
560
+ }
525
561
526
562
std::vector<SkColor> colors;
527
563
if (count > 5 && !arguments[5 ].isUndefined ()) {
528
564
auto colorsArray = arguments[5 ].asObject (runtime).asArray (runtime);
529
565
int colorsSize = static_cast <int >(colorsArray.size (runtime));
566
+
567
+ // Validate colors array matches the size of sprites and transforms
568
+ if (colorsSize != rectsSize) {
569
+ throw std::invalid_argument (" Colors array must have the same length as rects/transforms" );
570
+ }
571
+
530
572
colors.reserve (colorsSize);
531
573
for (int i = 0 ; i < colorsSize; i++) {
532
574
// Convert from [r,g,b,a] in [0,1] to SkColor
@@ -551,7 +593,8 @@ class JsiSkCanvas : public JsiSkHostObject {
551
593
sampling = SamplingOptionsFromValue (runtime, arguments[6 ]);
552
594
}
553
595
_canvas->drawAtlas (atlas.get (), xforms.data (), skRects.data (),
554
- colors.data (), skRects.size (), blendMode, sampling,
596
+ colors.empty () ? nullptr : colors.data (),
597
+ skRects.size (), blendMode, sampling,
555
598
nullptr , paint.get ());
556
599
557
600
return jsi::Value::undefined ();
0 commit comments