Repository navigation
Description
Motivation
PR #3282 ("refactor(deque): chunked map-of-fixed-size-blocks layout") replaced fl::deque<T>'s vector-style contiguous-ring storage with a chunked map of fixed-size blocks. Pointer stability across push is now guaranteed, but elements are NO LONGER contiguous. The commit message itself flagged one such bug it had to fix in json::to_string_native:
result.assign(&json_chars[0], json_chars.size())was UB even on the old layout after a wraparound; the chunked layout makes it an immediate buffer overrun.
That's one site the author caught. #3286 is another — fl::audio::detector::TempoAnalyzer::update crashes on macOS-arm64 because some part of the audio pipeline made the same &deque[0]-style assumption. Linux x86-64 doesn't catch it (probably stricter sanitizer/alignment on arm64). There are almost certainly more sites lurking that we just haven't found yet.
The root cause is a memory-safety pattern that should be banned in the codebase: treating any fl:: container as a contiguous buffer by taking a raw pointer to its storage. The chunked deque has made this UB; even on containers where contiguity holds today (fl::vector, fl::string), the raw-pointer pattern is brittle to layout changes and bypasses bounds checking.
Goal
Add a new C++ lint rule (in the existing ci/lint_cpp/ framework — see agents/docs/linter-architecture.md) that catches and fails these patterns on fl:: containers:
&container[N]or&container.at(N)— taking address of an indexed element&container.front(),&container.back()— taking address of front/backcontainer.data()— when the container isfl::deque<T>or any other type where contiguity is not guaranteedmemcpy(dst, &dq[0], ...),memmove,memseton container element addressesstatic_cast<T*>(&container[0])and similar pointer-casts of indexed elements
Each violation should require one of these rewrites instead:
fl::span<T>— when the consumer needs a bounded view and is comfortable that the underlying storage IS contiguous (e.g.fl::vector). Caller proves contiguity by passingfl::span<T>explicitly; the conversion is checked at the type level.- Iterator pair
[begin, end)— for arbitrary forward / random-access containers includingfl::deque<T>. Idiomatic STL pattern; works regardless of underlying storage. - Explicit copy into a contiguous buffer —
fl::vector<T> contiguous(dq.begin(), dq.end()); use(contiguous.data(), ...);— escape hatch for legacy C APIs that genuinely require a single contiguous pointer.
Scope
- In scope: all
fl::containers (fl::vector,fl::deque,fl::string,fl::array,fl::fixed_vector,fl::circular_buffer, etc.). - Out of scope (allowlist):
std::containers (FastLED rarely uses them, andstd::interop is a different concern). Raw C arrays andfl::span<T>itself (the canonical safe-view type). - Suppression:
// fl-lint: container-data-ptr-okline-suffix comment, justified by a short reason. Used sparingly for genuine C-API interop sites.
Plausible implementation
Two options, pick whichever fits the existing linter framework better:
-
clang-query AST matcher. Match
unaryOperator(hasOperatorName("&"), hasUnaryOperand(cxxOperatorCallExpr(hasOverloadedOperatorName("[]"), on(hasType(hasDeclaration(classTemplateSpecializationDecl(hasName("fl::deque"))))))). Reference: thecpp-linter-builder-agentand/new-cpp-lintskill scaffold new rules in this style. See alsoagents/docs/linter-architecture.md. -
Regex pre-pass + clang-tidy for confirmation. Cheaper to ship; produces false positives near macros. Use as a warn-only first cut while developing the AST matcher.
Recommend (1) — the AST match is more precise and the existing framework supports it.
Acceptance criteria
- New checker module under
ci/lint_cpp/(or wherever the existing C++ checkers live). - Wired into
bash lintas a stage; warn-only initially, withFASTLED_LINT_CONTAINER_PTR_ERROR=1to gate. - Catches the
&json_chars[0]pattern that refactor(deque): chunked map-of-fixed-size-blocks layout (closes #3270) #3282 fixed injson::to_string_native(run against the pre-refactor(deque): chunked map-of-fixed-size-blocks layout (closes #3270) #3282 state). - Catches whatever pattern fl_audio_detectors macOS-arm64 unit test failing on master since PR #3282 (deque refactor) #3286 reveals in the audio detector code (run against the pre-fl_audio_detectors macOS-arm64 unit test failing on master since PR #3282 (deque refactor) #3286-fix state).
- Run across the current tree; produce a punchlist of every existing site. Bulk-fix as a follow-up PR or one PR per subsystem.
- Add a short section to
agents/docs/cpp-standards.mddocumenting the rule and thefl::span/ iterator alternatives.
Refs
- Discovered via: fl_audio_detectors macOS-arm64 unit test failing on master since PR #3282 (deque refactor) #3286 (audio detector regression from chunked deque)
- Pattern source: PR refactor(deque): chunked map-of-fixed-size-blocks layout (closes #3270) #3282 (
json::to_string_nativefix in the same commit) - Linter framework:
agents/docs/linter-architecture.md,ci/lint_cpp/,/new-cpp-lintskill
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
fix(lint): follow member-qualified receivers in container_ptr (#3287)
feat(lint): forbid raw pointers into fl:: container storage (#3287) (#…