Skip to content

Commit 7b75443

Browse files
committed
Only handle null objects when its allowed
This should result in hitting NPEs rather than in-native panics/SEGFAULTs.
1 parent fcd768b commit 7b75443

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

gen_type_mapping.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
295295
ty_info.var_name = "ret"
296296

297297
if ty_info.rust_obj in self.opaque_structs:
298-
from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name))
298+
if is_nullable:
299+
from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name))
300+
else:
301+
from_hu_conv = (self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name))
299302
opaque_arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
300303
opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.inner = untag_ptr(" + ty_info.var_name + ");\n"
301304
opaque_arg_conv += ty_info.var_name + "_conv.is_owned = ptr_is_owned(" + ty_info.var_name + ");\n"
@@ -310,8 +313,10 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
310313
# whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
311314
if holds_ref:
312315
opaque_arg_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);"
313-
else:
316+
elif is_nullable:
314317
from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + ty_info.var_name + ".clone_ptr()", "")
318+
else:
319+
from_hu_conv = (ty_info.var_name + ".clone_ptr()", "")
315320
elif ty_info.passed_as_ptr:
316321
opaque_arg_conv += "\n// WARNING: we need a move here but no clone is available for " + ty_info.rust_obj + "\n"
317322
# TODO: Once we support features cloning (which just isn't in C yet), we can make this a compile error instead!
@@ -421,7 +426,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
421426
if holds_ref:
422427
base_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone((" + ty_info.rust_obj + "*)untag_ptr(" + ty_info.var_name + "));"
423428
else:
424-
from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + ty_info.var_name + ".clone_ptr()", "")
429+
from_hu_conv = (ty_info.var_name + ".clone_ptr()", "")
425430
base_conv += "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
426431
elif needs_full_clone:
427432
base_conv = base_conv + "\n// WARNING: we may need a move here but no clone is available for " + ty_info.rust_obj
@@ -467,7 +472,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
467472
else:
468473
ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_conv = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_conv = ", ";")
469474
if from_hu_conv is None:
470-
from_hu_conv = (ty_info.var_name + " != null ? " + self.consts.get_ptr(ty_info.var_name) + " : " + self.consts.native_zero_ptr, "")
475+
from_hu_conv = (self.consts.get_ptr(ty_info.var_name), "")
471476
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
472477
arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
473478
ret_conv = ret_conv, ret_conv_name = "tag_ptr(" + ty_info.var_name + "_conv, true)",
@@ -493,7 +498,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
493498
else:
494499
to_hu_conv_sfx = ""
495500
if from_hu_conv is None:
496-
from_hu_conv = (ty_info.var_name + " != null ? " + self.consts.get_ptr(ty_info.var_name) + " : " + self.consts.native_zero_ptr, "")
501+
from_hu_conv = (self.consts.get_ptr(ty_info.var_name), "")
497502
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
498503
arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
499504
ret_conv = ret_conv, ret_conv_name = ret_conv_name,
@@ -546,7 +551,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
546551
ret_conv = ret_conv, ret_conv_name = "ref_" + ty_info.var_name,
547552
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_hu_conv", ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ")") + ";",
548553
to_hu_conv_name = ty_info.var_name + "_hu_conv",
549-
from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), from_hu_sfx))
554+
from_hu_conv = (self.consts.get_ptr(ty_info.var_name), from_hu_sfx))
550555
elif ty_info.rust_obj in self.trait_structs:
551556
if ty_info.nonnull_ptr:
552557
arg_conv = "void* " + ty_info.var_name + "_ptr = untag_ptr(" + ty_info.var_name + ");\n"
@@ -568,7 +573,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
568573
arg_conv += "\t*" + ty_info.var_name + "_conv_ptr = " + ty_info.var_name + "_conv;\n"
569574
arg_conv += "}"
570575
arg_conv_name = ty_info.var_name + "_conv_ptr"
571-
from_hu_conv_pfx = ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name)
576+
from_hu_conv_pfx = self.consts.get_ptr(ty_info.var_name)
572577
if ty_info.rust_obj.replace("LDK", "") + "_clone" in self.clone_fns:
573578
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
574579
arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,

0 commit comments

Comments
 (0)