Skip to content

[cpp] Avoid temporary flecs::term objects in query builder with() methods - #2193

Open
AnonimiAngels wants to merge 1 commit into
SanderMertens:masterfrom
AnonimiAngels:fix/query-builder-with-no-term-temporary
Open

[cpp] Avoid temporary flecs::term objects in query builder with() methods#2193
AnonimiAngels wants to merge 1 commit into
SanderMertens:masterfrom
AnonimiAngels:fix/query-builder-with-no-term-temporary

Conversation

@AnonimiAngels

Copy link
Copy Markdown
Contributor

The with() overloads in query_builder_i construct a temporary flecs::term just to copy its value into the query descriptor. Term objects store pointers to their own stack memory (term_/term_ref_ point at the object's own value member), so the assignment gets reported by clang's static analyzer as an escaping stack address (clang-analyzer-core.StackAddressEscape, anchored at builder_i.hpp:77). It is a false positive — only the pointer-free ecs_term_t value is copied — but it fires in any downstream project that runs clang-tidy over code that instantiates a query builder (world.each, query/system/observer builders), and it cannot be suppressed downstream without disabling the checker.

This writes the term value directly instead of going through the temporary:

  • Term values are identical for all seven overloads; the $ name-to-variable handling is reused through the inherited first() / second().
  • with(flecs::term&) / with(flecs::term&&) are unchanged (caller-owned objects, no temporary involved).
  • Side effect: skips the temporary's construction, conversion copy and virtual destructor on every with() call.
  • distr/ contains the same change.

Tested with bake run test/query and bake run test/cpp (all green), and distr/flecs.c compiles with the CI flag set (clang -Werror -Wshadow -Wconversion ...). Also verified in a downstream C++26 project running clang-tidy 22 with clang-analyzer-* promoted to errors: the report disappears at the source and the full build stays green.

…hods

The with() overloads constructed a temporary flecs::term just to copy its
value into the query descriptor. Term objects store pointers to their own
stack memory (term_/term_ref_), so assigning a temporary to the descriptor
gets reported by static analyzers as an escaping stack address
(clang-analyzer core.StackAddressEscape), even though only the pointer-free
ecs_term_t value is copied.

Write the term value directly instead. This produces identical term values,
removes the analyzer report at the source, and skips the temporary's
construction, conversion copy and virtual destructor.
@AnonimiAngels AnonimiAngels changed the title Avoid temporary flecs::term objects in query builder with() methods [cpp] Avoid temporary flecs::term objects in query builder with() methods Jul 8, 2026
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.

1 participant