Skip to content

Commit 7ac01b7

Browse files
committed
Add a 2048-byte limit to stack allocation for strings.
1 parent 37e468d commit 7ac01b7

11 files changed

Lines changed: 6795 additions & 2709 deletions

src/CodeGenerator/Program.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -618,24 +618,35 @@ private static void EmitOverload(
618618
else
619619
{
620620
preCallLines.Add($"byte* {nativeArgName};");
621+
preCallLines.Add($"int {correctedIdentifier}_byteCount = 0;");
621622
if (!hasDefault)
622623
{
623624
preCallLines.Add($"if ({textToEncode} != null)");
624625
preCallLines.Add("{");
625626
}
626-
preCallLines.Add($" int {correctedIdentifier}_byteCount = Encoding.UTF8.GetByteCount({textToEncode});");
627-
preCallLines.Add($" byte* {nativeArgName}_stackBytes = stackalloc byte[{correctedIdentifier}_byteCount + 1];");
628-
preCallLines.Add($" {nativeArgName} = {nativeArgName}_stackBytes;");
629-
preCallLines.Add($" fixed (char* {correctedIdentifier}_ptr = {textToEncode})");
630-
preCallLines.Add(" {");
631-
preCallLines.Add($" int {nativeArgName}_offset = Encoding.UTF8.GetBytes({correctedIdentifier}_ptr, {textToEncode}.Length, {nativeArgName}, {correctedIdentifier}_byteCount);");
632-
preCallLines.Add($" {nativeArgName}[{nativeArgName}_offset] = 0;");
633-
preCallLines.Add(" }");
627+
preCallLines.Add($" {correctedIdentifier}_byteCount = Encoding.UTF8.GetByteCount({textToEncode});");
628+
preCallLines.Add($" if ({correctedIdentifier}_byteCount > Util.StackAllocationSizeLimit)");
629+
preCallLines.Add($" {{");
630+
preCallLines.Add($" {nativeArgName} = Util.Allocate({correctedIdentifier}_byteCount + 1);");
631+
preCallLines.Add($" }}");
632+
preCallLines.Add($" else");
633+
preCallLines.Add($" {{");
634+
preCallLines.Add($" byte* {nativeArgName}_stackBytes = stackalloc byte[{correctedIdentifier}_byteCount + 1];");
635+
preCallLines.Add($" {nativeArgName} = {nativeArgName}_stackBytes;");
636+
preCallLines.Add($" }}");
637+
preCallLines.Add($" int {nativeArgName}_offset = Util.GetUtf8({textToEncode}, {nativeArgName}, {correctedIdentifier}_byteCount);");
638+
preCallLines.Add($" {nativeArgName}[{nativeArgName}_offset] = 0;");
639+
634640
if (!hasDefault)
635641
{
636642
preCallLines.Add("}");
637643
preCallLines.Add($"else {{ {nativeArgName} = null; }}");
638644
}
645+
646+
postCallLines.Add($"if ({correctedIdentifier}_byteCount > Util.StackAllocationSizeLimit)");
647+
postCallLines.Add($"{{");
648+
postCallLines.Add($" Util.Free({nativeArgName});");
649+
postCallLines.Add($"}}");
639650
}
640651
}
641652
else if (tr.Type == "char* []")

