From 162628000f8e14c18e442d27efcee784b5ab988a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Feb 2023 11:55:08 +0000 Subject: [PATCH] Calculate the size needed before allocating --- core/os/os2/env_windows.odin | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/core/os/os2/env_windows.odin b/core/os/os2/env_windows.odin index f58922fac..105063343 100644 --- a/core/os/os2/env_windows.odin +++ b/core/os/os2/env_windows.odin @@ -65,7 +65,19 @@ _environ :: proc(allocator: runtime.Allocator) -> []string { } defer win32.FreeEnvironmentStringsW(envs) - r := make([dynamic]string, 0, 50, allocator) + n := 0 + for from, i, p := 0, 0, envs; true; i += 1 { + c := ([^]u16)(p)[i] + if c == 0 { + if i <= from { + break + } + n += 1 + from = i + 1 + } + } + + r := make([dynamic]string, 0, n, allocator) for from, i, p := 0, 0, envs; true; i += 1 { c := ([^]u16)(p)[i] if c == 0 {