From 41afa7d6d84b193a60da20a819bcc54d144e496b Mon Sep 17 00:00:00 2001 From: James Spencer Date: Mon, 12 Aug 2024 11:31:15 +0100 Subject: [PATCH] Avoid division-by-zero in Sketch::Sketch --- src/path_converters.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/path_converters.h b/src/path_converters.h index db732e126c3f..6d242e74415b 100644 --- a/src/path_converters.h +++ b/src/path_converters.h @@ -5,6 +5,7 @@ #include #include +#include #include "agg_clip_liang_barsky.h" #include "mplutils.h" @@ -1019,8 +1020,18 @@ class Sketch { rewind(0); const double d_M_PI = 3.14159265358979323846; - m_p_scale = (2.0 * d_M_PI) / (m_length * m_randomness); - m_log_randomness = 2.0 * log(m_randomness); + // Set derived values to zero if m_length or m_randomness are zero to + // avoid divide-by-zero errors when a sketch is created but not used. + if (m_length <= std::numeric_limits::epsilon() || m_randomness <= std::numeric_limits::epsilon()) { + m_p_scale = 0.0; + } else { + m_p_scale = (2.0 * d_M_PI) / (m_length * m_randomness); + } + if (m_randomness <= std::numeric_limits::epsilon()) { + m_log_randomness = 0.0; + } else { + m_log_randomness = 2.0 * log(m_randomness); + } } unsigned vertex(double *x, double *y)