]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
gdbus: Type-check properties
authorSergey Bugaev <bugaevc@gmail.com>
Sun, 8 Mar 2026 10:19:12 +0000 (13:19 +0300)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 16 May 2026 11:30:15 +0000 (13:30 +0200)
codegen/valagdbusclientmodule.vala
tests/dbus/arrays_client.c-expected
tests/dbus/basic-types_client.c-expected
tests/dbus/property-access_client.c-expected
tests/dbus/rawvariants_client.c-expected
tests/dbus/structs_client.c-expected

index 7f639a1c515d159dde48add16af08bbadf7dbd99..cb63f8ec3ad32ab5c176dda7f38c3b6415ea6af1 100644 (file)
@@ -966,7 +966,20 @@ public class Vala.GDBusClientModule : GDBusModule {
 
                // return on error
                ccode.open_if (new CCodeUnaryExpression (CCodeUnaryOperator.LOGICAL_NEGATION, new CCodeIdentifier ("_reply")));
-               return_default_value (prop.property_type);
+               return_default_value (prop.property_type, true);
+               ccode.close ();
+
+               var unref_reply = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_unref"));
+               unref_reply.add_argument (new CCodeIdentifier ("_reply"));
+
+               var ccheck = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_is_of_type"));
+               ccheck.add_argument (new CCodeIdentifier ("_reply"));
+               ccheck.add_argument (new CCodeConstant ("G_VARIANT_TYPE (\"(v)\")"));
+               var cunlikely_check = new CCodeFunctionCall (new CCodeIdentifier ("G_UNLIKELY"));
+               cunlikely_check.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.LOGICAL_NEGATION, ccheck));
+               ccode.open_if (cunlikely_check);
+               ccode.add_expression (unref_reply);
+               return_default_value (prop.property_type, true);
                ccode.close ();
 
                var get_variant = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_get"));
@@ -975,12 +988,35 @@ public class Vala.GDBusClientModule : GDBusModule {
                get_variant.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_inner_reply")));
                ccode.add_expression (get_variant);
 
-               var unref_reply = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_unref"));
-               unref_reply.add_argument (new CCodeIdentifier ("_reply"));
                ccode.add_expression (unref_reply);
 
                ccode.close ();
 
+               // Type-check the value.
+               var expected_signature = prop.property_type.get_type_signature (prop);
+               ccheck = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_is_of_type"));
+               ccheck.add_argument (new CCodeIdentifier ("_inner_reply"));
+               ccheck.add_argument (new CCodeConstant ("G_VARIANT_TYPE (\"%s\")".printf (expected_signature)));
+               cunlikely_check = new CCodeFunctionCall (new CCodeIdentifier ("G_UNLIKELY"));
+               cunlikely_check.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.LOGICAL_NEGATION, ccheck));
+               ccode.open_if (cunlikely_check);
+
+               var cwarning = new CCodeFunctionCall (new CCodeIdentifier ("g_warning"));
+               cwarning.add_argument (new CCodeConstant ("\"Expected property %s.%s to be of type '%s', but received '%s'\""));
+               cwarning.add_argument (new CCodeConstant ("\"%s\"".printf (dbus_iface_name)));
+               cwarning.add_argument (new CCodeConstant ("\"%s\"".printf (get_dbus_name_for_member (prop))));
+               cwarning.add_argument (new CCodeConstant ("\"%s\"".printf (expected_signature)));
+               var cget_type = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_get_type_string"));
+               cget_type.add_argument (new CCodeIdentifier ("_inner_reply"));
+               cwarning.add_argument (cget_type);
+               ccode.add_expression (cwarning);
+
+               unref_reply = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_unref"));
+               unref_reply.add_argument (new CCodeIdentifier ("_inner_reply"));
+               ccode.add_expression (unref_reply);
+               return_default_value (prop.property_type, true);
+               ccode.close ();
+
                if (prop.property_type.is_real_non_null_struct_type ()) {
                        var target = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("result"));
                        var result = deserialize_expression (prop.get_accessor.value_type, new CCodeIdentifier ("_inner_reply"), target);
index 4379c7fe6f5079d485e89333ab8cb60389cb502b..86f64229346633795f417da8d68e3b919705b041 100644 (file)
@@ -482,9 +482,18 @@ test_dbus_proxy_get_test_property (Test* self,
                if (!_reply) {
                        return NULL;
                }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       g_variant_unref (_reply);
+                       return NULL;
+               }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("as")))) {
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "TestProperty", "as", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return NULL;
+       }
        _result_length1 = 0;
        _tmp22_ = g_new (gchar*, 5);
        _tmp22__length = 0;
index f661f4dec27aad6360506c40a0968cb2f0e45c7a..5476ac7a5d083fcd2358b5f490502680bf1fc0e8 100644 (file)
@@ -504,9 +504,18 @@ test_dbus_proxy_get_test_property (Test* self)
                if (!_reply) {
                        return NULL;
                }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       g_variant_unref (_reply);
+                       return NULL;
+               }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("s")))) {
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "TestProperty", "s", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return NULL;
+       }
        _result = g_variant_dup_string (_inner_reply, NULL);
        g_variant_unref (_inner_reply);
        return _result;
@@ -549,12 +558,23 @@ test_dbus_proxy_get_test_int_property (Test* self)
                _arguments = g_variant_builder_end (&_arguments_builder);
                _reply = g_dbus_proxy_call_sync ((GDBusProxy *) self, "org.freedesktop.DBus.Properties.Get", _arguments, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
                if (!_reply) {
-                       gint _tmp8_ = 0;
+                       gint _tmp8_ = -1;
                        return _tmp8_;
                }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       gint _tmp9_ = -1;
+                       g_variant_unref (_reply);
+                       return _tmp9_;
+               }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("i")))) {
+               gint _tmp10_ = -1;
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "TestIntProperty", "i", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return _tmp10_;
+       }
        _result = g_variant_get_int32 (_inner_reply);
        g_variant_unref (_inner_reply);
        return _result;
@@ -599,9 +619,18 @@ test_dbus_proxy_get_test_signature_property (Test* self)
                if (!_reply) {
                        return NULL;
                }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       g_variant_unref (_reply);
+                       return NULL;
+               }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("g")))) {
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "TestSignatureProperty", "g", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return NULL;
+       }
        _result = g_variant_type_new (g_variant_get_string (_inner_reply, NULL));
        g_variant_unref (_inner_reply);
        return _result;
