fix(editor, runtime): guard bindable enums against out-of-range numbers (#14484) 8d951c92f3 * fix(editor): don't crash when a bound number exceeds a layout enum's range A number view model property bound to an enum-backed layout property (e.g. display) can push any value; ContextValueNumber only clamps uints at 0. The layout style getters indexed Enum.values directly and threw a RangeError from Artboard.advance. Read them through enumAt, which falls back to the first value, matching the runtime (display != none is visible). * fix(editor, runtime): guard the remaining bindable enums against out-of-range numbers Editor: nested artboard fit, instance width/height units, draggable direction, text sizing and text wrap still indexed Enum.values directly, so a bound number past the enum's range threw a RangeError. Read them through enumAt like the layout getters. Runtime: a bound layer mask mode reached the GPU paint params unmasked; values >= 8 overwrote the low bits of the clipID, so the masked layer was clipped against the wrong clip. LayerMask::maskMode() now falls back to alpha (as the editor does) and is what the renderer is handed, and PaintData::set masks the mode to its two bits. * fix(editor): match the runtime for out-of-range grid track enums A GridTrack with an unknown collection indexed the per-collection buckets directly and threw; the runtime drops such a track, so the editor now does too (falling back to template columns would lay out a column the runtime doesn't have). maxSizeType reads through enumAt, falling back to auto like the runtime's gridSizeLength. Co-authored-by: hernan <hernan@rive.app>
diff --git a/.rive_head b/.rive_head index c38dead..7c069aa 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -6f7a868171e0112a4c0385487889521bd46f6309 +8d951c92f3c97e3f4b166a54079fe494d299898c
diff --git a/include/rive/layer_mask.hpp b/include/rive/layer_mask.hpp index ab0c83f..e1be107 100644 --- a/include/rive/layer_mask.hpp +++ b/include/rive/layer_mask.hpp
@@ -122,7 +122,14 @@ LayerMask(); ~LayerMask() override; - MaskMode maskMode() const { return static_cast<MaskMode>(maskModeValue()); } + // A number bound to the mode can carry any value; an unknown one falls + // back to alpha, as the editor does. + MaskMode maskMode() const + { + return maskModeValue() <= (uint32_t)MaskMode::invertedLuminance + ? static_cast<MaskMode>(maskModeValue()) + : MaskMode::alpha; + } #ifdef WITH_RIVE_EDITOR // Dual storage, as every other editor-mutable cross-reference has:
diff --git a/renderer/src/gpu.cpp b/renderer/src/gpu.cpp index 25923c0..4c9de96 100644 --- a/renderer/src/gpu.cpp +++ b/renderer/src/gpu.cpp
@@ -1206,8 +1206,10 @@ if (isLayerMask) { localParams |= PAINT_FLAG_LAYER_MASK; - localParams |= static_cast<uint32_t>(layerMaskMode) - << PAINT_LAYER_MASK_MODE_SHIFT; + // Masked so an unknown mode can't spill into the clipID bits above. + localParams |= (static_cast<uint32_t>(layerMaskMode) + << PAINT_LAYER_MASK_MODE_SHIFT) & + PAINT_LAYER_MASK_MODE_MASK; } m_params = localParams; }
diff --git a/src/artboard.cpp b/src/artboard.cpp index 3b8efcc..1b0ae5a 100644 --- a/src/artboard.cpp +++ b/src/artboard.cpp
@@ -3018,7 +3018,7 @@ r->applyLayerMask( coverage.get(), ImageSampler::LinearClamp(), - static_cast<LayerMaskMode>(mask.maskModeValue())); + static_cast<LayerMaskMode>(mask.maskMode())); r->restore(); } }
diff --git a/tests/unit_tests/renderer/gpu_namespace_test.cpp b/tests/unit_tests/renderer/gpu_namespace_test.cpp index a8698f8..25e705d 100644 --- a/tests/unit_tests/renderer/gpu_namespace_test.cpp +++ b/tests/unit_tests/renderer/gpu_namespace_test.cpp
@@ -247,4 +247,36 @@ } } +TEST_CASE("layer_mask_mode_stays_out_of_the_clip_id", "[gpu]") +{ + auto params = [](LayerMaskMode mode) { + gpu::PaintData data; + data.set(gpu::DrawContents::none, + gpu::PaintType::solidColor, + gpu::SimplePaintValue{}, + gpu::GradTextureLayout{}, + /*clipID=*/2, + /*hasClipRect=*/false, + /*hasImage=*/true, + BlendMode::srcOver, + /*solidUnmultiplied=*/false, + /*additiveness=*/0.f, + /*isLayerMask=*/true, + mode); + uint32_t out; + memcpy(&out, &data, sizeof(out)); + return out; + }; + + uint32_t valid = params(LayerMaskMode::invertedLuminance); + CHECK((valid >> 16) == 2); + CHECK(((valid & PAINT_LAYER_MASK_MODE_MASK) >> + PAINT_LAYER_MASK_MODE_SHIFT) == LAYER_MASK_MODE_INVERTED_LUMINANCE); + + // A number bound to the mode can carry any value. 8 used to land on the + // low bit of the clipID. + uint32_t outOfRange = params(static_cast<LayerMaskMode>(8)); + CHECK((outOfRange >> 16) == 2); +} + } // namespace rive
diff --git a/tests/unit_tests/runtime/layer_mask_test.cpp b/tests/unit_tests/runtime/layer_mask_test.cpp index 7f7a09d..cacaef2 100644 --- a/tests/unit_tests/runtime/layer_mask_test.cpp +++ b/tests/unit_tests/runtime/layer_mask_test.cpp
@@ -301,6 +301,16 @@ REQUIRE(renderer.drawPaths == 1); } +TEST_CASE("an unknown mask mode falls back to alpha", "[layer-mask]") +{ + // A number bound to the mode can carry any value. + rive::LayerMask mask; + mask.maskModeValue(8); + CHECK(mask.maskMode() == rive::MaskMode::alpha); + mask.maskModeValue(3); + CHECK(mask.maskMode() == rive::MaskMode::invertedLuminance); +} + TEST_CASE("a mask is not a solo option", "[layer-mask]") { // A LayerMask is metadata of its parent, like a ClippingShape. If it