Skip to content

Commit 37e468d

Browse files
drcynicmellinoe
authored andcommitted
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.
1 parent 0ce4c37 commit 37e468d

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

src/ImGui.NET/ImGui.Manual.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static bool InputText(
4444
{
4545

4646
int labelByteCount = Encoding.UTF8.GetByteCount(label);
47-
byte* labelBytes = stackalloc byte[labelByteCount];
47+
byte* labelBytes = stackalloc byte[labelByteCount + 1];
4848
fixed (char* labelPtr = label)
4949
{
5050
Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount);
@@ -83,7 +83,7 @@ public static bool InputText(
8383
IntPtr user_data)
8484
{
8585
int labelByteCount = Encoding.UTF8.GetByteCount(label);
86-
byte* labelBytes = stackalloc byte[labelByteCount];
86+
byte* labelBytes = stackalloc byte[labelByteCount + 1];
8787
fixed (char* labelPtr = label)
8888
{
8989
Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount);
@@ -146,7 +146,7 @@ public static bool InputTextMultiline(
146146
IntPtr user_data)
147147
{
148148
int labelByteCount = Encoding.UTF8.GetByteCount(label);
149-
byte* labelBytes = stackalloc byte[labelByteCount];
149+
byte* labelBytes = stackalloc byte[labelByteCount + 1];
150150
fixed (char* labelPtr = label)
151151
{
152152
Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount);
@@ -216,7 +216,7 @@ public static bool InputText(
216216
{
217217

218218
int labelByteCount = Encoding.UTF8.GetByteCount(label);
219-
byte* labelBytes = stackalloc byte[labelByteCount];
219+
byte* labelBytes = stackalloc byte[labelByteCount + 1];
220220
fixed (char* labelPtr = label)
221221
{
222222
Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount);

0 commit comments

Comments
 (0)