Skip to content

host: min, max and constrain return a value, not a reference - #37

Merged
A13xB0 merged 1 commit into
MeshBench:mainfrom
hamsolodev:host-min-by-value
Nov 21, 2026
Merged

A13xB0 merged 1 commit into
MeshBench:mainfrom
hamsolodev:host-min-by-value

Conversation

@hamsolodev

@hamsolodev hamsolodev commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What this changes, and why

min, max and constrain in variants/host/HostArduino.h were declared -> decltype(a < b ? a : b). When both arguments have one type, that is a reference to the function's own by-value parameter, gone once it returns. They now return std::decay_t of the same expression: the type Arduino's macro would give, without the reference. Calls whose arguments have different types were never affected.

From outside, the symptom: on a native companion, CMD_SET_AUTOADD_CONFIG with a hop limit (3a 0e 07) read back as 19 0e 00. The hop limit stored was 0, whatever was asked (min(cmd_frame[2], (uint8_t)64), MyMesh.cpp:2026).

Verification

  • variants/host/arduino_test.cpp, new, run as its header says. Without the change it prints six FAILs: three return types that are references, and the wrong value from min and max of two uint8_ts and from constrain of three ints. It exits 1 at -O2. With it, it prints ok at -O0 and -O2.

  • companion_radio and simple_repeater were built at this commit and at main 9097ca8: same image, SOURCE_DATE_EPOCH pinned, MeshCore companion-v1.18.0.

    • The skipped-source lists are identical, every compile log is identical, and so are the object sets: 97 and 96.
    • Sorted nm -S output differs in one compiler-numbered switch table in the companion (CSWTCH.377 against .371), and nowhere in the repeater.
    • Only four objects' bytes changed: the companion's MyMesh.o, and CommonCLI.o, CommonRadioPrefs.o and RadioLibWrappers.o. These are exactly the files with one-type calls.
  • The one-type call sites, found by building main with [[deprecated]] one-type overloads added and -w removed, across companion_radio, simple_repeater and simple_room_server. There are 10:

    • examples/companion_radio/MyMesh.cpp:2026: min, uint8_t, the hop limit above;
    • examples/companion_radio/MyMesh.cpp:989-990, and src/helpers/CommonCLI.cpp:112-113: constrain of the radio's frequency and bandwidth, float;
    • src/helpers/CommonCLI.cpp:118: constrain of adc_multiplier, float;
    • src/helpers/CommonCLI.cpp:244: min, int, in neighbor.remove;
    • src/helpers/CommonRadioPrefs.cpp:169: constrain, int, set agc.reset.interval;
    • src/helpers/radiolib/RadioLibWrappers.cpp:241: max(0.0, min(1.0, …)), double, the packet score.

    The frequency and bandwidth calls happened to return the right values in my builds, so nodes stayed on frequency, but that was the undefined behaviour's luck.

  • Under MeshBench v0.4.1, the patched companion answers the hop limit as set (3a 0e 07, then 3b → 19 0e 07). It gives the same results as an unpatched build in the protocol checks I run against it (65 cases).

Reported in MeshBench/meshbench#810.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants