Skip to content

Commit a0db883

Browse files
committed
Implement function to throw on null pointers
• Specifically: checks if ptr == IntPtr.Zero
1 parent b94eb58 commit a0db883

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

modules/mono/editor/bindings_generator.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ StringBuilder &operator<<(StringBuilder &r_sb, const char *p_cstring) {
6969

7070
#define OPEN_BLOCK_L1 INDENT1 OPEN_BLOCK
7171
#define OPEN_BLOCK_L2 INDENT2 OPEN_BLOCK
72+
#define OPEN_BLOCK_L3 INDENT3 OPEN_BLOCK
7273
#define CLOSE_BLOCK_L1 INDENT1 CLOSE_BLOCK
7374
#define CLOSE_BLOCK_L2 INDENT2 CLOSE_BLOCK
7475
#define CLOSE_BLOCK_L3 INDENT3 CLOSE_BLOCK
@@ -2591,7 +2592,11 @@ Error BindingsGenerator::_generate_cs_native_calls(const InternalCall &p_icall,
25912592
// Generate icall function
25922593

25932594
r_output << MEMBER_BEGIN "internal static unsafe " << (ret_void ? "void" : return_type->c_type_out) << " "
2594-
<< icall_method << "(" << c_func_sig.as_string() << ") " OPEN_BLOCK;
2595+
<< icall_method << "(" << c_func_sig.as_string() << ")\n" OPEN_BLOCK_L1;
2596+
2597+
if (!p_icall.is_static) {
2598+
r_output << INDENT2 "ExceptionUtils.ThrowIfNullPtr(" CS_PARAM_INSTANCE ");\n";
2599+
}
25952600

25962601
if (!ret_void && (!p_icall.is_vararg || return_type->cname != name_cache.type_Variant)) {
25972602
String ptrcall_return_type;
@@ -2619,11 +2624,6 @@ Error BindingsGenerator::_generate_cs_native_calls(const InternalCall &p_icall,
26192624
r_output << ptrcall_return_type << " " C_LOCAL_RET << initialization << ";\n";
26202625
}
26212626

2622-
if (!p_icall.is_static) {
2623-
r_output << INDENT2 "if (" CS_PARAM_INSTANCE " == IntPtr.Zero)\n"
2624-
<< INDENT3 "throw new ArgumentNullException(nameof(" CS_PARAM_INSTANCE "));\n";
2625-
}
2626-
26272627
String argc_str = itos(p_icall.get_arguments_count());
26282628

26292629
auto generate_call_and_return_stmts = [&](const char *base_indent) {
@@ -2714,7 +2714,7 @@ Error BindingsGenerator::_generate_cs_native_calls(const InternalCall &p_icall,
27142714

27152715
r_output << c_in_statements.as_string();
27162716

2717-
r_output << INDENT3 "for (int i = 0; i < vararg_length; i++) " OPEN_BLOCK
2717+
r_output << INDENT3 "for (int i = 0; i < vararg_length; i++)\n" OPEN_BLOCK_L3
27182718
<< INDENT4 "varargs[i] = " << vararg_arg << "[i].NativeVar;\n"
27192719
<< INDENT4 C_LOCAL_PTRCALL_ARGS "[" << real_argc_str << " + i] = new IntPtr(&varargs[i]);\n"
27202720
<< CLOSE_BLOCK_L3;

modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Runtime.CompilerServices;
45
using System.Text;
56

67
#nullable enable
@@ -239,5 +240,13 @@ private unsafe static string GetVariantTypeName(godot_variant* variant)
239240

240241
return variant->Type.ToString();
241242
}
243+
244+
internal static void ThrowIfNullPtr(IntPtr ptr, [CallerArgumentExpression(nameof(ptr))] string? paramName = null)
245+
{
246+
if (ptr == IntPtr.Zero)
247+
{
248+
throw new ArgumentNullException(paramName);
249+
}
250+
}
242251
}
243252
}

0 commit comments

Comments
 (0)