Browse Source

Calculate the size needed before allocating

gingerBill 2 years ago
parent
commit
162628000f
1 changed files with 13 additions and 1 deletions
  1. 13 1
      core/os/os2/env_windows.odin

+ 13 - 1
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 {