Silence a GCC dangling-reference warning in controller parsing - #106
Merged
Conversation
GCC emits -Wdangling-reference for the supported_commands range-for in
process_server_state_controller(), and it fires on every ESP-IDF build
of the component:
warning: possibly dangling reference to a temporary
note: 'MemberProxy<JsonObject, RamString>' temporary created here
The range initializer chained .as<JsonArrayConst>() off the MemberProxy
temporary returned by operator[]. Only the final temporary gets lifetime
extension, so the proxy dies at the end of the full-expression while the
loop is still running.
It was benign: JsonArrayConst stores a const ArrayData* and a const
ResourceManager*, both pointing into the JsonDocument's memory pool, and
JsonArrayConstIterator holds an ArrayData::iterator plus that same
resource pointer. Nothing reads back through the dead proxy. GCC's
heuristic cannot see that, so bind the array to a named local instead of
suppressing the diagnostic.
This also matches what the file already does everywhere else. The five
other array loops (active_roles, artwork channels, visualizer types, and
both stream roles lists) already name a local first; this was the only
chained range initializer left.
Verified with g++ 15 on the host include set: the warning reproduces
before the change and no -Wdangling-reference remains across src/ or
src/host/ after it. Host clang build and all 123 tests still pass.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a GCC -Wdangling-reference warning in process_server_state_controller() by avoiding a chained temporary in a range-based for loop, aligning this loop with the existing local-binding pattern used elsewhere in src/protocol.cpp.
Changes:
- Bind
controller_object["supported_commands"].as<JsonArrayConst>()to a named local (commands_array) before iterating. - Keep parsing behavior unchanged while removing a noisy compiler warning on ESP-IDF/GCC builds.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Bind the
supported_commandsarray to a named local before iterating itin
process_server_state_controller(), so GCC stops emitting-Wdangling-referencethere.Why
The warning fires on every ESP-IDF build of the component (including via
ESPHome), and is present in released v0.7.1 and on current
main:The range initializer chained
.as<JsonArrayConst>()off theMemberProxytemporary returned byoperator[]. Only the finaltemporary in such a chain gets lifetime extension in C++20, so the proxy
is destroyed at the end of the full-expression while the loop body is
still running.
It was benign.
JsonArrayConststores only aconst ArrayData*anda
const ResourceManager*, both pointing into theJsonDocument'smemory pool, and
JsonArrayConstIteratorholds anArrayData::iteratorplus that same resource pointer. Neither the array nor its iterators
retain anything that points into the dead proxy, so the destroyed
temporary was never read. GCC's heuristic cannot see through the proxy
type, though, so this fixes the code rather than suppressing the
diagnostic.
How
This is also what the rest of the file already does. The five other
array loops (
active_roles, artworkchannels, visualizertypes, andthe
roleslists instream/endandstream/clear) each name a localfirst; the loop at line 264 ranges over a function parameter. This was
the only chained range initializer left, so the change makes the file
consistent rather than introducing a new idiom.
No behavior change.
Testing
macOS builds with clang, which does not implement
-Wdangling-referenceand so never reproduced this. Verified with Homebrew
g++-15driven atthe host build's exact flags and include set:
src/andsrc/host/with-Wall -Wextra -Wdangling-reference -fsyntax-only: nodangling-reference warnings remain anywhere.
ctest123/123 pass.clang-format --dry-run -Werror src/protocol.cppclean.