Skip to content

feat(format): add generic fl::printf placeholders and mismatch-safety tests #3174

Closed

Description

@zackees

Context

fl::printf / fl::snprintf currently provide FastLED-owned formatting in src/fl/stl/stdio.h, and #3158 is migrating logging call sites toward FL_*_F(...) macros backed by fl::printf-style formatting. That makes the formatter a larger public surface area, so it needs better ergonomics and stronger failure-mode coverage.

Two gaps should be tracked separately from the logging migration:

  1. Generic placeholders: C++ callers often want to log arbitrary FastLED types without hand-selecting %d, %u, %s, casts, or .c_str() conversions. Consider an idiomatic placeholder form, potentially {} for any supported variable, while preserving existing printf-style support where needed.
  2. Mismatched argument safety: calls like fl::printf("number: %d\n", "number") should not crash or read invalid memory. The formatter should degrade predictably, and tests should document the behavior.

Proposal

Enhance fl::printf / fl::snprintf so callers can use a generic placeholder for supported FastLED-printable values. {} is worth considering because it avoids pretending to be C printf when the implementation is type-aware C++ formatting, but the final syntax should fit the existing fl/stl/stdio.h design and embedded constraints.

Add comprehensive tests around malformed or surprising format strings and type mismatches. The goal is not necessarily to make every invalid call produce perfect output, but to guarantee bounded, deterministic behavior with no crashes, no invalid reads, and clearly documented fallback text.

Acceptance criteria

  • fl::printf and fl::snprintf support an idiomatic generic placeholder for common scalar/string/FastLED-owned types, with tests for at least integers, unsigned integers, bool, char, C strings, fl::string, pointers, and representative FastLED value types where supported by the formatter.
  • Existing %-style behavior remains covered by tests and continues to work for current supported specifiers.
  • Type mismatch tests cover scenarios such as:
    • %d with a string argument.
    • %s with an integer argument.
    • %p with non-pointer and null pointer values.
    • too few arguments for placeholders.
    • extra arguments after all placeholders are consumed.
    • malformed specifiers such as a trailing %, unknown %q, incomplete width/precision forms, and literal %%.
    • generic {} with too few and extra arguments.
    • escaped braces or literal braces, if {} is adopted.
  • Wrong-type or malformed format calls have documented behavior and tests assert that behavior. At minimum they must not crash or read through invalid pointers.
  • Tests live near the current formatter tests, such as tests/fl/stl/stdio.cpp and/or tests/fl/stl/cstdio.cpp, and include both direct string-buffer checks and print-path coverage where practical.

Open questions

  • Should the generic placeholder be {}, %?, or another syntax that avoids conflict with existing percent-format behavior?
  • Should wrong-type % placeholders emit a visible sentinel such as <format-error>, leave the placeholder unchanged, or format through the argument's generic string path?
  • Should compile-time checking be added for literal format strings where the implementation can reasonably do so without hurting embedded compatibility?

Related issues

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