Commit 213390b
Cyrus Najmabadi
·
2026-10-22 22:42:01 -0400 EDT
parent 7f0ffdb
Fix double free when a tracked environment variable is missing (#301)
1 files changed,
+3,
-5
+3,
-5
jump to top
+3,
-5
jump to top
| ... | ... | @@ -855,9 +855,10 @@ pub fn printEnv(alloc: Allocator, io: Io, name: []const u8, key: ?[]const u8) ![ | |
| 855 | 855 | defer alloc.free(socket_path); | |
| 856 | 856 | ||
| 857 | 857 | const payload = try ipc.roundTripForTag(alloc, socket_path, .EnvGet, "", .EnvData); | |
| 858 | - | errdefer alloc.free(payload); | |
| 859 | 858 | ||
| 859 | + | // A whole-environment query gives this buffer to the caller. | |
| 860 | 860 | if (key == null) return payload; | |
| 861 | + | defer alloc.free(payload); | |
| 861 | 862 | ||
| 862 | 863 | // Search for key in payload: lines formatted as "KEY=VALUE\n" | |
| 863 | 864 | var lines = std.mem.splitScalar(u8, payload, '\n'); |
| ... | ... | @@ -865,12 +866,9 @@ pub fn printEnv(alloc: Allocator, io: Io, name: []const u8, key: ?[]const u8) ![ | |
| 865 | 866 | if (std.mem.startsWith(u8, line, key.?)) { | |
| 866 | 867 | if (line.len > key.?.len and line[key.?.len] == '=') { | |
| 867 | 868 | const val = line[key.?.len + 1 ..]; | |
| 868 | - | const result = try alloc.dupe(u8, val); | |
| 869 | - | alloc.free(payload); | |
| 870 | - | return result; | |
| 869 | + | return try alloc.dupe(u8, val); | |
| 871 | 870 | } | |
| 872 | 871 | } | |
| 873 | 872 | } | |
| 874 | - | alloc.free(payload); | |
| 875 | 873 | return error.EnvVarNotFound; | |
| 876 | 874 | } |