From f17bc1a8bc83a87c14ede3f210697c9ff9809a46 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Sun, 21 Mar 2021 12:40:37 +0100 Subject: [PATCH] Fix incorrect bound enforcement due to truncation --- src/sfizz/Opcode.cpp | 4 ++-- tests/OpcodeT.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/sfizz/Opcode.cpp b/src/sfizz/Opcode.cpp index fdf66adf..406032dc 100644 --- a/src/sfizz/Opcode.cpp +++ b/src/sfizz/Opcode.cpp @@ -223,12 +223,12 @@ absl::optional readFloat_(OpcodeSpec spec, absl::string_view v) if (spec.flags & kWrapPhase) returnedValue = wrapPhase(returnedValue); - if (returnedValue > static_cast(spec.bounds.getEnd())) { + if (returnedValue > spec.bounds.getEnd()) { if (spec.flags & kEnforceUpperBound) return spec.bounds.getEnd(); else if (!(spec.flags & kPermissiveUpperBound)) return absl::nullopt; - } else if (returnedValue < static_cast(spec.bounds.getStart())) { + } else if (returnedValue < spec.bounds.getStart()) { if (spec.flags & kEnforceLowerBound) return spec.bounds.getStart(); else if (!(spec.flags & kPermissiveLowerBound)) diff --git a/tests/OpcodeT.cpp b/tests/OpcodeT.cpp index abdac3d6..037c41c9 100644 --- a/tests/OpcodeT.cpp +++ b/tests/OpcodeT.cpp @@ -335,6 +335,20 @@ TEST_CASE("[Opcode] opcode read (uint8_t)") REQUIRE( opcode.read(spec) == 20 ); } + SECTION("Clamp upper (real)") + { + Opcode opcode { "", "101" }; + OpcodeSpec spec { 0.0f, Range(0.0f, 100.5f), kEnforceUpperBound }; + REQUIRE( opcode.read(spec) == 100.5f ); + } + + SECTION("Clamp lower (real)") + { + Opcode opcode { "", "19" }; + OpcodeSpec spec { 0.0f, Range(19.5f, 100.0f), kEnforceLowerBound }; + REQUIRE( opcode.read(spec) == 19.5f ); + } + SECTION("Floating point") { Opcode opcode { "", "10.5" };