From 35d522a4218f0696b93b2b5f29a2d51c8e121a1a Mon Sep 17 00:00:00 2001 From: mch <32224+hamsolodev@users.noreply.github.com> Date: Sat, 21 Nov 2026 09:44:54 +1100 Subject: [PATCH] host: min, max and constrain return a value, not a reference Declared -> decltype(a < b ? a : b), a call whose two arguments have one type returned a reference to the function's own by-value parameter, gone once it returns. Optimised, the companion's CMD_SET_AUTOADD_CONFIG stored 0 as the hop limit, whatever was asked. std::decay_t keeps the type the Arduino macro's expression has, without the reference. arduino_test.cpp pins it; it fails six checks without this change. --- variants/host/HostArduino.h | 14 +++++++--- variants/host/arduino_test.cpp | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 variants/host/arduino_test.cpp diff --git a/variants/host/HostArduino.h b/variants/host/HostArduino.h index 455a4eb..cbdd738 100644 --- a/variants/host/HostArduino.h +++ b/variants/host/HostArduino.h @@ -27,6 +27,8 @@ #include #include +#include + // Seeded rather than entropic: a run has to be reproducible from its seed, and // the identity a node generates comes through here. // @@ -59,16 +61,22 @@ inline long hostRandom() { // standard library breaks , whose numeric_limits::min() then looks // like a macro call with one argument. That produced 368 errors inside libstdc++ // and none in any MeshCore file, which is a confusing place to start debugging. +// +// Each returns a value, never a reference: when both arguments have one type, +// `decltype(a < b ? a : b)` is a reference to the function's own by-value +// parameter, gone once it returns. std::decay_t keeps the type a macro's +// expression would have, without the reference. template -constexpr auto min(T a, U b) -> decltype(a < b ? a : b) { +constexpr auto min(T a, U b) -> std::decay_t { return a < b ? a : b; } template -constexpr auto max(T a, U b) -> decltype(a > b ? a : b) { +constexpr auto max(T a, U b) -> std::decay_t b ? a : b)> { return a > b ? a : b; } template -constexpr auto constrain(T v, L lo, H hi) -> decltype(v < lo ? lo : (v > hi ? hi : v)) { +constexpr auto constrain(T v, L lo, H hi) + -> std::decay_t hi ? hi : v))> { return v < lo ? lo : (v > hi ? hi : v); } diff --git a/variants/host/arduino_test.cpp b/variants/host/arduino_test.cpp new file mode 100644 index 0000000..4dc7d6e --- /dev/null +++ b/variants/host/arduino_test.cpp @@ -0,0 +1,47 @@ +// min, max and constrain must return a value, as Arduino's macros give one. +// +// They are templates here, not macros (see HostArduino.h). Declared with +// `-> decltype(a < b ? a : b)`, a call whose two arguments have one type +// returned a reference to the function's own by-value parameter, which is gone +// once it returns. Optimised, `min(frame[2], (uint8_t)64)` in the companion's +// CMD_SET_AUTOADD_CONFIG read whatever was left on the stack and stored 0 as +// the hop limit. +// +// c++ -std=c++17 -O2 -I variants/host variants/host/arduino_test.cpp -o /tmp/arduino_test +// /tmp/arduino_test + +#include "HostArduino.h" + +#include +#include + +static int failures = 0; + +static void expect(bool ok, const char* what) { + if (!ok) { + printf("FAIL %s\n", what); + failures++; + } +} + +// Inputs the optimiser can't fold, as a frame's bytes are. +volatile uint8_t frame[3] = {0x3a, 0x0e, 0x07}; +volatile int lo = 2, hi = 9, v = 12; + +int main() { + uint8_t a = frame[2], b = 64; + expect(!std::is_reference::value, "min of one type returns a value"); + expect(!std::is_reference::value, "max of one type returns a value"); + expect(!std::is_reference::value, "constrain of one type returns a value"); + + uint8_t hops = min(frame[2], (uint8_t)64); // as the companion's CMD_SET_AUTOADD_CONFIG calls it + expect(hops == 7, "min(uint8_t, uint8_t) gives the smaller"); + expect(max(frame[1], frame[2]) == 0x0e, "max(uint8_t, uint8_t) gives the larger"); + int vv = v, l = lo, h = hi; + expect(constrain(vv, l, h) == 9, "constrain(int, int, int) clamps"); + expect(min(frame[2], 64) == 7, "min of mixed types still works"); + + if (failures) return 1; + printf("ok\n"); + return 0; +}