src/ImGui.NET/Generated/GlyphRangesBuilder.gen.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,29 @@ public void AddRanges(IntPtr ranges)
3030
public void AddText(string text)
3131
{
3232
byte* native_text;
33+
int text_byteCount = 0;
3334
if (text != null)
3435
{
35-
int text_byteCount = Encoding.UTF8.GetByteCount(text);
36-
byte* native_text_stackBytes = stackalloc byte[text_byteCount + 1];
37-
native_text = native_text_stackBytes;
38-
fixed (char* text_ptr = text)
36+
text_byteCount = Encoding.UTF8.GetByteCount(text);
37+
if (text_byteCount > Util.StackAllocationSizeLimit)
3938
{
40-
int native_text_offset = Encoding.UTF8.GetBytes(text_ptr, text.Length, native_text, text_byteCount);
41-
native_text[native_text_offset] = 0;
39+
native_text = Util.Allocate(text_byteCount + 1);
4240
}
41+
else
42+
{
43+
byte* native_text_stackBytes = stackalloc byte[text_byteCount + 1];
44+
native_text = native_text_stackBytes;
45+
}
46+
int native_text_offset = Util.GetUtf8(text, native_text, text_byteCount);
47+
native_text[native_text_offset] = 0;
4348
}
4449
else { native_text = null; }
4550
byte* native_text_end = null;
4651
ImGuiNative.GlyphRangesBuilder_AddText(NativePtr, native_text, native_text_end);
52+
if (text_byteCount > Util.StackAllocationSizeLimit)
53+
{
54+
Util.Free(native_text);
55+
}
4756
}
4857
public void BuildRanges(out ImVector out_ranges)
4958
{

src/ImGui.NET/Generated/ImFontAtlas.gen.cs

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -85,121 +85,175 @@ public ImFontPtr AddFontDefault(ImFontConfigPtr font_cfg)
8585
public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels)
8686
{
8787
byte* native_filename;
88+
int filename_byteCount = 0;
8889
if (filename != null)
8990
{
90-
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
91-
byte* native_filename_stackBytes = stackalloc byte[filename_byteCount + 1];
92-
native_filename = native_filename_stackBytes;
93-
fixed (char* filename_ptr = filename)
91+
filename_byteCount = Encoding.UTF8.GetByteCount(filename);
92+
if (filename_byteCount > Util.StackAllocationSizeLimit)
9493
{
95-
int native_filename_offset = Encoding.UTF8.GetBytes(filename_ptr, filename.Length, native_filename, filename_byteCount);
96-
native_filename[native_filename_offset] = 0;
94+
native_filename = Util.Allocate(filename_byteCount + 1);
9795
}
96+
else
97+
{
98+
byte* native_filename_stackBytes = stackalloc byte[filename_byteCount + 1];
99+
native_filename = native_filename_stackBytes;
100+
}
101+
int native_filename_offset = Util.GetUtf8(filename, native_filename, filename_byteCount);
102+
native_filename[native_filename_offset] = 0;
98103
}
99104
else { native_filename = null; }
100105
ImFontConfig* font_cfg = null;
101106
ushort* glyph_ranges = null;
102107
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(NativePtr, native_filename, size_pixels, font_cfg, glyph_ranges);
108+
if (filename_byteCount > Util.StackAllocationSizeLimit)
109+
{
110+
Util.Free(native_filename);
111+
}
103112
return new ImFontPtr(ret);
104113
}
105114
public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels, ImFontConfigPtr font_cfg)
106115
{
107116
byte* native_filename;
117+
int filename_byteCount = 0;
108118
if (filename != null)
109119
{
110-
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
111-
byte* native_filename_stackBytes = stackalloc byte[filename_byteCount + 1];
112-
native_filename = native_filename_stackBytes;
113-
fixed (char* filename_ptr = filename)
120+
filename_byteCount = Encoding.UTF8.GetByteCount(filename);
121+
if (filename_byteCount > Util.StackAllocationSizeLimit)
122+
{
123+
native_filename = Util.Allocate(filename_byteCount + 1);
124+
}
125+
else
114126
{
115-
int native_filename_offset = Encoding.UTF8.GetBytes(filename_ptr, filename.Length, native_filename, filename_byteCount);
116-
native_filename[native_filename_offset] = 0;
127+
byte* native_filename_stackBytes = stackalloc byte[filename_byteCount + 1];
128+
native_filename = native_filename_stackBytes;
117129
}
130+
int native_filename_offset = Util.GetUtf8(filename, native_filename, filename_byteCount);
131+
native_filename[native_filename_offset] = 0;
118132
}
119133
else { native_filename = null; }
120134
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
121135
ushort* glyph_ranges = null;
122136
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(NativePtr, native_filename, size_pixels, native_font_cfg, glyph_ranges);
137+
if (filename_byteCount > Util.StackAllocationSizeLimit)
138+
{
139+
Util.Free(native_filename);
140+
}
123141
return new ImFontPtr(ret);
124142
}
125143
public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels, ImFontConfigPtr font_cfg, IntPtr glyph_ranges)
126144
{
127145
byte* native_filename;
146+
int filename_byteCount = 0;
128147
if (filename != null)
129148
{
130-
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
131-
byte* native_filename_stackBytes = stackalloc byte[filename_byteCount + 1];
132-
native_filename = native_filename_stackBytes;
133-
fixed (char* filename_ptr = filename)
149+
filename_byteCount = Encoding.UTF8.GetByteCount(filename);
150+
if (filename_byteCount > Util.StackAllocationSizeLimit)
151+
{
152+
native_filename = Util.Allocate(filename_byteCount + 1);
153+
}
154+
else
134155
{
135-
int native_filename_offset = Encoding.UTF8.GetBytes(filename_ptr, filename.Length, native_filename, filename_byteCount);
136-
native_filename[native_filename_offset] = 0;
156+
byte* native_filename_stackBytes = stackalloc byte[filename_byteCount + 1];
157+
native_filename = native_filename_stackBytes;
137158
}
159+
int native_filename_offset = Util.GetUtf8(filename, native_filename, filename_byteCount);
160+
native_filename[native_filename_offset] = 0;
138161
}
139162
else { native_filename = null; }
140163
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
141164
ushort* native_glyph_ranges = (ushort*)glyph_ranges.ToPointer();
142165
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(NativePtr, native_filename, size_pixels, native_font_cfg, native_glyph_ranges);
166+
if (filename_byteCount > Util.StackAllocationSizeLimit)
167+
{
168+
Util.Free(native_filename);
169+
}
143170
return new ImFontPtr(ret);
144171
}
145172
public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_data_base85, float size_pixels)
146173
{
147174
byte* native_compressed_font_data_base85;
175+
int compressed_font_data_base85_byteCount = 0;
148176
if (compressed_font_data_base85 != null)
149177
{
150-
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
151-
byte* native_compressed_font_data_base85_stackBytes = stackalloc byte[compressed_font_data_base85_byteCount + 1];
152-
native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes;
153-
fixed (char* compressed_font_data_base85_ptr = compressed_font_data_base85)
178+
compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
179+
if (compressed_font_data_base85_byteCount > Util.StackAllocationSizeLimit)
154180
{
155-
int native_compressed_font_data_base85_offset = Encoding.UTF8.GetBytes(compressed_font_data_base85_ptr, compressed_font_data_base85.Length, native_compressed_font_data_base85, compressed_font_data_base85_byteCount);
156-
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
181+
native_compressed_font_data_base85 = Util.Allocate(compressed_font_data_base85_byteCount + 1);
157182
}
183+
else
184+
{
185+
byte* native_compressed_font_data_base85_stackBytes = stackalloc byte[compressed_font_data_base85_byteCount + 1];
186+
native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes;
187+
}
188+
int native_compressed_font_data_base85_offset = Util.GetUtf8(compressed_font_data_base85, native_compressed_font_data_base85, compressed_font_data_base85_byteCount);
189+
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
158190
}
159191
else { native_compressed_font_data_base85 = null; }
160192
ImFontConfig* font_cfg = null;
161193
ushort* glyph_ranges = null;
162194
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(NativePtr, native_compressed_font_data_base85, size_pixels, font_cfg, glyph_ranges);
195+
if (compressed_font_data_base85_byteCount > Util.StackAllocationSizeLimit)
196+
{
197+
Util.Free(native_compressed_font_data_base85);
198+
}
163199
return new ImFontPtr(ret);
164200
}
165201
public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_data_base85, float size_pixels, ImFontConfigPtr font_cfg)
166202
{
167203
byte* native_compressed_font_data_base85;
204+
int compressed_font_data_base85_byteCount = 0;
168205
if (compressed_font_data_base85 != null)
169206
{
170-
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
171-
byte* native_compressed_font_data_base85_stackBytes = stackalloc byte[compressed_font_data_base85_byteCount + 1];
172-
native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes;
173-
fixed (char* compressed_font_data_base85_ptr = compressed_font_data_base85)
207+
compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
208+
if (compressed_font_data_base85_byteCount > Util.StackAllocationSizeLimit)
209+
{
210+
native_compressed_font_data_base85 = Util.Allocate(compressed_font_data_base85_byteCount + 1);
211+
}
212+
else
174213
{
175-
int native_compressed_font_data_base85_offset = Encoding.UTF8.GetBytes(compressed_font_data_base85_ptr, compressed_font_data_base85.Length, native_compressed_font_data_base85, compressed_font_data_base85_byteCount);
176-
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
214+
byte* native_compressed_font_data_base85_stackBytes = stackalloc byte[compressed_font_data_base85_byteCount + 1];
215+
native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes;
177216
}
217+
int native_compressed_font_data_base85_offset = Util.GetUtf8(compressed_font_data_base85, native_compressed_font_data_base85, compressed_font_data_base85_byteCount);
218+
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
178219
}
179220
else { native_compressed_font_data_base85 = null; }
180221
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
181222
ushort* glyph_ranges = null;
182223
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(NativePtr, native_compressed_font_data_base85, size_pixels, native_font_cfg, glyph_ranges);
224+
if (compressed_font_data_base85_byteCount > Util.StackAllocationSizeLimit)
225+
{
226+
Util.Free(native_compressed_font_data_base85);
227+
}
183228
return new ImFontPtr(ret);
184229
}
185230
public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_data_base85, float size_pixels, ImFontConfigPtr font_cfg, IntPtr glyph_ranges)
186231
{
187232
byte* native_compressed_font_data_base85;
233+
int compressed_font_data_base85_byteCount = 0;
188234
if (compressed_font_data_base85 != null)
189235
{
190-
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
191-
byte* native_compressed_font_data_base85_stackBytes = stackalloc byte[compressed_font_data_base85_byteCount + 1];
192-
native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes;
193-
fixed (char* compressed_font_data_base85_ptr = compressed_font_data_base85)
236+
compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
237+
if (compressed_font_data_base85_byteCount > Util.StackAllocationSizeLimit)
238+
{
239+
native_compressed_font_data_base85 = Util.Allocate(compressed_font_data_base85_byteCount + 1);
240+
}
241+
else
194242
{
195-
int native_compressed_font_data_base85_offset = Encoding.UTF8.GetBytes(compressed_font_data_base85_ptr, compressed_font_data_base85.Length, native_compressed_font_data_base85, compressed_font_data_base85_byteCount);
196-
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
243+
byte* native_compressed_font_data_base85_stackBytes = stackalloc byte[compressed_font_data_base85_byteCount + 1];
244+
native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes;
197245
}
246+
int native_compressed_font_data_base85_offset = Util.GetUtf8(compressed_font_data_base85, native_compressed_font_data_base85, compressed_font_data_base85_byteCount);
247+
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
198248
}
199249
else { native_compressed_font_data_base85 = null; }
200250
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
201251
ushort* native_glyph_ranges = (ushort*)glyph_ranges.ToPointer();
202252
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(NativePtr, native_compressed_font_data_base85, size_pixels, native_font_cfg, native_glyph_ranges);
253+
if (compressed_font_data_base85_byteCount > Util.StackAllocationSizeLimit)
254+
{
255+
Util.Free(native_compressed_font_data_base85);
256+
}
203257
return new ImFontPtr(ret);
204258
}
205259
public ImFontPtr AddFontFromMemoryCompressedTTF(IntPtr compressed_font_data, int compressed_font_size, float size_pixels)

0 commit comments

Comments
 (0)