Skip to content

Commit

Permalink
Merge pull request #123 from m-carrasco/fix-gradient-ub
Browse files Browse the repository at this point in the history
Fix undefined behavior caused by a domain error on an sqrt call.
  • Loading branch information
sammycage authored Mar 16, 2023
2 parents 585d61e + 4d51541 commit 4c16abf
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion 3rdparty/plutovg/plutovg-blend.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <limits.h>
#include <math.h>
#include <float.h>

#define COLOR_TABLE_SIZE 1024
typedef struct {
Expand Down Expand Up @@ -243,6 +244,7 @@ static void fetch_radial_gradient(uint32_t* buffer, const radial_gradient_values
while(buffer < end)
{
uint32_t result = 0;
det = fabs(det) < DBL_EPSILON ? 0.0 : det;
if(det >= 0)
{
double w = sqrt(det) - b;
Expand All @@ -261,7 +263,11 @@ static void fetch_radial_gradient(uint32_t* buffer, const radial_gradient_values
{
while(buffer < end)
{
*buffer++ = gradient_pixel(gradient, sqrt(det) - b);
det = fabs(det) < DBL_EPSILON ? 0.0 : det;
uint32_t result = 0;
if (det >= 0)
result = gradient_pixel(gradient, sqrt(det) - b);
*buffer++ = result;
det += delta_det;
delta_det += delta_delta_det;
b += delta_b;
Expand Down

0 comments on commit 4c16abf

Please sign in to comment.