]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
gdbus: Handle errors when extracting fds from dbus messages
authorCarlos Garnacho <carlosg@gnome.org>
Sun, 12 Feb 2017 22:10:05 +0000 (23:10 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 13 Feb 2017 11:41:19 +0000 (12:41 +0100)
On the right situations (eg. fd exhaustion), both
g_dbus_message_get_unix_fd_list() and g_unix_fd_list_get() should be
considered failable here. Add proper error handling to avoid triggering
glib warnings.

https://bugzilla.gnome.org/show_bug.cgi?id=778540

codegen/valagdbusclientmodule.vala
codegen/valagdbusmodule.vala
codegen/valagdbusservermodule.vala

index 6fdda8a50105b7a79d941dd5c13da1a38d9fe12a..ac45b58cd8cab01d9a8dae0c33d6828c427a06ca 100644 (file)
@@ -615,7 +615,6 @@ public class Vala.GDBusClientModule : GDBusModule {
                        ccode.add_expression (builder_init);
 
                        if (uses_fd) {
-                               ccode.add_declaration ("GUnixFDList", new CCodeVariableDeclarator ("*_fd_list"));
                                ccode.add_assignment (new CCodeIdentifier ("_fd_list"), new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_new")));
                        }
 
@@ -772,6 +771,8 @@ public class Vala.GDBusClientModule : GDBusModule {
 
                        if (uses_fd) {
                                ccode.add_declaration ("gint", new CCodeVariableDeclarator.zero ("_fd_index", new CCodeConstant ("0")));
+                               ccode.add_declaration ("GUnixFDList*", new CCodeVariableDeclarator ("_fd_list"));
+                               ccode.add_declaration ("gint", new CCodeVariableDeclarator ("_fd"));
                        }
 
                        foreach (Parameter param in m.get_parameters ()) {
index 8f10daa02124c662f78e91c225759e9d84752689..0a6404ad89884ce9a7a5321e72eca9c5b675c26d 100644 (file)
@@ -220,21 +220,42 @@ public class Vala.GDBusModule : GVariantModule {
                var fd_list = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_message_get_unix_fd_list"));
                fd_list.add_argument (message_expr);
 
-               var fd = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_get"));
-               fd.add_argument (fd_list);
-               fd.add_argument (new CCodeIdentifier ("_fd_index"));
-               fd.add_argument (new CCodeConstant ("NULL"));
+               var fd_var = new CCodeIdentifier ("_fd");
 
-               var stream = create_from_file_descriptor (type, fd);
+               var stream = create_from_file_descriptor (type, fd_var);
                if (stream != null) {
+                       var fd_list_var = new CCodeIdentifier ("_fd_list");
+
+                       var fd = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_get"));
+                       fd.add_argument (fd_list_var);
+                       fd.add_argument (new CCodeIdentifier ("_fd_index"));
+                       fd.add_argument (error_expr);
+
+                       ccode.add_assignment (fd_list_var, fd_list);
+                       ccode.open_if (fd_list_var);
+
                        var get_fd = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_iter_next"));
                        get_fd.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, iter_expr));
                        get_fd.add_argument (new CCodeConstant ("\"h\""));
                        get_fd.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_fd_index")));
                        ccode.add_expression (get_fd);
 
+                       ccode.add_assignment (fd_var, fd);
+                       ccode.open_if (new CCodeBinaryExpression (CCodeBinaryOperator.GREATER_THAN_OR_EQUAL, fd_var, new CCodeConstant ("0")));
+
                        ccode.add_assignment (target_expr, stream);
-                       may_fail = false;
+                       may_fail = true;
+
+                       ccode.close ();
+
+                       ccode.add_else ();
+                       var set_error = new CCodeFunctionCall (new CCodeIdentifier ("g_set_error_literal"));
+                       set_error.add_argument (error_expr);
+                       set_error.add_argument (new CCodeIdentifier ("G_IO_ERROR"));
+                       set_error.add_argument (new CCodeIdentifier ("G_IO_ERROR_FAILED"));
+                       set_error.add_argument (new CCodeConstant ("\"FD List is NULL\""));
+                       ccode.add_expression (set_error);
+                       ccode.close ();
                } else {
                        read_expression (type, iter_expr, target_expr, sym, error_expr, out may_fail);
                }
index 0df78c24c93159aa24e8ebc9b8d6a5133e0d90bc..e3cbc6e4f744d783b865f949e584aed3971940c5 100644 (file)
@@ -62,6 +62,7 @@ public class Vala.GDBusServerModule : GDBusClientModule {
                bool uses_fd = dbus_method_uses_file_descriptor (m);
                if (uses_fd) {
                        cfile.add_include ("gio/gunixfdlist.h");
+                       ccode.add_declaration ("GUnixFDList*", new CCodeVariableDeclarator ("_fd_list"));
                }
 
                bool uses_error = false;
@@ -93,6 +94,7 @@ public class Vala.GDBusServerModule : GDBusClientModule {
                if (!ready) {
                        if (uses_fd) {
                                ccode.add_declaration ("gint", new CCodeVariableDeclarator.zero ("_fd_index", new CCodeConstant ("0")));
+                               ccode.add_declaration ("gint", new CCodeVariableDeclarator ("_fd"));
                        }
 
                        foreach (Parameter param in m.get_parameters ()) {
@@ -257,7 +259,6 @@ public class Vala.GDBusServerModule : GDBusClientModule {
                        ccode.add_expression (builder_init);
 
                        if (uses_fd) {
-                               ccode.add_declaration ("GUnixFDList", new CCodeVariableDeclarator ("*_fd_list"));
                                ccode.add_assignment (new CCodeIdentifier ("_fd_list"), new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_new")));
                        }