CodeEdit icon indicating copy to clipboard operation
CodeEdit copied to clipboard

🐞 Possible memory leak in CurrentUser struct

Open wjk opened this issue 4 months ago • 1 comments

Description

In CurrentUser.getCurrentUser(), an UnsafeMutablePointer<passwd> is allocated but never explicitly freed. When I added a deallocate() call in a defer block, the process crashed saying the pointer was freed but never allocated. I am uncertain if there is a memory leak here.

To Reproduce

  1. Open a workspace, thus creating a terminal, which calls CurrentUser.getCurrentUser().

Expected Behavior

No memory leak, but, again, I am uncertain if there is actually a leak here.

Version Information

CodeEdit: 0.3.6-dev (37) macOS: 15.6.1 Xcode: 26.0

Additional Context

No response

Screenshots

No response

wjk avatar Sep 18 '25 17:09 wjk

I think the there is a Problem on line 43:

var result: UnsafeMutablePointer<passwd>? = UnsafeMutablePointer<passwd>.allocate(capacity: 1)

This allocates heap memory for a passwd pointer, but this memory is never deallocated.

When I tried to deallocate result in a defer block, it crashed because:

  1. I allocated memory for result
  2. Then getpwuid_r overwrites result to point to pwd (a stack variable)
  3. When I tried to deallocate result, it was now pointing to stack memory (pwd), not the heap memory you allocated
  4. Trying to deallocate stack memory = crash

For a fix, I have replaced line 43 with: var result: UnsafeMutablePointer<passwd>?

This way:

  1. No memory is allocated
  2. getpwuid_r sets result to point to pwd
  3. No memory leak
  4. No crash

https://github.com/vksvicky/CodeEdit/tree/fix/deprecations-memory-leak-entitlements. Shall create a pull request

vksvicky avatar Dec 02 '25 09:12 vksvicky