shell: assert get_shell_surface() != NULL as appropriate
authorU. Artie Eoff <ullysses.a.eoff@intel.com>
Fri, 17 Jan 2014 18:08:25 +0000 (10:08 -0800)
committerKristian Høgsberg <krh@bitplanet.net>
Mon, 20 Jan 2014 06:06:05 +0000 (22:06 -0800)
Various functions that operate on a weston_surface assume the
surface has a shell_surface.  That is, they unconditionally
deref the get_shell_surface() result.  Hence, if for some reason
the call to get_shell_surface() returned NULL to those functions then
a segmentation fault would occur and the program would crash.  So,
adding an assert(...) on the get_shell_surface() return value adds an
extra sanity check and does not change this behavior.  The assert also
adds an extra benefit to the programmer by documenting that the function
expects and requires the weston_surface to have a shell_surface and
would be a program logic error, otherwise.

The assert() also silences some static analyzers about the possible
NULL deref.

Signed-off-by: U. Artie Eoff <ullysses.a.eoff@intel.com>
desktop-shell/shell.c

index 7d7563f..09614b6 100644 (file)
@@ -356,7 +356,7 @@ shell_touch_grab_start(struct shell_touch_grab *grab,
                       struct weston_touch *touch)
 {
        struct desktop_shell *shell = shsurf->shell;
-       
+
        grab->grab.interface = interface;
        grab->shsurf = shsurf;
        grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
@@ -1506,14 +1506,14 @@ common_surface_move(struct wl_resource *resource,
            seat->pointer->button_count > 0 &&
            seat->pointer->grab_serial == serial) {
                surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
-               if ((surface == shsurf->surface) && 
+               if ((surface == shsurf->surface) &&
                    (surface_move(shsurf, seat) < 0))
                        wl_resource_post_no_memory(resource);
        } else if (seat->touch &&
                   seat->touch->focus &&
                   seat->touch->grab_serial == serial) {
                surface = weston_surface_get_main_surface(seat->touch->focus->surface);
-               if ((surface == shsurf->surface) && 
+               if ((surface == shsurf->surface) &&
                    (surface_touch_move(shsurf, seat) < 0))
                        wl_resource_post_no_memory(resource);
        }
@@ -1578,6 +1578,8 @@ send_configure(struct weston_surface *surface,
 {
        struct shell_surface *shsurf = get_shell_surface(surface);
 
+       assert(shsurf);
+
        wl_shell_surface_send_configure(shsurf->resource,
                                        edges, width, height);
 }
@@ -3258,6 +3260,8 @@ xdg_send_configure(struct weston_surface *surface,
 {
        struct shell_surface *shsurf = get_shell_surface(surface);
 
+       assert(shsurf);
+
        xdg_surface_send_configure(shsurf->resource, edges, width, height);
 }
 
@@ -4226,6 +4230,8 @@ activate(struct desktop_shell *shell, struct weston_surface *es,
        wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
 
        shsurf = get_shell_surface(main_surface);
+       assert(shsurf);
+
        if (shsurf->state.fullscreen)
                shell_configure_fullscreen(shsurf);
        else
@@ -4238,7 +4244,7 @@ activate(struct desktop_shell *shell, struct weston_surface *es,
 
        /* Update the surface’s layer. This brings it to the top of the stacking
         * order as appropriate. */
-       shell_surface_update_layer(get_shell_surface(main_surface));
+       shell_surface_update_layer(shsurf);
 }
 
 /* no-op func for checking black surface */
@@ -4716,6 +4722,8 @@ configure(struct desktop_shell *shell, struct weston_surface *surface,
 
        shsurf = get_shell_surface(surface);
 
+       assert(shsurf);
+
        if (shsurf->state.fullscreen)
                shell_configure_fullscreen(shsurf);
        else if (shsurf->state.maximized) {
@@ -4744,10 +4752,13 @@ static void
 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
 {
        struct shell_surface *shsurf = get_shell_surface(es);
-       struct desktop_shell *shell = shsurf->shell;
-
+       struct desktop_shell *shell;
        int type_changed = 0;
 
+       assert(shsurf);
+
+       shell = shsurf->shell;
+
        if (!weston_surface_is_mapped(es) &&
            !wl_list_empty(&shsurf->popup.grab_link)) {
                remove_popup_grab(shsurf);
@@ -4784,6 +4795,8 @@ shell_surface_output_destroyed(struct weston_surface *es)
 {
        struct shell_surface *shsurf = get_shell_surface(es);
 
+       assert(shsurf);
+
        shsurf->saved_position_valid = false;
        shsurf->next_state.maximized = false;
        shsurf->next_state.fullscreen = false;