fix(webserver): fix CWebSocket memory leaks and address AI quality findings - #29
Merged
Merged
Conversation
…uffer leaks
CWebSocket had no destructor, so when a socket was destroyed with pending
write data, the entire CChunk linked list and m_pBuf were leaked.
The correct fix is a CWebSocket destructor that walks the list using the
same save-next-then-delete pattern as OnSend(). Adding delete-m_pNext to
~CChunk() instead (as suggested by GitHub's AI findings) would be wrong:
OnSend() saves pNext before calling delete m_pHead, so recursive chain
deletion inside the destructor would cause a use-after-free there.
Also fix a typo in the m_pHead comment ("tails" -> "head"), add a CChunk
default constructor to zero-initialise all members, and remove the
redundant null guard in ~CChunk() (delete[] NULL is a no-op).
…ounds check Three independent cleanups from code analysis: - Remove m_Cookie: the field was initialized in the constructor but never read or written anywhere in the codebase; dead code removed from both the header and the constructor. - Fix extra whitespace in buffer growth expression: 'm_dwBufSize >> 1' -> 'm_dwBufSize >> 1'. - Restructure POST body bounds check to anchor the subtraction on bodyOffset rather than bodyLen. The original 'bodyLen <= m_dwRecv && bodyOffset <= m_dwRecv - bodyLen' is safe due to short-circuit evaluation, but 'bodyOffset <= m_dwRecv && bodyLen <= m_dwRecv - bodyOffset' removes the subtle ordering dependency and reads more naturally as "offset fits, then length fits within the remainder". Skipped: renaming FindHeaderCaseInsensitive (anonymous namespace, single call site searching for a header -- name is accurate) and replacing 0 with nullptr (project consistently uses NULL/0 throughout).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses findings from the GitHub AI quality scan on
src/webserver/src/WebSocket.cpp.fix: add CWebSocket destructor to prevent chunk list and buffer leaks
CWebSockethad no destructor, so when a socket was destroyed with pendingwrite data the entire
CChunklinked list andm_pBufwere silently leaked.The fix adds
~CWebSocket()that walks the list using the samesave-next-then-delete pattern already used in
OnSend(). Addingdelete m_pNextto~CChunk()instead (the AI's suggested approach)would be wrong:
OnSend()savespNextbefore callingdelete m_pHead,so recursive chain deletion inside the destructor would cause a
use-after-free there.
Also adds a
CChunkdefault constructor to zero-initialise all members,removes the redundant null guard in
~CChunk()(delete[] NULLis ano-op), and fixes the
m_pHeadcomment typo ("tails" → "head").fix: remove dead m_Cookie field, fix whitespace, clarify bounds check
m_Cookie: initialized in the constructor but never read orwritten anywhere in the codebase; removed from both the header and the
constructor.
m_dwBufSize >> 1→m_dwBufSize >> 1.bodyLen <= m_dwRecv && bodyOffset <= m_dwRecv - bodyLentobodyOffset <= m_dwRecv && bodyLen <= m_dwRecv - bodyOffset.The original is safe due to short-circuit evaluation, but the new form
removes the subtle ordering dependency and reads more naturally as
"offset fits, then length fits within the remainder".
Skipped findings
FindHeaderCaseInsensitive: anonymous-namespace function witha single call site searching for a header — the name accurately describes
its use.
0withnullptr: the project consistently usesNULL/0throughout (~1400 occurrences vs ~20
nullptr); a two-line change wouldbe inconsistent with the established style.