From 37e468dbe5927652cab8ea66c70d2e607913b712 Mon Sep 17 00:00:00 2001 From: cynic Date: Thu, 6 Dec 2018 22:07:16 +0100 Subject: [PATCH] Fix non '\0' terminated labels for InputText Previously only the length of the label was allocated. As .net is initializing arrays with 0 and (at least for this stackalloc here) this happens in 4-byte chunks only strings with a length multiple of 4 were affected as in this case the allocated size exactly matched the length of the string and so no 0 was left to be used for string end. Now the array is generated always one byte larger than the label length, so there will always be a 0 at the end. --- src/ImGui.NET/ImGui.Manual.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ImGui.NET/ImGui.Manual.cs b/src/ImGui.NET/ImGui.Manual.cs index 72400d9..30f4550 100644 --- a/src/ImGui.NET/ImGui.Manual.cs +++ b/src/ImGui.NET/ImGui.Manual.cs @@ -44,7 +44,7 @@ namespace ImGuiNET { int labelByteCount = Encoding.UTF8.GetByteCount(label); - byte* labelBytes = stackalloc byte[labelByteCount]; + byte* labelBytes = stackalloc byte[labelByteCount + 1]; fixed (char* labelPtr = label) { Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount); @@ -83,7 +83,7 @@ namespace ImGuiNET IntPtr user_data) { int labelByteCount = Encoding.UTF8.GetByteCount(label); - byte* labelBytes = stackalloc byte[labelByteCount]; + byte* labelBytes = stackalloc byte[labelByteCount + 1]; fixed (char* labelPtr = label) { Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount); @@ -146,7 +146,7 @@ namespace ImGuiNET IntPtr user_data) { int labelByteCount = Encoding.UTF8.GetByteCount(label); - byte* labelBytes = stackalloc byte[labelByteCount]; + byte* labelBytes = stackalloc byte[labelByteCount + 1]; fixed (char* labelPtr = label) { Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount); @@ -216,7 +216,7 @@ namespace ImGuiNET { int labelByteCount = Encoding.UTF8.GetByteCount(label); - byte* labelBytes = stackalloc byte[labelByteCount]; + byte* labelBytes = stackalloc byte[labelByteCount + 1]; fixed (char* labelPtr = label) { Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount);