[ubuntu-x] [PATCH 4/15] gnome-desktop: Null ptr check #2

Bryce Harrington bryce at canonical.com
Thu Apr 10 20:54:11 BST 2008


gnome-desktop:  105_gd-randr-null-ptrs.patch (LP: #210226)

I notice Soeren has added some NULL pointer checks in the code; this
expands on this by providing similar checks in similar spots throughout
the code.

We have been getting various reports of segfaults that seem to be due to
invalid pointer use, making gnome-settings-daemon crash and the user's
system fail to start up.  This should hopefully help g-s-d terminate
more gracefully in such situations.

diff -Nurp gnome-desktop-2.22.1-patched/libgnome-desktop/monitor-db.c gnome-desktop-2.22.1-working/libgnome-desktop/monitor-db.c
--- gnome-desktop-2.22.1-patched/libgnome-desktop/monitor-db.c	2008-04-08 22:36:57.000000000 -0700
+++ gnome-desktop-2.22.1-working/libgnome-desktop/monitor-db.c	2008-04-09 00:20:23.000000000 -0700
@@ -270,6 +270,8 @@ parser_free (Parser *parser)
     int i;
     GList *list;
 
+    g_return_if_fail (parser != NULL);
+
     if (parser->output)
 	output_free (parser->output);
 
@@ -349,6 +351,8 @@ configuration_new_current (RWScreen *scr
     int clone_width = -1;
     int clone_height = -1;
 
+    g_return_val_if_fail (screen != NULL, NULL);
+
     rw_outputs = rw_screen_list_outputs (screen);
 
     config->clone = TRUE;
@@ -509,6 +513,8 @@ outputs_free (Output **outputs)
 {
     int i;
 
+    g_return_if_fail(outputs != NULL);
+
     for (i = 0; outputs[i] != NULL; ++i)
 	output_free (outputs[i]);
 }
@@ -516,6 +522,7 @@ outputs_free (Output **outputs)
 void
 configuration_free (Configuration *config)
 {
+    g_return_if_fail(config != NULL);
     outputs_free (config->outputs);
     
     g_free (config);
@@ -525,6 +532,7 @@ static void
 configurations_free (Configuration **configurations)
 {
     int i;
+    g_return_if_fail(configurations != NULL);
 
     for (i = 0; configurations[i] != NULL; ++i)
 	configuration_free (configurations[i]);
@@ -576,6 +584,9 @@ out:
 static gboolean
 output_match (Output *output1, Output *output2)
 {
+    g_return_val_if_fail(output1 != NULL, FALSE);
+    g_return_val_if_fail(output2 != NULL, FALSE);
+
     if (strcmp (output1->name, output2->name) != 0)
 	return FALSE;
 
diff -Nurp gnome-desktop-2.22.1-patched/libgnome-desktop/randrwrap.c gnome-desktop-2.22.1-working/libgnome-desktop/randrwrap.c
--- gnome-desktop-2.22.1-patched/libgnome-desktop/randrwrap.c	2008-04-08 22:36:57.000000000 -0700
+++ gnome-desktop-2.22.1-working/libgnome-desktop/randrwrap.c	2008-04-08 23:15:31.000000000 -0700
@@ -118,7 +118,9 @@ static RWOutput *
 rw_output_by_id (ScreenInfo *info, RROutput id)
 {
     RWOutput **output;
-    
+
+    g_return_val_if_fail (info != NULL, NULL);
+
     for (output = info->outputs; *output; ++output)
     {
 	if ((*output)->id == id)
@@ -132,7 +134,11 @@ static RWCrtc *
 crtc_by_id (ScreenInfo *info, RRCrtc id)
 {
     RWCrtc **crtc;
-    
+
+    g_return_val_if_fail (info != NULL, NULL);
+    if (!info)
+        return NULL;
+
     for (crtc = info->crtcs; *crtc; ++crtc)
     {
 	if ((*crtc)->id == id)
@@ -146,7 +152,9 @@ static RWMode *
 mode_by_id (ScreenInfo *info, RRMode id)
 {
     RWMode **mode;
-    
+
+    g_return_val_if_fail (info != NULL, NULL);
+
     for (mode = info->modes; *mode; ++mode)
     {
 	if ((*mode)->id == id)
@@ -163,6 +171,8 @@ screen_info_free (ScreenInfo *info)
     RWCrtc **crtc;
     RWMode **mode;
 
+    g_return_if_fail (info != NULL);
+
     if (info->resources)
     {
 	XRRFreeScreenResources (info->resources);
@@ -200,6 +210,8 @@ fill_out_screen_info (Display *xdisplay,
 {
     XRRScreenResources *resources;
 
+    g_return_val_if_fail (info != NULL, FALSE);
+
     gdk_error_trap_push ();
 
     if (!XRRGetScreenSizeRange (xdisplay, xroot,
@@ -305,6 +317,9 @@ screen_info_new (RWScreen *screen)
     ScreenInfo *info = g_new0 (ScreenInfo, 1);
     RWOutput **o;
 
+    g_return_val_if_fail (info != NULL, NULL);
+    g_return_val_if_fail (screen != NULL, NULL);
+
     info->outputs = NULL;
     info->crtcs = NULL;
     info->modes = NULL;
@@ -360,7 +375,7 @@ screen_on_event (GdkXEvent *xevent,
     RWScreen *screen = data;
     XEvent *e = xevent;
     
-    if (e->type - screen->randr_event_base == RRNotify)
+    if (e && e->type - screen->randr_event_base == RRNotify)
     {
 	XRRNotifyEvent *event = (XRRNotifyEvent *)e;
 
@@ -395,7 +410,8 @@ rw_screen_new (GdkScreen *gdk_screen,
     if (XRRQueryExtension (dpy, &event_base, &ignore))
     {
 	RWScreen *screen = g_new0 (RWScreen, 1);
-	
+        g_return_val_if_fail (screen != NULL, NULL);
+
 	screen->gdk_screen = gdk_screen;
 	screen->gdk_root = gdk_screen_get_root_window (gdk_screen);
 	screen->xroot = gdk_x11_drawable_get_xid (screen->gdk_root);
@@ -471,18 +487,27 @@ rw_screen_refresh (RWScreen *screen)
 RWMode **
 rw_screen_list_modes (RWScreen *screen)
 {
+    g_return_val_if_fail (screen != NULL, NULL);
+    g_return_val_if_fail (screen->info != NULL, NULL);
+
     return screen->info->modes;
 }
 
 RWCrtc **
 rw_screen_list_crtcs (RWScreen *screen)
 {
+    g_return_val_if_fail (screen != NULL, NULL);
+    g_return_val_if_fail (screen->info != NULL, NULL);
+
     return screen->info->crtcs;
 }
 
 RWOutput **
 rw_screen_list_outputs (RWScreen *screen)
 {
+    g_return_val_if_fail (screen != NULL, NULL);
+    g_return_val_if_fail (screen->info != NULL, NULL);
+
     return screen->info->outputs;
 }
 
@@ -492,6 +517,9 @@ rw_screen_get_crtc_by_id (RWScreen *scre
 {
     int i;
 
+    g_return_val_if_fail (screen != NULL, NULL);
+    g_return_val_if_fail (screen->info != NULL, NULL);
+
     for (i = 0; screen->info->crtcs[i] != NULL; ++i)
     {
 	if (screen->info->crtcs[i]->id == id)
@@ -507,6 +535,9 @@ rw_screen_get_output_by_id (RWScreen *sc
 {
     int i;
 
+    g_return_val_if_fail (screen != NULL, NULL);
+    g_return_val_if_fail (screen->info != NULL, NULL);
+
     for (i = 0; screen->info->outputs[i] != NULL; ++i)
     {
 	if (screen->info->outputs[i]->id == id)
@@ -521,7 +552,8 @@ static RWOutput *
 output_new (ScreenInfo *info, RROutput id)
 {
     RWOutput *output = g_new0 (RWOutput, 1);
-    
+    g_return_val_if_fail (output != NULL, NULL);
+
     output->id = id;
     output->info = info;
     
@@ -659,12 +691,16 @@ output_free (RWOutput *output)
 guint32
 rw_output_get_id (RWOutput *output)
 {
+    g_assert(output != NULL);
+
     return output->id;
 }
 
 const guint8 *
 rw_output_get_edid_data (RWOutput *output)
 {
+    g_return_val_if_fail (output != NULL, NULL);
+
     return output->edid_data;
 }
 
@@ -674,6 +710,9 @@ rw_screen_get_output_by_name (RWScreen  
 {
     int i;
 
+    g_return_val_if_fail (screen != NULL, NULL);
+    g_return_val_if_fail (screen->info != NULL, NULL);
+
     for (i = 0; screen->info->outputs[i] != NULL; ++i)
     {
 	RWOutput *output = screen->info->outputs[i];
@@ -688,6 +727,8 @@ rw_screen_get_output_by_name (RWScreen  
 RWCrtc *
 rw_output_get_crtc (RWOutput *output)
 {
+    g_return_val_if_fail (output != NULL, NULL);
+
     return output->current_crtc;
 }
 
@@ -697,7 +738,7 @@ rw_output_get_current_mode (RWOutput *ou
     RWCrtc *crtc;
 
     g_return_val_if_fail (output != NULL, NULL);
-    
+
     if ((crtc = rw_output_get_crtc (output)))
 	return rw_crtc_get_current_mode (crtc);
 
@@ -712,7 +753,7 @@ rw_output_get_position (RWOutput        
     RWCrtc *crtc;
 
     g_return_if_fail (output != NULL);
-    
+
     if ((crtc = rw_output_get_crtc (output)))
 	rw_crtc_get_position (crtc, x, y);
 }
@@ -720,24 +761,28 @@ rw_output_get_position (RWOutput        
 const char *
 rw_output_get_name (RWOutput *output)
 {
+    g_assert (output != NULL);
     return output->name;
 }
 
 int
 rw_output_get_width_mm (RWOutput *output)
 {
+    g_assert (output != NULL);
     return output->width_mm;
 }
 
 int
 rw_output_get_height_mm (RWOutput *output)
 {
+    g_assert (output != NULL);
     return output->height_mm;
 }
 
 RWMode *
 rw_output_get_preferred_mode (RWOutput *output)
 {
+    g_return_val_if_fail (output != NULL, NULL);
     if (output->n_preferred)
 	return output->modes[0];
 
@@ -747,12 +792,14 @@ rw_output_get_preferred_mode (RWOutput *
 RWMode **
 rw_output_list_modes (RWOutput *output)
 {
+    g_return_val_if_fail (output != NULL, NULL);
     return output->modes;
 }
 
 gboolean
 rw_output_is_connected (RWOutput *output)
 {
+    g_return_val_if_fail (output != NULL, FALSE);
     return output->connected;
 }
 
@@ -936,12 +983,14 @@ rw_crtc_get_position (RWCrtc          *c
 RWRotation
 rw_crtc_get_current_rotation (RWCrtc *crtc)
 {
+    g_assert(crtc != NULL);
     return crtc->current_rotation;
 }
 
 RWRotation
 rw_crtc_get_rotations (RWCrtc *crtc)
 {
+    g_assert(crtc != NULL);
     return crtc->rotations;
 }
 
@@ -949,6 +998,7 @@ gboolean
 rw_crtc_supports_rotation (RWCrtc *   crtc,
 			   RWRotation rotation)
 {
+    g_return_val_if_fail (crtc != NULL, FALSE);
     return (crtc->rotations & rotation);
 }
 
@@ -956,7 +1006,8 @@ static RWCrtc *
 crtc_new (ScreenInfo *info, RROutput id)
 {
     RWCrtc *crtc = g_new0 (RWCrtc, 1);
-    
+    g_return_val_if_fail (crtc != NULL, NULL);
+
     crtc->id = id;
     crtc->info = info;
     
@@ -1028,6 +1079,7 @@ static RWMode *
 mode_new (ScreenInfo *info, RRMode id)
 {
     RWMode *mode = g_new0 (RWMode, 1);
+    g_return_val_if_fail (mode != NULL, NULL);
     
     mode->id = id;
     mode->info = info;
@@ -1038,30 +1090,35 @@ mode_new (ScreenInfo *info, RRMode id)
 guint32
 rw_mode_get_id (RWMode *mode)
 {
+    g_return_val_if_fail (mode != NULL, 0);
     return mode->id;
 }
 
 guint
 rw_mode_get_width (RWMode *mode)
 {
+    g_return_val_if_fail (mode != NULL, 0);
     return mode->width;
 }
 
 int
 rw_mode_get_freq (RWMode *mode)
 {
+    g_return_val_if_fail (mode != NULL, 0);
     return (mode->freq) / 1000;
 }
 
 guint
 rw_mode_get_height (RWMode *mode)
 {
+    g_return_val_if_fail (mode != NULL, 0);
     return mode->height;
 }
 
 static void
 mode_initialize (RWMode *mode, XRRModeInfo *info)
 {
+    g_return_if_fail(mode != NULL);
     mode->name = g_strdup (info->name);
     mode->width = info->width;
     mode->height = info->height;
@@ -1071,6 +1128,7 @@ mode_initialize (RWMode *mode, XRRModeIn
 static void
 mode_free (RWMode *mode)
 {
+    g_return_if_fail(mode != NULL);
     g_free (mode->name);
     g_free (mode);
 }



More information about the Ubuntu-x mailing list