Repository navigation
host: min, max and constrain return a value, not a reference - #37
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes, and why
min,maxandconstraininvariants/host/HostArduino.hwere 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 returnstd::decay_tof 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 as19 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 fromminandmaxof twouint8_ts and fromconstrainof threeints. It exits 1 at -O2. With it, it printsokat -O0 and -O2.companion_radio and simple_repeater were built at this commit and at main 9097ca8: same image,
SOURCE_DATE_EPOCHpinned, MeshCore companion-v1.18.0.nm -Soutput differs in one compiler-numbered switch table in the companion (CSWTCH.377against.371), and nowhere in the repeater.MyMesh.o, andCommonCLI.o,CommonRadioPrefs.oandRadioLibWrappers.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-wremoved, 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, andsrc/helpers/CommonCLI.cpp:112-113:constrainof the radio's frequency and bandwidth,float;src/helpers/CommonCLI.cpp:118:constrainofadc_multiplier,float;src/helpers/CommonCLI.cpp:244:min,int, inneighbor.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, then3b→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.