Skip to content

Commit dc53fb7

Browse files
committed
Fix: Memory leak in CurrentUser.getCurrentUser()
Fixed memory leak where an UnsafeMutablePointer<passwd> was allocated but never deallocated. Issue Analysis: - Line 43 was allocating heap memory for `result` pointer - getpwuid_r() immediately overwrites `result` to point to `pwd` (stack) - The allocated heap memory was leaked on every call - Attempting to deallocate crashed because `result` pointed to stack memory Root Cause: The allocation was unnecessary. getpwuid_r() expects `result` as an output parameter - it sets the pointer itself, no pre-allocation needed. Solution: Removed the unnecessary .allocate(capacity: 1) call. Now `result` is initialized as nil and getpwuid_r() sets it to point to `pwd` on success. This function is called every time a terminal is opened, so the leak would accumulate with each terminal session.
1 parent d0d2893 commit dc53fb7

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

CodeEdit/Features/TerminalEmulator/Model/CurrentUser.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ struct CurrentUser {
3939
buffer.deallocate()
4040
}
4141
var pwd = passwd()
42-
// points to `pwd`
43-
var result: UnsafeMutablePointer<passwd>? = UnsafeMutablePointer<passwd>.allocate(capacity: 1)
42+
// `result` will be set by getpwuid_r to point to `pwd` on success
43+
var result: UnsafeMutablePointer<passwd>?
4444

4545
if getpwuid_r(getuid(), &pwd, buffer, bufsize, &result) != 0 { return nil }
4646

0 commit comments

Comments
 (0)