Repository navigation
fix(cstring): make fl::strncpy always null-terminate (#3588) - #3602
Conversation
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]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe Changesfl::strncpy null-termination behavior
Estimated code review effort: 1 (Trivial) | ~5 minutes Poem:
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
…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]>
fl::strncpywas a raw pass-through to::strncpy, which does not null-terminate whenstrlen(src) >= n. Every FastLED caller passesn = sizeof(dst) - 1(reserving a terminator byte), so the safe contract is: copy at mostnchars, then always terminate atdest[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)intochar uri[160]; for a URI ≥159 bytes the old strncpy lefturiunterminated, and the query-strip loopfor (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 passsizeof(dst)-1and are now safe without changes.Honest scope: this is not the sole #3588 corruptor — the short-URI repro still crashes (a
json_valuecontrol 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
Documentation
Tests