From c9fb28402a5c8b9482f7a0f9d676dccf45624a2c Mon Sep 17 00:00:00 2001 From: Jinny You Date: Fri, 25 Jul 2025 01:03:24 +0900 Subject: [PATCH] sw_engine: prevent NaN in gradient pixel calculations Add std::isfinite() check in _pixel() to prevent undefined behavior when NaN values are cast to int32_t. This prevents potential crashes during gradient color table indexing. NaN can occur from: - Division by zero in radial gradient calculations - Negative values passed to sqrtf() in radial gradient determinant --- src/renderer/sw_engine/tvgSwFill.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/renderer/sw_engine/tvgSwFill.cpp b/src/renderer/sw_engine/tvgSwFill.cpp index 9978baf9..8715ca13 100644 --- a/src/renderer/sw_engine/tvgSwFill.cpp +++ b/src/renderer/sw_engine/tvgSwFill.cpp @@ -308,6 +308,7 @@ static inline uint32_t _fixedPixel(const SwFill* fill, int32_t pos) static inline uint32_t _pixel(const SwFill* fill, float pos) { + if (!std::isfinite(pos)) pos = 0; auto i = static_cast(pos * (GRADIENT_STOP_SIZE - 1) + 0.5f); return fill->ctable[_clamp(fill, i)]; }