Skip to content

Commit 57de7ca

Browse files
committed
Allow "null" to be passed for string parameters.
NOTE: Not all native functions permit a null string to be passed, and may still fail. This change just fixes the null-ref that occurred wheen attempting to marshal the null string to UTF8. Bumps beta identifier to beta3.
1 parent 9212f83 commit 57de7ca

10 files changed

Lines changed: 4808 additions & 2186 deletions

src/CodeGenerator/Program.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,19 @@ private static void EmitOverload(
623623
}
624624
else
625625
{
626-
preCallLines.Add($"int {correctedIdentifier}_byteCount = Encoding.UTF8.GetByteCount({textToEncode});");
627-
preCallLines.Add($"byte* {nativeArgName} = stackalloc byte[{correctedIdentifier}_byteCount + 1];");
628-
preCallLines.Add($"fixed (char* {correctedIdentifier}_ptr = {textToEncode})");
626+
preCallLines.Add($"byte* {nativeArgName};");
627+
preCallLines.Add($"if ({textToEncode} != null)");
629628
preCallLines.Add("{");
630-
preCallLines.Add($" int {nativeArgName}_offset = Encoding.UTF8.GetBytes({correctedIdentifier}_ptr, {textToEncode}.Length, {nativeArgName}, {correctedIdentifier}_byteCount);");
631-
preCallLines.Add($" {nativeArgName}[{nativeArgName}_offset] = 0;");
629+
preCallLines.Add($" int {correctedIdentifier}_byteCount = Encoding.UTF8.GetByteCount({textToEncode});");
630+
preCallLines.Add($" byte* {nativeArgName}_stackBytes = stackalloc byte[{correctedIdentifier}_byteCount + 1];");
631+
preCallLines.Add($" {nativeArgName} = {nativeArgName}_stackBytes;");
632+
preCallLines.Add($" fixed (char* {correctedIdentifier}_ptr = {textToEncode})");
633+
preCallLines.Add(" {");
634+
preCallLines.Add($" int {nativeArgName}_offset = Encoding.UTF8.GetBytes({correctedIdentifier}_ptr, {textToEncode}.Length, {nativeArgName}, {correctedIdentifier}_byteCount);");
635+
preCallLines.Add($" {nativeArgName}[{nativeArgName}_offset] = 0;");
636+
preCallLines.Add(" }");
632637
preCallLines.Add("}");
638+
preCallLines.Add($"else {{ {nativeArgName} = null; }}");
633639
}
634640
}
635641
else if (tr.Type == "char* []")

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ public void SetBit(int n)
2424
}
2525
public void AddText(string text)
2626
{
27-
int text_byteCount = Encoding.UTF8.GetByteCount(text);
28-
byte* native_text = stackalloc byte[text_byteCount + 1];
29-
fixed (char* text_ptr = text)
27+
byte* native_text;
28+
if (text != null)
3029
{
31-
int native_text_offset = Encoding.UTF8.GetBytes(text_ptr, text.Length, native_text, text_byteCount);
32-
native_text[native_text_offset] = 0;
30+
int text_byteCount = Encoding.UTF8.GetByteCount(text);
31+
byte* native_text_stackBytes = stackalloc byte[text_byteCount + 1];
32+
native_text = native_text_stackBytes;
33+
fixed (char* text_ptr = text)
34+
{
35+
int native_text_offset = Encoding.UTF8.GetBytes(text_ptr, text.Length, native_text, text_byteCount);
36+
native_text[native_text_offset] = 0;
37+
}
3338
}
39+
else { native_text = null; }
3440
byte* native_text_end = null;
3541
ImGuiNative.GlyphRangesBuilder_AddText(NativePtr, native_text, native_text_end);
3642
}

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

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,41 +48,59 @@ public unsafe partial struct ImFontAtlasPtr
4848
public RangeAccessor<int> CustomRectIds => new RangeAccessor<int>(NativePtr->CustomRectIds, 1);
4949
public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_data_base85, float size_pixels)
5050
{
51-
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
52-
byte* native_compressed_font_data_base85 = stackalloc byte[compressed_font_data_base85_byteCount + 1];
53-
fixed (char* compressed_font_data_base85_ptr = compressed_font_data_base85)
51+
byte* native_compressed_font_data_base85;
52+
if (compressed_font_data_base85 != null)
5453
{
55-
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);
56-
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
54+
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
55+
byte* native_compressed_font_data_base85_stackBytes = stackalloc byte[compressed_font_data_base85_byteCount + 1];
56+
native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes;
57+
fixed (char* compressed_font_data_base85_ptr = compressed_font_data_base85)
58+
{
59+
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);
60+
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
61+
}
5762
}
63+
else { native_compressed_font_data_base85 = null; }
5864
ImFontConfig* font_cfg = null;
5965
ushort* glyph_ranges = null;
6066
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(NativePtr, native_compressed_font_data_base85, size_pixels, font_cfg, glyph_ranges);
6167
return new ImFontPtr(ret);
6268
}
6369
public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_data_base85, float size_pixels, ImFontConfigPtr font_cfg)
6470
{
65-
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
66-
byte* native_compressed_font_data_base85 = stackalloc byte[compressed_font_data_base85_byteCount + 1];
67-
fixed (char* compressed_font_data_base85_ptr = compressed_font_data_base85)
71+
byte* native_compressed_font_data_base85;
72+
if (compressed_font_data_base85 != null)
6873
{
69-
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);
70-
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
74+
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
75+
byte* native_compressed_font_data_base85_stackBytes = stackalloc byte[compressed_font_data_base85_byteCount + 1];
76+
native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes;
77+
fixed (char* compressed_font_data_base85_ptr = compressed_font_data_base85)
78+
{
79+
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);
80+
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
81+
}
7182
}
83+
else { native_compressed_font_data_base85 = null; }
7284
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
7385
ushort* glyph_ranges = null;
7486
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(NativePtr, native_compressed_font_data_base85, size_pixels, native_font_cfg, glyph_ranges);
7587
return new ImFontPtr(ret);
7688
}
7789
public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_data_base85, float size_pixels, ImFontConfigPtr font_cfg, IntPtr glyph_ranges)
7890
{
79-
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
80-
byte* native_compressed_font_data_base85 = stackalloc byte[compressed_font_data_base85_byteCount + 1];
81-
fixed (char* compressed_font_data_base85_ptr = compressed_font_data_base85)
91+
byte* native_compressed_font_data_base85;
92+
if (compressed_font_data_base85 != null)
8293
{
83-
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);
84-
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
94+
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
95+
byte* native_compressed_font_data_base85_stackBytes = stackalloc byte[compressed_font_data_base85_byteCount + 1];
96+
native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes;
97+
fixed (char* compressed_font_data_base85_ptr = compressed_font_data_base85)
98+
{
99+
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);
100+
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
101+
}
85102
}
103+
else { native_compressed_font_data_base85 = null; }
86104
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
87105
ushort* native_glyph_ranges = (ushort*)glyph_ranges.ToPointer();
88106
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(NativePtr, native_compressed_font_data_base85, size_pixels, native_font_cfg, native_glyph_ranges);
@@ -218,41 +236,59 @@ public ImFontPtr AddFontFromMemoryTTF(IntPtr font_data, int font_size, float siz
218236
}
219237
public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels)
220238
{
221-
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
222-
byte* native_filename = stackalloc byte[filename_byteCount + 1];
223-
fixed (char* filename_ptr = filename)
239+
byte* native_filename;
240+
if (filename != null)
224241
{
225-
int native_filename_offset = Encoding.UTF8.GetBytes(filename_ptr, filename.Length, native_filename, filename_byteCount);
226-
native_filename[native_filename_offset] = 0;
242+
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
243+
byte* native_filename_stackBytes = stackalloc byte[filename_byteCount + 1];
244+
native_filename = native_filename_stackBytes;
245+
fixed (char* filename_ptr = filename)
246+
{
247+
int native_filename_offset = Encoding.UTF8.GetBytes(filename_ptr, filename.Length, native_filename, filename_byteCount);
248+
native_filename[native_filename_offset] = 0;
249+
}
227250
}
251+
else { native_filename = null; }
228252
ImFontConfig* font_cfg = null;
229253
ushort* glyph_ranges = null;
230254
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(NativePtr, native_filename, size_pixels, font_cfg, glyph_ranges);
231255
return new ImFontPtr(ret);
232256
}
233257
public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels, ImFontConfigPtr font_cfg)
234258
{
235-
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
236-
byte* native_filename = stackalloc byte[filename_byteCount + 1];
237-
fixed (char* filename_ptr = filename)
259+
byte* native_filename;
260+
if (filename != null)
238261
{
239-
int native_filename_offset = Encoding.UTF8.GetBytes(filename_ptr, filename.Length, native_filename, filename_byteCount);
240-
native_filename[native_filename_offset] = 0;
262+
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
263+
byte* native_filename_stackBytes = stackalloc byte[filename_byteCount + 1];
264+
native_filename = native_filename_stackBytes;
265+
fixed (char* filename_ptr = filename)
266+
{
267+
int native_filename_offset = Encoding.UTF8.GetBytes(filename_ptr, filename.Length, native_filename, filename_byteCount);
268+
native_filename[native_filename_offset] = 0;
269+
}
241270
}
271+
else { native_filename = null; }
242272
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
243273
ushort* glyph_ranges = null;
244274
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(NativePtr, native_filename, size_pixels, native_font_cfg, glyph_ranges);
245275
return new ImFontPtr(ret);
246276
}
247277
public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels, ImFontConfigPtr font_cfg, IntPtr glyph_ranges)
248278
{
249-
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
250-
byte* native_filename = stackalloc byte[filename_byteCount + 1];
251-
fixed (char* filename_ptr = filename)
279+
byte* native_filename;
280+
if (filename != null)
252281
{
253-
int native_filename_offset = Encoding.UTF8.GetBytes(filename_ptr, filename.Length, native_filename, filename_byteCount);
254-
native_filename[native_filename_offset] = 0;
282+
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
283+
byte* native_filename_stackBytes = stackalloc byte[filename_byteCount + 1];
284+
native_filename = native_filename_stackBytes;
285+
fixed (char* filename_ptr = filename)
286+
{
287+
int native_filename_offset = Encoding.UTF8.GetBytes(filename_ptr, filename.Length, native_filename, filename_byteCount);
288+
native_filename[native_filename_offset] = 0;
289+
}
255290
}
291+
else { native_filename = null; }
256292
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
257293
ushort* native_glyph_ranges = (ushort*)glyph_ranges.ToPointer();
258294
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(NativePtr, native_filename, size_pixels, native_font_cfg, native_glyph_ranges);

0 commit comments

Comments
 (0)