Skip to content

fix(cstring): make fl::strncpy always null-terminate (#3588) - #3602

Merged
zackees merged 1 commit into
masterfrom
fix/strncpy-always-terminate
Jul 5, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/strncpy-always-terminate

Conversation

@zackees

@zackees zackees commented Jul 5, 2026 •

Copy link
Copy Markdown
Member

fl::strncpy was a raw pass-through to ::strncpy, which does not null-terminate when strlen(src) >= n. Every FastLED caller passes n = sizeof(dst) - 1 (reserving a terminator byte), so the safe contract is: copy at most n chars, then always terminate at dest[copied] (in bounds). Remaining bytes are still zero-padded.

Closes a real buffer overrun found auditing #3588: the HTTP marshaling path does fl::strncpy(item->uri, req->uri, sizeof(item->uri)-1) into char uri[160]; for a URI ≥159 bytes the old strncpy left uri unterminated, and the query-strip loop for (char* c = item->uri; *c; ++c) would walk past the array and write a stray null byte one past the buffer on the first ? in adjacent memory. All call sites already pass sizeof(dst)-1 and are now safe without changes.

Honest scope: this is not the sole #3588 corruptor — the short-URI repro still crashes (a json_value control block is stomped by a separate writer, still under investigation). But this is a genuine latent overrun worth fixing on its own, and the user requested the reimplementation.

The cstring test subcase that asserted the old unterminated behavior is updated to assert guaranteed termination. Host 348/348, lint clean.

Refs #3588.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved bounded string copying so the destination is always null-terminated.
    • Remaining buffer bytes are now cleared after copying, making the result more predictable when the source is longer than the limit.
  • Documentation

    • Updated the string-copying API notes to clearly describe the new termination behavior and how it differs from standard library behavior.
  • Tests

    • Adjusted coverage to verify the new always-terminated behavior.

fl::strncpy was a raw pass-through to ::strncpy, which does NOT
null-terminate the destination when strlen(src) >= n. Every FastLED
caller passes n = sizeof(dst) - 1 (reserving a byte for the
terminator), so the safe contract is: copy at most n chars, then always
write a terminator at dest[copied] (copied <= n, in bounds). The
remaining destination bytes up to n are still zero-padded, preserving
::strncpy's padding behavior for shorter sources.

This closes a real buffer-overrun hazard surfaced while auditing #3588:
the HTTP request marshaling path does
`fl::strncpy(item->uri, req->uri, sizeof(item->uri) - 1)` into a
`char uri[160]`; for a request URI >= 159 bytes the old strncpy left
`uri` unterminated, and the subsequent query-strip loop
`for (char* c = item->uri; *c; ++c)` would walk past the array and, on
the first '?' it found in adjacent memory, write a stray null byte one
past the buffer. All existing call sites are unchanged (they already
pass sizeof(dst)-1) and are now safe.

Note: this is NOT the sole #3588 corruptor — the short-URI repro still
crashes (a json_value control block is stomped by a separate writer,
under investigation) — but it is a genuine latent overrun on its own.

The cstring test subcase that asserted the old unterminated behavior is
updated to assert guaranteed termination.

Co-Authored-By: Claude Fable 5 <[email protected]>
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Jul 5, 2026
@zackees
zackees merged commit fbeb889 into master Jul 5, 2026
11 checks passed
@coderabbitai

coderabbitai Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: db809c02-0480-44f7-b06e-6c0e4e3de082

📥 Commits

Reviewing files that changed from the base of the PR and between 7e71b49 and 4d00542.

📒 Files selected for processing (3)
  • src/fl/stl/cstring.cpp.hpp
  • src/fl/stl/cstring.h
  • tests/fl/stl/cstring.cpp

📝 Walkthrough

Walkthrough

The fl::strncpy implementation is changed from delegating to standard ::strncpy to a custom loop that copies characters, always null-terminates the destination, and zero-fills remaining bytes. Documentation and a test case are updated to reflect this new always-terminated behavior.

Changes

fl::strncpy null-termination behavior

Layer / File(s) Summary
Custom copy loop and documentation
src/fl/stl/cstring.cpp.hpp, src/fl/stl/cstring.h
Replaces the ::strncpy delegation with a manual loop that copies up to n characters, writes a null terminator, and zero-pads remaining bytes; header comment expanded to document this behavior versus standard ::strncpy.
Test assertion update
tests/fl/stl/cstring.cpp
Updates the n < source length subcase to expect buffer[n] to be '\0' instead of retaining the pre-filled sentinel value.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Poem:

A rabbit hopped to fix a copy,
No dangling bytes, no string too sloppy.
Now every tail ends clean and neat,
Zero-padded, null-terminated, complete!
🐰✂️

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/strncpy-always-terminate

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jul 5, 2026
eelstretching pushed a commit to eelstretching/FastLED-pico that referenced this pull request Jul 11, 2026
…astLED#3602)

fl::strncpy was a raw pass-through to ::strncpy, which does NOT
null-terminate the destination when strlen(src) >= n. Every FastLED
caller passes n = sizeof(dst) - 1 (reserving a byte for the
terminator), so the safe contract is: copy at most n chars, then always
write a terminator at dest[copied] (copied <= n, in bounds). The
remaining destination bytes up to n are still zero-padded, preserving
::strncpy's padding behavior for shorter sources.

This closes a real buffer-overrun hazard surfaced while auditing FastLED#3588:
the HTTP request marshaling path does
`fl::strncpy(item->uri, req->uri, sizeof(item->uri) - 1)` into a
`char uri[160]`; for a request URI >= 159 bytes the old strncpy left
`uri` unterminated, and the subsequent query-strip loop
`for (char* c = item->uri; *c; ++c)` would walk past the array and, on
the first '?' it found in adjacent memory, write a stray null byte one
past the buffer. All existing call sites are unchanged (they already
pass sizeof(dst)-1) and are now safe.

Note: this is NOT the sole FastLED#3588 corruptor — the short-URI repro still
crashes (a json_value control block is stomped by a separate writer,
under investigation) — but it is a genuine latent overrun on its own.

The cstring test subcase that asserted the old unterminated behavior is
updated to assert guaranteed termination.

Co-authored-by: Claude Fable 5 <[email protected]>
@zackees
zackees deleted the fix/strncpy-always-terminate branch August 21, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant