Skip to content

Native builds: host min/max/constrain return a dangling reference, so the companion stores a hop limit of 0 #810

Closed

Description

@hamsolodev

What happened

On a native companion (v1.18.0, built from source with meshcore-native main 9097ca8 and MeshBench v0.4.1), setting the auto-add hop limit stores 0, whatever value is sent. CMD_SET_AUTOADD_CONFIG 3a 0e 07, then CMD_GET_AUTOADD_CONFIG, reads back 19 0e 00. On a radio it reads 19 0e 07.

The cause is the host's min, max and constrain in variants/host/HostArduino.h:62-73. They're declared -> decltype(a < b ? a : b), and when both arguments have one type that is a reference to the function's by-value parameter, which is gone once it returns. Optimised, min(cmd_frame[2], (uint8_t)64) (MyMesh.cpp:2026) reads a stale stack slot.

It's undefined behaviour at every one-type call. In MeshCore v1.18.0, across the companion, repeater and room server, there are 10 such calls, including:

  • constrain of the radio's frequency and bandwidth (MyMesh.cpp:989-990, CommonCLI.cpp:112-113);
  • the packet score's max(0.0, min(1.0, …)) (RadioLibWrappers.cpp:241).

Those happened to return the right values in my builds, but nothing guarantees it.

How to reproduce it

  1. Run a native companion and connect a client.
  2. Send 3a 0e 07 (CMD_SET_AUTOADD_CONFIG with a hop limit of 7), then 3b (CMD_GET_AUTOADD_CONFIG).
  3. The reply is 19 0e 00: the hop limit is 0.

Alone, without the firmware:

#include <cstdint>
template <class T, class U>
constexpr auto min(T a, U b) -> decltype(a < b ? a : b) { return a < b ? a : b; }
volatile uint8_t x = 7;
int main() { return min(x, (uint8_t)64); }  // exits 7 at -O0, 0 at -O2 (g++ 14.2)

Version

meshcore-native main 9097ca8 (and every tag I looked at), MeshBench v0.4.1, MeshCore companion-v1.18.0, g++ from Debian trixie.

Platform

Linux

Anything else

Fix, with a test that fails without it: MeshBench/meshcore-native#37. It returns std::decay_t of the same expression. The one-type call sites, and how I found them, are listed there.

Activity

  1. A13xB0 commented on Nov 21, 2026

    @A13xB0
    Collaborator

    Fixed by MeshBench/meshcore-native#37. Thanks for the detailed report and the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions