]> git.bitcoin.ninja Git - ldk-java/commitdiff
Convert primitives before passing to host language
authorMatt Corallo <git@bluematt.me>
Fri, 15 Apr 2022 16:11:46 +0000 (16:11 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 15 Apr 2022 16:15:43 +0000 (16:15 +0000)
This resolves an issue where we map unsigned primitives to their
signed variants, and occasionally pass the unsigned variant (which
is outside the bounds of the signed variant) to the host language.

In Java this can cause a panic as the VM sees an int outside the
range of a short (for example with network ports).

gen_type_mapping.py

index ad7d46fcfa5b8ce1628503f8e33fe6bac83c7b9e..9aeee61544a2ae479b91380b2e4119c97411d90f 100644 (file)
@@ -260,11 +260,19 @@ class TypeMappingGenerator:
                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
                     arg_conv = None, arg_conv_name = "arg", arg_conv_cleanup = None,
                     ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 8", to_hu_conv_name = None, from_hu_conv = None)
-        elif ty_info.is_native_primitive:
+        elif ty_info.is_native_primitive and ty_info.c_ty != "void":
             assert not is_nullable
             return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
-                arg_conv = None, arg_conv_name = ty_info.var_name, arg_conv_cleanup = None,
-                ret_conv = None, ret_conv_name = None, to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
+                arg_conv = None, arg_conv_name =  ty_info.var_name, arg_conv_cleanup = None,
+                ret_conv = (ty_info.c_ty + " " + ty_info.var_name + "_conv = ", ";"), ret_conv_name = ty_info.var_name + "_conv",
+                to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
+        elif ty_info.c_ty == "void":
+            assert ty_info.is_native_primitive
+            assert not is_nullable
+            return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
+                arg_conv = None, arg_conv_name =  ty_info.var_name, arg_conv_cleanup = None,
+                ret_conv = None, ret_conv_name = ty_info.var_name,
+                to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
         else:
             if ty_info.var_name == "":
                 ty_info.var_name = "ret"