Skip to content

Fix heap allocation leak in Socket.getValue - #226

Merged
swhitty merged 1 commit into
swhitty:mainfrom
ianegordon:ian/tvt-1062-socketgetvalue-leaks-a-heap-allocation-on-every-call
Jul 22, 2026
Merged

Fix heap allocation leak in Socket.getValue#226
swhitty merged 1 commit into
swhitty:mainfrom
ianegordon:ian/tvt-1062-socketgetvalue-leaks-a-heap-allocation-on-every-call

Conversation

@ianegordon

Copy link
Copy Markdown
Contributor

Summary

Socket.getValue allocates an UnsafeMutablePointer<O.SocketValue> that is never deallocated — on either the success or throw path — leaking one heap block on every socket-option read (socketType, receiveBufferSize, sendBufferSize, etc.).

The fix adds defer { valuePtr.deallocate() } immediately after allocation. This satisfies the documented precondition of UnsafeMutablePointer.deallocate(): the pointer is the start of the allocated block, and the memory is never initialized through Swift's typed model (getsockopt writes raw bytes; existing conformances use trivial Int32 values).

Testing

Full suite passes (449 tests, 51 suites). Existing SocketTests already cover correct value reads through getValue; the leak itself is not assertable in unit form.

🤖 Generated with Claude Code

UnsafeMutablePointer.allocate was never deallocated on either the
success or throw path, leaking one heap block per socket-option read.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.89%. Comparing base (4e246d3) to head (48e68fa).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #226   +/-   ##
=======================================
  Coverage   92.88%   92.89%           
=======================================
  Files          71       71           
  Lines        3727     3728    +1     
=======================================
+ Hits         3462     3463    +1     
  Misses        265      265           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@swhitty
swhitty merged commit 0ddf486 into swhitty:main Jul 22, 2026
13 checks passed
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.

2 participants