Skip to content

cpp-lint: forbid &container[N] / .data() / &container.front() — require fl::span or iterators #3287

Closed

Description

@zackees

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/back
  • container.data() — when the container is fl::deque<T> or any other type where contiguity is not guaranteed
  • memcpy(dst, &dq[0], ...), memmove, memset on container element addresses
  • static_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 passing fl::span<T> explicitly; the conversion is checked at the type level.
  • Iterator pair [begin, end) — for arbitrary forward / random-access containers including fl::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, and std:: interop is a different concern). Raw C arrays and fl::span<T> itself (the canonical safe-view type).
  • Suppression: // fl-lint: container-data-ptr-ok line-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:

  1. clang-query AST matcher. Match unaryOperator(hasOperatorName("&"), hasUnaryOperand(cxxOperatorCallExpr(hasOverloadedOperatorName("[]"), on(hasType(hasDeclaration(classTemplateSpecializationDecl(hasName("fl::deque"))))))). Reference: the cpp-linter-builder-agent and /new-cpp-lint skill scaffold new rules in this style. See also agents/docs/linter-architecture.md.

  2. 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

Refs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions