Skip to content

fix(webserver): fix CWebSocket memory leaks and address AI quality findings - #29

Merged
mrjimenez merged 2 commits into
amule-org:masterfrom
mrjimenez:worktree-pr-ia
Jun 9, 2026
Merged

fix(webserver): fix CWebSocket memory leaks and address AI quality findings#29
mrjimenez merged 2 commits into
amule-org:masterfrom
mrjimenez:worktree-pr-ia

Conversation

@mrjimenez

Copy link
Copy Markdown

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

CWebSocket had no destructor, so when a socket was destroyed with pending
write data the entire CChunk linked list and m_pBuf were silently leaked.

The fix adds ~CWebSocket() that walks the list using the same
save-next-then-delete pattern already used in OnSend(). Adding
delete m_pNext to ~CChunk() instead (the AI's suggested approach)
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 adds a CChunk default constructor to zero-initialise all members,
removes the redundant null guard in ~CChunk() (delete[] NULL is a
no-op), and fixes the m_pHead comment typo ("tails" → "head").

fix: remove dead m_Cookie field, fix whitespace, clarify bounds check

  • Remove m_Cookie: initialized in the constructor but never read or
    written anywhere in the codebase; removed from both the header and the
    constructor.
  • Whitespace: m_dwBufSize >> 1m_dwBufSize >> 1.
  • Bounds check: restructure the POST body check from
    bodyLen <= m_dwRecv && bodyOffset <= m_dwRecv - bodyLen to
    bodyOffset <= 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

  • Rename FindHeaderCaseInsensitive: anonymous-namespace function with
    a single call site searching for a header — the name accurately describes
    its use.
  • Replace 0 with nullptr: the project consistently uses NULL/0
    throughout (~1400 occurrences vs ~20 nullptr); a two-line change would
    be inconsistent with the established style.

mrjimenez added 2 commits June 8, 2026 22:51
…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).
@mrjimenez
mrjimenez merged commit 115c3d1 into amule-org:master Jun 9, 2026
10 checks passed
@mrjimenez
mrjimenez deleted the worktree-pr-ia branch June 9, 2026 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant