shell: Check more thoroughly for undefined surface resource

The surface can have an undefined resource in certain situations (such
as with xwayland).  So, since NULL is a valid state for this parameter,
and since the wl_resource_*, etc. calls require their parameters to be
non-NULL, make a practice of always checking the surface resource before
making wayland calls.

update v2:
  * Fix some c/p errors for pointer names
  * Drop null ptr check in add_popup_grab; probably redundant now

Signed-off-by: Bryce Harrington <bryce@osg.samsung.com>
Reviewed-by: Daniel Stone <daniels@collabora.com>
diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c
index 5ae83ef..991effe 100644
--- a/desktop-shell/shell.c
+++ b/desktop-shell/shell.c
@@ -2098,7 +2098,7 @@
 			continue;
 
 		shsurf = get_shell_surface(pointer->focus->surface);
-		if (shsurf &&
+		if (shsurf && shsurf->resource &&
 		    wl_resource_get_client(shsurf->resource) == sc->client)
 			set_busy_cursor(shsurf, pointer);
 	}
@@ -2133,7 +2133,8 @@
 	if (shell_surface_is_xdg_surface(shsurf) ||
 	    shell_surface_is_xdg_popup(shsurf))
 		xdg_shell_send_ping(sc->resource, serial);
-	else if (shell_surface_is_wl_shell_surface(shsurf))
+	else if (shell_surface_is_wl_shell_surface(shsurf)
+		 && shsurf->resource)
 		wl_shell_surface_send_ping(shsurf->resource, serial);
 }
 
@@ -3342,7 +3343,9 @@
 static void
 shell_surface_send_popup_done(struct shell_surface *shsurf)
 {
-	if (shell_surface_is_wl_shell_surface(shsurf))
+	if (shsurf->resource == NULL)
+		return;
+	else if (shell_surface_is_wl_shell_surface(shsurf))
 		wl_shell_surface_send_popup_done(shsurf->resource);
 	else if (shell_surface_is_xdg_popup(shsurf))
 		xdg_popup_send_popup_done(shsurf->resource);
@@ -3589,7 +3592,8 @@
 
 	if (!wl_list_empty(&shsurf->popup.grab_link))
 		remove_popup_grab(shsurf);
-	wl_list_remove(wl_resource_get_link(shsurf->resource));
+	if (shsurf->resource)
+		wl_list_remove(wl_resource_get_link(shsurf->resource));
 	shsurf->resource = NULL;
 }
 
@@ -3767,6 +3771,10 @@
 	shsurf->resource =
 		wl_resource_create(client,
 				   &wl_shell_surface_interface, 1, id);
+	if (!shsurf->resource) {
+		wl_resource_post_no_memory(surface_resource);
+	        return;
+	}
 	wl_resource_set_implementation(shsurf->resource,
 				       &shell_surface_implementation,
 				       shsurf, shell_destroy_shell_surface);
@@ -4115,6 +4123,10 @@
 	shsurf->resource =
 		wl_resource_create(client,
 				   &xdg_surface_interface, 1, id);
+	if (!shsurf->resource) {
+		wl_resource_post_no_memory(surface_resource);
+		return;
+	}
 	wl_resource_set_implementation(shsurf->resource,
 				       &xdg_surface_implementation,
 				       shsurf, shell_destroy_shell_surface);
@@ -4246,6 +4258,10 @@
 	shsurf->resource =
 		wl_resource_create(client,
 				   &xdg_popup_interface, 1, id);
+	if (!shsurf->resource) {
+		wl_resource_post_no_memory(surface_resource);
+		return;
+	}
 	wl_resource_set_implementation(shsurf->resource,
 				       &xdg_popup_implementation,
 				       shsurf, shell_destroy_shell_surface);
@@ -4266,9 +4282,10 @@
 static bool
 shell_surface_is_xdg_popup(struct shell_surface *shsurf)
 {
-	return wl_resource_instance_of(shsurf->resource,
-				       &xdg_popup_interface,
-				       &xdg_popup_implementation);
+	return (shsurf->resource &&
+		wl_resource_instance_of(shsurf->resource,
+					&xdg_popup_interface,
+					&xdg_popup_implementation));
 }
 
 static const struct xdg_shell_interface xdg_implementation = {