fix: S12.34 drain full lwIP RX pbuf chain, not just the head link#564
Conversation
LwipRawTcpStream_DrainHeadBytes read only the head link (head->len) then pbuf_free(head) freed the whole chain, silently losing tot_len - len tail bytes whenever lwIP delivered a chained pbuf. Network-reachable corruption for the TLS-over-LwipRawTcpStream stack, whose _Read carries TLS record bytes. Key the drain off tot_len and delegate the chain walk to lwIP's own pbuf_copy_partial. Advance the cursor and return by the actual copied count, not the requested amount, so a malformed/overstated tot_len degrades to would-block rather than reporting un-copied stale bytes. Add a faithful pbuf_copy_partial to LwipPbufFake so the chained tests exercise a real multi-link walk. Note the chained-pbuf contract in docs/integrating-lwip.md. Move three cast-helper MISRA suppressions to track the removed <string.h> include. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughThis PR fixes a critical correctness bug where the lwIP raw TCP stream's RX path silently drops tail bytes when a received segment spans multiple pbuf links. The fix rewrites the drain function to use ChangeslwIP pbuf chain RX drain fix
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
☀️ Quality Summary 🚦 build-linux-gcc: 100% successful (✔️ 1521 passed) Created by Quality Monitor v1.14.0 (#f3859fd). More details are shown in the GitHub Checks Result. |
Closes #559.
Problem
LwipRawTcpStream_DrainHeadBytesread only the head link (head->len)then
pbuf_free(head)freed the whole chain — silently losingtot_len - lentail bytes whenever lwIP delivered a chained pbuf (a receivedsegment spanning more than one pool pbuf). Memory-safe, but a
network-reachable correctness defect: the advertised TLS-over-
LwipRawTcpStreamstack carries real TLS record bytes on
_Read, so the truncation corrupts thehandshake/record stream non-deterministically with on-path segmentation.
Slipped through because the unit test only ever fabricated single-link pbufs
(
len == tot_len,next == NULL).Fix
RxHeadOffset) and the fully-drained test offtot_len, and delegate the chain walk to lwIP's ownpbuf_copy_partial(walks
head->next).pbuf_frees the head — which frees every link — onlyonce the whole chain is consumed.
requested amount. Under lwIP's
tot_len == Σ link->leninvariant the two areequal; keying off the real copy means a malformed/overstated
tot_lendegrades to would-block (StreamSender recovers) instead of advancing past
un-copied bytes and reporting stale buffer content into a stacked TLS stream.
Tests (TDD, red→green)
ReadDrainsEveryLinkOfAChainedPbuf— full 2-link chain returned across a read.ReadAcrossChainedPbufLinkBoundaryPreservesByteOrder— a read straddling thelink boundary; chain stays queued until fully drained, freed exactly once.
ReadReportsOnlyBytesActuallyCopiedWhenTotLenOverstatesChain— overstatedtot_lenreturns only what was copied, never the phantom tail.pushIncomingChainfixture helper; faithfulpbuf_copy_partialadded toLwipPbufFakeso the tests exercise a real multi-link walk.SolidSyslogLwipRawTcpStreamTest60 green;SolidSyslogLwipRawDatagramTest(shares the fake) 36 green; pbuf leak invariant balanced.
Docs
Chained-pbuf contract noted in
docs/integrating-lwip.md, including thewarning for integrators supplying their own
SolidSyslogStreambyte transport.Out of scope
The MITM-class no-hostname-verification TLS default — already tracked/decided in
S12.28 (#529).
🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests
Documentation