@@ -614,13 +643,13 @@ test_dbus_proxy_set_test_signature_property (Test* self,
        GVariant *_arguments;
        GVariant *_reply;
        GVariantBuilder _arguments_builder;
-       gchar* _tmp9_;
+       gchar* _tmp11_;
        g_variant_builder_init (&_arguments_builder, G_VARIANT_TYPE_TUPLE);
        g_variant_builder_add_value (&_arguments_builder, g_variant_new_string ("org.example.Test"));
        g_variant_builder_add_value (&_arguments_builder, g_variant_new_string ("TestSignatureProperty"));
        g_variant_builder_open (&_arguments_builder, G_VARIANT_TYPE_VARIANT);
-       _tmp9_ = g_variant_type_dup_string (value);
-       g_variant_builder_add_value (&_arguments_builder, g_variant_new_from_data (G_VARIANT_TYPE_SIGNATURE, _tmp9_, strlen (_tmp9_) + 1, TRUE, g_free, _tmp9_));
+       _tmp11_ = g_variant_type_dup_string (value);
+       g_variant_builder_add_value (&_arguments_builder, g_variant_new_from_data (G_VARIANT_TYPE_SIGNATURE, _tmp11_, strlen (_tmp11_) + 1, TRUE, g_free, _tmp11_));
        g_variant_builder_close (&_arguments_builder);
        _arguments = g_variant_builder_end (&_arguments_builder);
        _reply = g_dbus_proxy_call_sync ((GDBusProxy *) self, "org.freedesktop.DBus.Properties.Set", _arguments, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
@@ -678,16 +707,16 @@ _dbus_test_test_int (Test* self,
        GError* error = NULL;
        GVariantIter _arguments_iter;
        gint i = 0;
-       GVariant* _tmp10_;
+       GVariant* _tmp12_;
        GDBusMessage* _reply_message = NULL;
        GVariant* _reply;
        GVariantBuilder _reply_builder;
        gint j = 0;
        gint result;
        g_variant_iter_init (&_arguments_iter, _parameters_);
-       _tmp10_ = g_variant_iter_next_value (&_arguments_iter);
-       i = g_variant_get_int32 (_tmp10_);
-       g_variant_unref (_tmp10_);
+       _tmp12_ = g_variant_iter_next_value (&_arguments_iter);
+       i = g_variant_get_int32 (_tmp12_);
+       g_variant_unref (_tmp12_);
        result = test_test_int (self, i, &j, &error);
        if (error) {
                g_dbus_method_invocation_take_error (invocation, error);
@@ -712,16 +741,16 @@ _dbus_test_test_string (Test* self,
        GError* error = NULL;
        GVariantIter _arguments_iter;
        gchar* s = NULL;
-       GVariant* _tmp11_;
+       GVariant* _tmp13_;
        GDBusMessage* _reply_message = NULL;
        GVariant* _reply;
        GVariantBuilder _reply_builder;
        gchar* t = NULL;
        gchar* result;
        g_variant_iter_init (&_arguments_iter, _parameters_);
-       _tmp11_ = g_variant_iter_next_value (&_arguments_iter);
-       s = g_variant_dup_string (_tmp11_, NULL);
-       g_variant_unref (_tmp11_);
+       _tmp13_ = g_variant_iter_next_value (&_arguments_iter);
+       s = g_variant_dup_string (_tmp13_, NULL);
+       g_variant_unref (_tmp13_);
        result = test_test_string (self, s, &t, &error);
        if (error) {
                g_dbus_method_invocation_take_error (invocation, error);
@@ -751,18 +780,18 @@ _dbus_test_test_signature (Test* self,
        GError* error = NULL;
        GVariantIter _arguments_iter;
        GVariantType* tp1 = NULL;
-       GVariant* _tmp12_;
+       GVariant* _tmp14_;
        GDBusMessage* _reply_message = NULL;
        GVariant* _reply;
        GVariantBuilder _reply_builder;
        GVariantType* tp2 = NULL;
-       gchar* _tmp13_;
+       gchar* _tmp15_;
        GVariantType* result;
-       gchar* _tmp14_;
+       gchar* _tmp16_;
        g_variant_iter_init (&_arguments_iter, _parameters_);
-       _tmp12_ = g_variant_iter_next_value (&_arguments_iter);
-       tp1 = g_variant_type_new (g_variant_get_string (_tmp12_, NULL));
-       g_variant_unref (_tmp12_);
+       _tmp14_ = g_variant_iter_next_value (&_arguments_iter);
+       tp1 = g_variant_type_new (g_variant_get_string (_tmp14_, NULL));
+       g_variant_unref (_tmp14_);
        result = test_test_signature (self, tp1, &tp2, &error);
        if (error) {
                g_dbus_method_invocation_take_error (invocation, error);
@@ -770,10 +799,10 @@ _dbus_test_test_signature (Test* self,
        }
        _reply_message = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
        g_variant_builder_init (&_reply_builder, G_VARIANT_TYPE_TUPLE);
-       _tmp13_ = g_variant_type_dup_string (tp2);
-       g_variant_builder_add_value (&_reply_builder, g_variant_new_from_data (G_VARIANT_TYPE_SIGNATURE, _tmp13_, strlen (_tmp13_) + 1, TRUE, g_free, _tmp13_));
-       _tmp14_ = g_variant_type_dup_string (result);
-       g_variant_builder_add_value (&_reply_builder, g_variant_new_from_data (G_VARIANT_TYPE_SIGNATURE, _tmp14_, strlen (_tmp14_) + 1, TRUE, g_free, _tmp14_));
+       _tmp15_ = g_variant_type_dup_string (tp2);
+       g_variant_builder_add_value (&_reply_builder, g_variant_new_from_data (G_VARIANT_TYPE_SIGNATURE, _tmp15_, strlen (_tmp15_) + 1, TRUE, g_free, _tmp15_));
+       _tmp16_ = g_variant_type_dup_string (result);
+       g_variant_builder_add_value (&_reply_builder, g_variant_new_from_data (G_VARIANT_TYPE_SIGNATURE, _tmp16_, strlen (_tmp16_) + 1, TRUE, g_free, _tmp16_));
        _g_variant_type_free0 (result);
        _reply = g_variant_builder_end (&_reply_builder);
        g_dbus_message_set_body (_reply_message, _reply);
@@ -839,10 +868,10 @@ _dbus_test_get_test_signature_property (Test* self)
 {
        GVariantType* result;
        GVariant* _reply;
-       gchar* _tmp15_;
+       gchar* _tmp17_;
        result = test_get_test_signature_property (self);
-       _tmp15_ = g_variant_type_dup_string (result);
-       _reply = g_variant_new_from_data (G_VARIANT_TYPE_SIGNATURE, _tmp15_, strlen (_tmp15_) + 1, TRUE, g_free, _tmp15_);
+       _tmp17_ = g_variant_type_dup_string (result);
+       _reply = g_variant_new_from_data (G_VARIANT_TYPE_SIGNATURE, _tmp17_, strlen (_tmp17_) + 1, TRUE, g_free, _tmp17_);
        _g_variant_type_free0 (result);
        return _reply;
 }
index f2094788399ecad44cc49bd5c7f35ab59dbba451..1e3b284c4432fc50f8db735033ae23caa6ad5078 100644 (file)
@@ -282,12 +282,23 @@ test_dbus_proxy_get_construct_only (Test* self)
                _arguments = g_variant_builder_end (&_arguments_builder);
                _reply = g_dbus_proxy_call_sync ((GDBusProxy *) self, "org.freedesktop.DBus.Properties.Get", _arguments, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
                if (!_reply) {
-                       gint _tmp0_ = 0;
+                       gint _tmp0_ = -1;
                        return _tmp0_;
                }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       gint _tmp1_ = -1;
+                       g_variant_unref (_reply);
+                       return _tmp1_;
+               }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("i")))) {
+               gint _tmp2_ = -1;
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "ConstructOnly", "i", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return _tmp2_;
+       }
        _result = g_variant_get_int32 (_inner_reply);
        g_variant_unref (_inner_reply);
        return _result;
@@ -309,12 +320,23 @@ test_dbus_proxy_get_read (Test* self)
                _arguments = g_variant_builder_end (&_arguments_builder);
                _reply = g_dbus_proxy_call_sync ((GDBusProxy *) self, "org.freedesktop.DBus.Properties.Get", _arguments, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
                if (!_reply) {
-                       gint _tmp1_ = 0;
-                       return _tmp1_;
+                       gint _tmp3_ = -1;
+                       return _tmp3_;
+               }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       gint _tmp4_ = -1;
+                       g_variant_unref (_reply);
+                       return _tmp4_;
                }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("i")))) {
+               gint _tmp5_ = -1;
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "Read", "i", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return _tmp5_;
+       }
        _result = g_variant_get_int32 (_inner_reply);
        g_variant_unref (_inner_reply);
        return _result;
@@ -357,12 +379,23 @@ test_dbus_proxy_get_readwrite (Test* self)
                _arguments = g_variant_builder_end (&_arguments_builder);
                _reply = g_dbus_proxy_call_sync ((GDBusProxy *) self, "org.freedesktop.DBus.Properties.Get", _arguments, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
                if (!_reply) {
-                       gint _tmp2_ = 0;
-                       return _tmp2_;
+                       gint _tmp6_ = -1;
+                       return _tmp6_;
+               }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       gint _tmp7_ = -1;
+                       g_variant_unref (_reply);
+                       return _tmp7_;
                }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("i")))) {
+               gint _tmp8_ = -1;
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "Readwrite", "i", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return _tmp8_;
+       }
        _result = g_variant_get_int32 (_inner_reply);
        g_variant_unref (_inner_reply);
        return _result;
