From fd01afc12b4696dae92d6fa98a408e644f2117be Mon Sep 17 00:00:00 2001 From: Mike Kestner Date: Fri, 9 Mar 2007 14:22:43 +0000 Subject: [PATCH] 2007-03-08 Mike Kestner * glib/Signal.cs : guard against NULL gchandles. * gdk/Input.custom : add try/catch blocks to native callbacks. * gtk/*.custom : ditto * gtk/NodeStore.cs : ditto svn path=/trunk/gtk-sharp/; revision=74004 --- ChangeLog | 7 ++ gdk/Input.custom | 14 ++- glib/Signal.cs | 2 + gtk/CellRenderer.custom | 62 +++++++---- gtk/Clipboard.custom | 22 ++-- gtk/Container.custom | 32 ++++-- gtk/Input.custom | 12 +- gtk/NodeStore.cs | 240 ++++++++++++++++++++++++---------------- gtk/Widget.custom | 22 ++-- 9 files changed, 261 insertions(+), 152 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5e42fc444..27820eac7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2007-03-08 Mike Kestner + + * glib/Signal.cs : guard against NULL gchandles. + * gdk/Input.custom : add try/catch blocks to native callbacks. + * gtk/*.custom : ditto + * gtk/NodeStore.cs : ditto + 2007-03-08 Mike Kestner * glib/*.cs : add try/catch blocks to native callback diff --git a/gdk/Input.custom b/gdk/Input.custom index 1caf9a09c..bf9443e7d 100644 --- a/gdk/Input.custom +++ b/gdk/Input.custom @@ -2,7 +2,7 @@ // // Author: Mike Kestner // -// Copyright (C) 2005 Novell, Inc. +// Copyright (C) 2005, 2007 Novell, Inc. // // This code is inserted after the automatically generated code. // @@ -25,11 +25,15 @@ static void ReleaseGCHandle (IntPtr data) { - if (data == IntPtr.Zero) - return; + try { + if (data == IntPtr.Zero) + return; - GCHandle gch = (GCHandle) data; - gch.Free (); + GCHandle gch = (GCHandle) data; + gch.Free (); + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } static InputDestroyNotify release_gchandle; diff --git a/glib/Signal.cs b/glib/Signal.cs index fd43114fb..58beb146a 100644 --- a/glib/Signal.cs +++ b/glib/Signal.cs @@ -178,6 +178,8 @@ namespace GLib { static void voidObjectCallback (IntPtr handle, IntPtr gch) { try { + if (gch == IntPtr.Zero) + return; Signal sig = ((GCHandle) gch).Target as Signal; if (sig == null) { ExceptionManager.RaiseUnhandledException (new Exception ("Unknown signal class GC handle received."), false); diff --git a/gtk/CellRenderer.custom b/gtk/CellRenderer.custom index bc2d636e1..997155474 100644 --- a/gtk/CellRenderer.custom +++ b/gtk/CellRenderer.custom @@ -38,20 +38,24 @@ static void GetSize_cb (IntPtr item, IntPtr widget, IntPtr cell_area_ptr, IntPtr x_offset, IntPtr y_offset, IntPtr width, IntPtr height) { - CellRenderer obj = GLib.Object.GetObject (item, false) as CellRenderer; - Gtk.Widget widg = GLib.Object.GetObject (widget, false) as Gtk.Widget; - Gdk.Rectangle cell_area = Gdk.Rectangle.New (cell_area_ptr); - int a, b, c, d; + try { + CellRenderer obj = GLib.Object.GetObject (item, false) as CellRenderer; + Gtk.Widget widg = GLib.Object.GetObject (widget, false) as Gtk.Widget; + Gdk.Rectangle cell_area = Gdk.Rectangle.New (cell_area_ptr); + int a, b, c, d; - obj.GetSize (widg, ref cell_area, out a, out b, out c, out d); - if (x_offset != IntPtr.Zero) - Marshal.WriteInt32 (x_offset, a); - if (y_offset != IntPtr.Zero) - Marshal.WriteInt32 (y_offset, b); - if (width != IntPtr.Zero) - Marshal.WriteInt32 (width, c); - if (height != IntPtr.Zero) - Marshal.WriteInt32 (height, d); + obj.GetSize (widg, ref cell_area, out a, out b, out c, out d); + if (x_offset != IntPtr.Zero) + Marshal.WriteInt32 (x_offset, a); + if (y_offset != IntPtr.Zero) + Marshal.WriteInt32 (y_offset, b); + if (width != IntPtr.Zero) + Marshal.WriteInt32 (width, c); + if (height != IntPtr.Zero) + Marshal.WriteInt32 (height, d); + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } static void OverrideGetSize (GLib.GType gtype) @@ -88,10 +92,14 @@ static void Render_cb (IntPtr item, IntPtr window, IntPtr widget, ref Gdk.Rectangle background_area, ref Gdk.Rectangle cell_area, ref Gdk.Rectangle expose_area, Gtk.CellRendererState flags) { - CellRenderer obj = GLib.Object.GetObject (item, false) as CellRenderer; - Gdk.Drawable wind = GLib.Object.GetObject (window, false) as Gdk.Drawable; - Gtk.Widget widg = GLib.Object.GetObject (widget, false) as Gtk.Widget; - obj.Render (wind, widg, background_area, cell_area, expose_area, flags); + try { + CellRenderer obj = GLib.Object.GetObject (item, false) as CellRenderer; + Gdk.Drawable wind = GLib.Object.GetObject (window, false) as Gdk.Drawable; + Gtk.Widget widg = GLib.Object.GetObject (widget, false) as Gtk.Widget; + obj.Render (wind, widg, background_area, cell_area, expose_area, flags); + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } static void OverrideRender (GLib.GType gtype) @@ -125,13 +133,19 @@ static IntPtr StartEditing_cb (IntPtr raw, IntPtr evnt, IntPtr widget, IntPtr path, ref Gdk.Rectangle background_area, ref Gdk.Rectangle cell_area, Gtk.CellRendererState flags) { - CellRenderer obj = GLib.Object.GetObject (raw, false) as CellRenderer; - Gdk.Event _event = new Gdk.Event (evnt); - Widget widg = GLib.Object.GetObject (widget, false) as Gtk.Widget; - CellEditable retval = obj.StartEditing (_event, widg, GLib.Marshaller.Utf8PtrToString (path), background_area, cell_area, flags); - if (retval == null) - return IntPtr.Zero; - return retval.Handle; + try { + CellRenderer obj = GLib.Object.GetObject (raw, false) as CellRenderer; + Gdk.Event _event = new Gdk.Event (evnt); + Widget widg = GLib.Object.GetObject (widget, false) as Gtk.Widget; + CellEditable retval = obj.StartEditing (_event, widg, GLib.Marshaller.Utf8PtrToString (path), background_area, cell_area, flags); + if (retval == null) + return IntPtr.Zero; + return retval.Handle; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } + + return IntPtr.Zero; } static void OverrideStartEditing (GLib.GType gtype) diff --git a/gtk/Clipboard.custom b/gtk/Clipboard.custom index 4ae21f6fc..4199427bb 100644 --- a/gtk/Clipboard.custom +++ b/gtk/Clipboard.custom @@ -94,15 +94,19 @@ void RichTextReceivedCallback (IntPtr clipboard_ptr, IntPtr format_ptr, IntPtr text_ptr, UIntPtr length, IntPtr data) { - Gtk.Clipboard clipboard = GLib.Object.GetObject(clipboard_ptr) as Gtk.Clipboard; - Gdk.Atom format = format_ptr == IntPtr.Zero ? null : (Gdk.Atom) GLib.Opaque.GetOpaque (format_ptr, typeof (Gdk.Atom), false); - int sz = (int) (uint) length; - byte[] text = new byte [sz]; - Marshal.Copy (text, 0, text_ptr, sz); - GCHandle gch = (GCHandle) data; - RichTextReceivedFunc cb = gch.Target as RichTextReceivedFunc; - cb (clipboard, format, text); - gch.Free (); + try { + Gtk.Clipboard clipboard = GLib.Object.GetObject(clipboard_ptr) as Gtk.Clipboard; + Gdk.Atom format = format_ptr == IntPtr.Zero ? null : (Gdk.Atom) GLib.Opaque.GetOpaque (format_ptr, typeof (Gdk.Atom), false); + int sz = (int) (uint) length; + byte[] text = new byte [sz]; + Marshal.Copy (text, 0, text_ptr, sz); + GCHandle gch = (GCHandle) data; + RichTextReceivedFunc cb = gch.Target as RichTextReceivedFunc; + cb (clipboard, format, text); + gch.Free (); + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } [DllImport("libgtk-win32-2.0-0.dll")] diff --git a/gtk/Container.custom b/gtk/Container.custom index ecc787903..e6ddbed19 100644 --- a/gtk/Container.custom +++ b/gtk/Container.custom @@ -145,9 +145,13 @@ public struct CallbackInvoker { static void ForallOld_cb (IntPtr container, bool include_internals, IntPtr cb, IntPtr data) { - Container obj = GLib.Object.GetObject (container, false) as Container; - CallbackInvoker invoker = new CallbackInvoker (cb, data); - obj.ForAll (include_internals, invoker); + try { + Container obj = GLib.Object.GetObject (container, false) as Container; + CallbackInvoker invoker = new CallbackInvoker (cb, data); + obj.ForAll (include_internals, invoker); + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } static void OverrideForallOld (GLib.GType gtype) @@ -166,9 +170,13 @@ protected virtual void ForAll (bool include_internals, CallbackInvoker invoker) static void Forall_cb (IntPtr container, bool include_internals, IntPtr cb, IntPtr data) { - Container obj = GLib.Object.GetObject (container, false) as Container; - CallbackInvoker invoker = new CallbackInvoker (cb, data); - obj.ForAll (include_internals, new Gtk.Callback (invoker.Invoke)); + try { + Container obj = GLib.Object.GetObject (container, false) as Container; + CallbackInvoker invoker = new CallbackInvoker (cb, data); + obj.ForAll (include_internals, new Gtk.Callback (invoker.Invoke)); + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } static void OverrideForall (GLib.GType gtype) @@ -203,9 +211,15 @@ static ChildTypeDelegate ChildTypeCallback; static IntPtr ChildType_cb (IntPtr raw) { - Container obj = GLib.Object.GetObject (raw, false) as Container; - GLib.GType gtype = obj.ChildType (); - return gtype.Val; + try { + Container obj = GLib.Object.GetObject (raw, false) as Container; + GLib.GType gtype = obj.ChildType (); + return gtype.Val; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } + + return GLib.GType.Invalid.Val; } static void OverrideChildType (GLib.GType gtype) diff --git a/gtk/Input.custom b/gtk/Input.custom index 34788fb21..15e5e5f9d 100644 --- a/gtk/Input.custom +++ b/gtk/Input.custom @@ -29,10 +29,14 @@ public void NativeCallback (IntPtr data, int source, int condition) { - IntPtr _arg0 = data; - int _arg1 = source; - Gdk.InputCondition _arg2 = (Gdk.InputCondition) condition; - managed ( _arg0, _arg1, _arg2); + try { + IntPtr _arg0 = data; + int _arg1 = source; + Gdk.InputCondition _arg2 = (Gdk.InputCondition) condition; + managed ( _arg0, _arg1, _arg2); + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } internal InputFunctionNative NativeDelegate; diff --git a/gtk/NodeStore.cs b/gtk/NodeStore.cs index 60ab5da38..aae89bc9d 100644 --- a/gtk/NodeStore.cs +++ b/gtk/NodeStore.cs @@ -113,32 +113,49 @@ namespace Gtk { IntPtr get_column_type_cb (int col) { - return ctypes [col].Val; + try { + return ctypes [col].Val; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } + + return IntPtr.Zero; } bool get_node_cb (out int node_idx, IntPtr path) { - if (path == IntPtr.Zero) - throw new ArgumentNullException ("path"); + try { + if (path == IntPtr.Zero) + throw new ArgumentNullException ("path"); - TreePath treepath = new TreePath (path); + TreePath treepath = new TreePath (path); + node_idx = -1; + + ITreeNode node = GetNodeAtPath (treepath); + if (node == null) + return false; + + node_idx = node.ID; + node_hash [node.ID] = node; + return true; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } node_idx = -1; - - ITreeNode node = GetNodeAtPath (treepath); - if (node == null) - return false; - - node_idx = node.ID; - node_hash [node.ID] = node; - return true; + return false; } IntPtr get_path_cb (int node_idx) { - ITreeNode node = node_hash [node_idx] as ITreeNode; - if (node == null) throw new Exception ("Invalid Node ID"); + try { + ITreeNode node = node_hash [node_idx] as ITreeNode; + if (node == null) throw new Exception ("Invalid Node ID"); - return GetPath (node).Handle; + return GetPath (node).Handle; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } + return IntPtr.Zero; } [DllImport("libgobject-2.0-0.dll")] @@ -146,125 +163,160 @@ namespace Gtk { void get_value_cb (int node_idx, int col, ref GLib.Value val) { - ITreeNode node = node_hash [node_idx] as ITreeNode; - if (node == null) - return; - g_value_init (ref val, ctypes [col].Val); - object col_val; - if (getters [col] is PropertyInfo) - col_val = ((PropertyInfo) getters [col]).GetValue (node, null); - else - col_val = ((FieldInfo) getters [col]).GetValue (node); - val.Val = col_val; + try { + ITreeNode node = node_hash [node_idx] as ITreeNode; + if (node == null) + return; + g_value_init (ref val, ctypes [col].Val); + object col_val; + if (getters [col] is PropertyInfo) + col_val = ((PropertyInfo) getters [col]).GetValue (node, null); + else + col_val = ((FieldInfo) getters [col]).GetValue (node); + val.Val = col_val; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } bool next_cb (ref int node_idx) { - ITreeNode node = node_hash [node_idx] as ITreeNode; - if (node == null) - return false; + try { + ITreeNode node = node_hash [node_idx] as ITreeNode; + if (node == null) + return false; - int idx; - if (node.Parent == null) - idx = Nodes.IndexOf (node); - else - idx = node.Parent.IndexOf (node); + int idx; + if (node.Parent == null) + idx = Nodes.IndexOf (node); + else + idx = node.Parent.IndexOf (node); - if (idx < 0) throw new Exception ("Node not found in Nodes list"); + if (idx < 0) throw new Exception ("Node not found in Nodes list"); - if (node.Parent == null) { - if (++idx >= Nodes.Count) - return false; - node = Nodes [idx] as ITreeNode; - } else { - if (++idx >= node.Parent.ChildCount) - return false; - node = node.Parent [idx]; + if (node.Parent == null) { + if (++idx >= Nodes.Count) + return false; + node = Nodes [idx] as ITreeNode; + } else { + if (++idx >= node.Parent.ChildCount) + return false; + node = node.Parent [idx]; + } + node_hash [node.ID] = node; + node_idx = node.ID; + return true; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); } - node_hash [node.ID] = node; - node_idx = node.ID; - return true; + return false; } bool children_cb (out int child_idx, int parent) { - child_idx = -1; - ITreeNode node; + try { + child_idx = -1; + ITreeNode node; - if (parent == -1) { - if (Nodes.Count <= 0) - return false; - node = Nodes [0] as ITreeNode; - child_idx = node.ID; - node_hash [node.ID] = node; - return true; - } + if (parent == -1) { + if (Nodes.Count <= 0) + return false; + node = Nodes [0] as ITreeNode; + child_idx = node.ID; + node_hash [node.ID] = node; + return true; + } - node = node_hash [parent] as ITreeNode; - if (node == null || node.ChildCount <= 0) - return false; + node = node_hash [parent] as ITreeNode; + if (node == null || node.ChildCount <= 0) + return false; - ITreeNode child = node [0]; - node_hash [child.ID] = child; - child_idx = child.ID; - return true; + ITreeNode child = node [0]; + node_hash [child.ID] = child; + child_idx = child.ID; + return true; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } + child_idx = -1; + return false; } bool has_child_cb (int node_idx) { - ITreeNode node = node_hash [node_idx] as ITreeNode; - if (node == null || node.ChildCount <= 0) - return false; + try { + ITreeNode node = node_hash [node_idx] as ITreeNode; + if (node == null || node.ChildCount <= 0) + return false; - return true; + return true; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } + return false; } int n_children_cb (int node_idx) { - if (node_idx == -1) - return Nodes.Count; + try { + if (node_idx == -1) + return Nodes.Count; - ITreeNode node = node_hash [node_idx] as ITreeNode; - if (node == null || node.ChildCount <= 0) - return 0; + ITreeNode node = node_hash [node_idx] as ITreeNode; + if (node == null || node.ChildCount <= 0) + return 0; - return node.ChildCount; + return node.ChildCount; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } + return 0; } bool nth_child_cb (out int child_idx, int parent, int n) { child_idx = -1; - ITreeNode node; + try { + ITreeNode node; - if (parent == -1) { - if (Nodes.Count <= n) - return false; - node = Nodes [n] as ITreeNode; - child_idx = node.ID; - node_hash [node.ID] = node; - return true; - } + if (parent == -1) { + if (Nodes.Count <= n) + return false; + node = Nodes [n] as ITreeNode; + child_idx = node.ID; + node_hash [node.ID] = node; + return true; + } - node = node_hash [parent] as ITreeNode; - if (node == null || node.ChildCount <= n) - return false; + node = node_hash [parent] as ITreeNode; + if (node == null || node.ChildCount <= n) + return false; - ITreeNode child = node [n]; - node_hash [child.ID] = child; - child_idx = child.ID; - return true; + ITreeNode child = node [n]; + node_hash [child.ID] = child; + child_idx = child.ID; + return true; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } + return false; } bool parent_cb (out int parent_idx, int child) { parent_idx = -1; - ITreeNode node = node_hash [child] as ITreeNode; - if (node == null || node.Parent == null) - return false; + try { + ITreeNode node = node_hash [child] as ITreeNode; + if (node == null || node.Parent == null) + return false; - node_hash [node.Parent.ID] = node.Parent; - parent_idx = node.Parent.ID; - return true; + node_hash [node.Parent.ID] = node.Parent; + parent_idx = node.Parent.ID; + return true; + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } + return false; } [DllImport("gtksharpglue-2")] diff --git a/gtk/Widget.custom b/gtk/Widget.custom index 71eb14059..2bf53e884 100644 --- a/gtk/Widget.custom +++ b/gtk/Widget.custom @@ -198,10 +198,14 @@ static SetScrollAdjustmentsDelegate SetScrollAdjustmentsCallback; static void SetScrollAdjustments_cb (IntPtr widget, IntPtr hadj, IntPtr vadj) { - Widget obj = GLib.Object.GetObject (widget, false) as Widget; - Gtk.Adjustment h = GLib.Object.GetObject (hadj, false) as Gtk.Adjustment; - Gtk.Adjustment v = GLib.Object.GetObject (vadj, false) as Gtk.Adjustment; - obj.OnSetScrollAdjustments (h, v); + try { + Widget obj = GLib.Object.GetObject (widget, false) as Widget; + Gtk.Adjustment h = GLib.Object.GetObject (hadj, false) as Gtk.Adjustment; + Gtk.Adjustment v = GLib.Object.GetObject (vadj, false) as Gtk.Adjustment; + obj.OnSetScrollAdjustments (h, v); + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } static void ConnectSetScrollAdjustments (GLib.GType gtype) @@ -237,9 +241,13 @@ private delegate void BindingHandler (IntPtr handle, IntPtr user_data); private static void BindingCallback (IntPtr handle, IntPtr user_data) { - Widget w = GLib.Object.GetObject (handle, false) as Widget; - BindingInvoker invoker = ((GCHandle) user_data).Target as BindingInvoker; - invoker.Invoke (w); + try { + Widget w = GLib.Object.GetObject (handle, false) as Widget; + BindingInvoker invoker = ((GCHandle) user_data).Target as BindingInvoker; + invoker.Invoke (w); + } catch (Exception e) { + GLib.ExceptionManager.RaiseUnhandledException (e, false); + } } static BindingHandler binding_delegate;