From 1e4a060d9e37ee2058cbfa7a25449ce6c196fa65 Mon Sep 17 00:00:00 2001 From: Rangi Date: Mon, 6 Jul 2026 22:38:06 -0400 Subject: [PATCH] Guard against undefined behavior in `double2fix` I haven't found input which triggers any, but this is more technically correct just in case. --- src/asm/fixpoint.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/asm/fixpoint.cpp b/src/asm/fixpoint.cpp index d8097bac..eb261f21 100644 --- a/src/asm/fixpoint.cpp +++ b/src/asm/fixpoint.cpp @@ -8,6 +8,8 @@ #include #include +#include "helpers.hpp" // assume + static constexpr double tau = std::numbers::pi * 2; static double fix2double(int32_t i, int32_t q) { @@ -21,7 +23,8 @@ static int32_t double2fix(double d, int32_t q) { if (isinf(d)) { return d < 0 ? INT32_MIN : INT32_MAX; } - return static_cast(round(d * pow(2.0, q))); + double v = round(d * pow(2.0, q)); + return v < INT32_MIN ? INT32_MIN : v > INT32_MAX ? INT32_MAX : static_cast(v); } static double turn2rad(double t) {