index bf906958a2d227febc7806d27d0f66a1dec1eaef..c0b0986d9168bc80fbbe8153edbd8d838ead0521 100644 (file)
@@ -427,9 +427,18 @@ test_dbus_proxy_get_test_property (Test* self)
                if (!_reply) {
                        return NULL;
                }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       g_variant_unref (_reply);
+                       return NULL;
+               }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("s")))) {
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "TestProperty", "s", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return NULL;
+       }
        _result = g_variant_dup_string (_inner_reply, NULL);
        g_variant_unref (_inner_reply);
        return _result;
@@ -781,9 +790,18 @@ test_raw_dbus_proxy_get_test_property (TestRaw* self)
                if (!_reply) {
                        return NULL;
                }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       g_variant_unref (_reply);
+                       return NULL;
+               }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("s")))) {
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "TestProperty", "s", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return NULL;
+       }
        _result = _inner_reply;
        return _result;
 }
index 83e7f119e533809ef1ea1b0995ff57670f73b5b8..6980cd0dcc7cb659bbd33b4c576531214571d41d 100644 (file)
@@ -384,9 +384,18 @@ test_dbus_proxy_get_test_property (Test* self,
                if (!_reply) {
                        return;
                }
+               if (G_UNLIKELY (!g_variant_is_of_type (_reply, G_VARIANT_TYPE ("(v)")))) {
+                       g_variant_unref (_reply);
+                       return;
+               }
                g_variant_get (_reply, "(v)", &_inner_reply);
                g_variant_unref (_reply);
        }
+       if (G_UNLIKELY (!g_variant_is_of_type (_inner_reply, G_VARIANT_TYPE ("(is)")))) {
+               g_warning ("Expected property %s.%s to be of type '%s', but received '%s'", "org.example.Test", "TestProperty", "(is)", g_variant_get_type_string (_inner_reply));
+               g_variant_unref (_inner_reply);
+               return;
+       }
        g_variant_iter_init (&_tmp12_, _inner_reply);
        _tmp13_ = g_variant_iter_next_value (&_tmp12_);
        _tmp11_.i = g_variant_get_int32 (_tmp13_);