| ... | @@ -2036,7 +2036,8 @@ test createNullDelimitedEnvMap { | ... | @@ -2036,7 +2036,8 @@ test createNullDelimitedEnvMap { |
| 2036 | pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) ![]u16 { | 2036 | pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) ![]u16 { |
| 2037 | // count bytes needed | 2037 | // count bytes needed |
| 2038 | const max_chars_needed = x: { | 2038 | const max_chars_needed = x: { |
| 2039 | var max_chars_needed: usize = 4; // 4 for the final 4 null bytes | 2039 | // Only need 2 trailing NUL code units for an empty environment |
| | 2040 | var max_chars_needed: usize = if (env_map.count() == 0) 2 else 1; |
| 2040 | var it = env_map.iterator(); | 2041 | var it = env_map.iterator(); |
| 2041 | while (it.next()) |pair| { | 2042 | while (it.next()) |pair| { |
| 2042 | // +1 for '=' | 2043 | // +1 for '=' |
| ... | @@ -2060,12 +2061,14 @@ pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) ! | ... | @@ -2060,12 +2061,14 @@ pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) ! |
| 2060 | } | 2061 | } |
| 2061 | result[i] = 0; | 2062 | result[i] = 0; |
| 2062 | i += 1; | 2063 | i += 1; |
| 2063 | result[i] = 0; | 2064 | // An empty environment is a special case that requires a redundant |
| 2064 | i += 1; | 2065 | // NUL terminator. CreateProcess will read the second code unit even |
| 2065 | result[i] = 0; | 2066 | // though theoretically the first should be enough to recognize that the |
| 2066 | i += 1; | 2067 | // environment is empty (see https://nullprogram.com/blog/2023/08/23/) |
| 2067 | result[i] = 0; | 2068 | if (env_map.count() == 0) { |
| 2068 | i += 1; | 2069 | result[i] = 0; |
| | 2070 | i += 1; |
| | 2071 | } |
| 2069 | return try allocator.realloc(result, i); | 2072 | return try allocator.realloc(result, i); |
| 2070 | } | 2073 | } |
| 2071 | | 2074 | |