[rygel/wip/phako/remove-warnings: 7/14] server: Fix potential null access warnings



commit 6a80e62d91194a78b46198e5dc788d6807bd04f8
Author: Jens Georg <mail jensge org>
Date:   Thu Feb 13 23:32:48 2020 +0100

    server: Fix potential null access warnings

 src/librygel-server/rygel-content-directory.vala   |  6 ++--
 .../rygel-dtcp-cleartext-request.vala              | 14 +++++----
 src/librygel-server/rygel-http-get.vala            |  4 +--
 src/librygel-server/rygel-http-item-uri.vala       |  6 ++--
 src/librygel-server/rygel-http-post.vala           |  7 +++--
 src/librygel-server/rygel-import-resource.vala     | 10 +++++--
 src/librygel-server/rygel-item-destroyer.vala      |  5 ++--
 src/librygel-server/rygel-media-container.vala     |  6 ++--
 src/librygel-server/rygel-media-object.vala        |  6 ++--
 src/librygel-server/rygel-object-creator.vala      | 34 +++++++++++++---------
 src/librygel-server/rygel-panasonic-hacks.vala     | 11 ++-----
 src/librygel-server/rygel-playspeed-request.vala   |  7 +++--
 .../rygel-relational-expression.vala               | 30 +++++++------------
 src/librygel-server/rygel-samsung-tv-hacks.vala    | 19 +++++-------
 src/librygel-server/rygel-simple-container.vala    |  2 +-
 15 files changed, 85 insertions(+), 82 deletions(-)
---
diff --git a/src/librygel-server/rygel-content-directory.vala 
b/src/librygel-server/rygel-content-directory.vala
index 0b432cd9..99f2b9c6 100644
--- a/src/librygel-server/rygel-content-directory.vala
+++ b/src/librygel-server/rygel-content-directory.vala
@@ -472,8 +472,10 @@ public class Rygel.ContentDirectory: Service {
         object.object_update_id = this.system_update_id;
         // Whenever container experiences object update it also
         // experiences a container update
-        if (object is MediaContainer) {
-            (object as MediaContainer).update_id = this.system_update_id;
+
+        var container = object as MediaContainer;
+        if (container != null) {
+            container.update_id = this.system_update_id;
         }
 
         return container_changed;
diff --git a/src/librygel-server/rygel-dtcp-cleartext-request.vala 
b/src/librygel-server/rygel-dtcp-cleartext-request.vala
index 32070de5..3dfc6d8d 100644
--- a/src/librygel-server/rygel-dtcp-cleartext-request.vala
+++ b/src/librygel-server/rygel-dtcp-cleartext-request.vala
@@ -58,9 +58,9 @@ public class Rygel.DTCPCleartextRequest : Rygel.HTTPSeekRequest {
 
         // It's only possible to get the cleartext size from a MediaResource
         //  (and only if it is link protected)
-        if (handler is HTTPMediaResourceHandler) {
-            var resource = (handler as HTTPMediaResourceHandler)
-                                        .media_resource;
+        var resource_handler = handler as HTTPMediaResourceHandler;
+        if (resource_handler != null) {
+            var resource = resource_handler.media_resource;
             total_size = resource.cleartext_size;
             if (total_size <= 0) {
                 // Even if it's a resource and the content is link-protected,
@@ -148,9 +148,11 @@ public class Rygel.DTCPCleartextRequest : Rygel.HTTPSeekRequest {
 
     public static bool supported (Soup.Message message,
                                   Rygel.HTTPGetHandler handler) {
-        return (handler is HTTPMediaResourceHandler)
-               && (handler as HTTPMediaResourceHandler)
-                  .media_resource.is_cleartext_range_support_enabled ();
+        var resource_handler = handler as HTTPMediaResourceHandler;
+
+        return (resource_handler != null)
+               && resource_handler.media_resource
+                  .is_cleartext_range_support_enabled ();
     }
 
     public static bool requested (Soup.Message message) {
diff --git a/src/librygel-server/rygel-http-get.vala b/src/librygel-server/rygel-http-get.vala
index d2084c74..bfabc9bd 100644
--- a/src/librygel-server/rygel-http-get.vala
+++ b/src/librygel-server/rygel-http-get.vala
@@ -105,8 +105,8 @@ public class Rygel.HTTPGet : HTTPRequest {
             return;
         }
 
-        if (unlikely ((this.object is MediaFileItem)
-                      && (this.object as MediaFileItem).place_holder)) {
+        var item = this.object as MediaFileItem;
+        if (item != null && item.place_holder) {
             throw new HTTPRequestError.NOT_FOUND ("Item '%s' is empty",
                                                   this.object.id);
         }
diff --git a/src/librygel-server/rygel-http-item-uri.vala b/src/librygel-server/rygel-http-item-uri.vala
index d74e8d25..dd64ed7c 100644
--- a/src/librygel-server/rygel-http-item-uri.vala
+++ b/src/librygel-server/rygel-http-item-uri.vala
@@ -76,13 +76,13 @@ public class Rygel.HTTPItemURI : Object {
         var item = object as MediaFileItem;
         if (thumbnail_index > -1) {
             if (item is VisualItem) {
-                var thumbnails = (item as VisualItem).thumbnails;
+                var thumbnails = ((VisualItem) item).thumbnails;
 
                 if (thumbnails.size > thumbnail_index) {
                     this.extension = thumbnails[thumbnail_index].file_extension;
                 }
             } else if (item is MusicItem) {
-                var album_art = (item as MusicItem).album_art;
+                var album_art = ((MusicItem) item).album_art;
 
                 if (album_art != null) {
                     this.extension = album_art.file_extension;
@@ -90,7 +90,7 @@ public class Rygel.HTTPItemURI : Object {
             }
         } else if (subtitle_index > -1) {
             if (item is VideoItem) {
-                var subtitles = (item as VideoItem).subtitles;
+                var subtitles = ((VideoItem) item).subtitles;
 
                 if (subtitles.size > subtitle_index) {
                     this.extension = subtitles[subtitle_index].caption_type;
diff --git a/src/librygel-server/rygel-http-post.vala b/src/librygel-server/rygel-http-post.vala
index b2a9efd7..d511a4b3 100644
--- a/src/librygel-server/rygel-http-post.vala
+++ b/src/librygel-server/rygel-http-post.vala
@@ -58,14 +58,15 @@ internal class Rygel.HTTPPost : HTTPRequest {
     }
 
     private async void handle_real () throws Error {
-        if (!(this.object as MediaFileItem).place_holder) {
+        var item = (MediaFileItem) this.object;
+
+        if (!item.place_holder) {
             var msg = _("Pushing data to non-empty item “%s” not allowed");
 
             throw new ContentDirectoryError.INVALID_ARGS (msg, this.object.id);
         }
 
-        this.file = yield (this.object as MediaFileItem).get_writable
-                                        (this.cancellable);
+        this.file = yield item.get_writable (this.cancellable);
         if (this.file == null) {
             throw new HTTPRequestError.BAD_REQUEST
                                         (_("No writable URI for %s available"),
diff --git a/src/librygel-server/rygel-import-resource.vala b/src/librygel-server/rygel-import-resource.vala
index 3767074d..78f9a6d7 100644
--- a/src/librygel-server/rygel-import-resource.vala
+++ b/src/librygel-server/rygel-import-resource.vala
@@ -180,12 +180,16 @@ internal class Rygel.ImportResource : GLib.Object, Rygel.StateMachine {
                                                                   null);
         string msg = null;
 
+        var media_item = media_object as MediaFileItem;
+
         if (media_object == null ||
-            !(media_object is MediaFileItem) ||
+            media_item == null ||
             !(media_object.parent is WritableContainer)) {
             msg = _("URI “%s” invalid for importing contents to").printf
                                         (this.destination_uri);
-        } else if (!(media_object as MediaFileItem).place_holder) {
+        }
+
+        else if (!media_item.place_holder) {
             msg = _("Pushing data to non-empty item “%s” not allowed").printf
                                         (media_object.id);
         } else if (media_object.get_uris ().is_empty) {
@@ -196,7 +200,7 @@ internal class Rygel.ImportResource : GLib.Object, Rygel.StateMachine {
             throw new ContentDirectoryError.INVALID_ARGS (msg);
         }
 
-        return media_object as MediaFileItem;
+        return media_item;
     }
 
     private void got_headers_cb (Message message) {
diff --git a/src/librygel-server/rygel-item-destroyer.vala b/src/librygel-server/rygel-item-destroyer.vala
index 911378f6..be43da3f 100644
--- a/src/librygel-server/rygel-item-destroyer.vala
+++ b/src/librygel-server/rygel-item-destroyer.vala
@@ -76,11 +76,12 @@ internal class Rygel.ItemDestroyer: GLib.Object, Rygel.StateMachine {
     private async void remove_object () throws Error {
         var media_object = yield this.fetch_object ();
         var parent = media_object.parent as WritableContainer;
+        var item = media_object as MediaFileItem;
 
-        if (media_object is MediaFileItem ) {
+        if (item != null) {
             yield parent.remove_item (this.object_id, this.cancellable);
 
-            if (!(media_object as MediaFileItem).place_holder) {
+            if (!item.place_holder) {
                 var writables = yield media_object.get_writables (this.cancellable);
                 foreach (var file in writables) {
                     if (file.query_exists (this.cancellable)) {
diff --git a/src/librygel-server/rygel-media-container.vala b/src/librygel-server/rygel-media-container.vala
index d9f9ed44..631cc5e0 100644
--- a/src/librygel-server/rygel-media-container.vala
+++ b/src/librygel-server/rygel-media-container.vala
@@ -148,7 +148,7 @@ internal class Rygel.PlaylistDatasource : Rygel.DataSource, Object {
  * interface.
  *
  * If the container should support the change tracking profile of the UPnP
- * ContentDirectory:3 specification then you should also implement the 
+ * ContentDirectory:3 specification then you should also implement the
  * #RygelTrackableContainer interface.
  *
  * The #RygelSimpleContainer class contains a simple memory-based container
@@ -345,7 +345,7 @@ public abstract class Rygel.MediaContainer : MediaObject {
      * for this container, if items under it are removed or added, if
      * there are metadata changes to items under it, etc.
      *
-     * If sub_tree_update is true then the caller should later emit the 
+     * If sub_tree_update is true then the caller should later emit the
      * sub_tree_updates_finished signal on the root container of the sub-tree
      * that was updated.
      *
@@ -432,7 +432,7 @@ public abstract class Rygel.MediaContainer : MediaObject {
 
         // If the container is searchable then it must add search class parameters.
         if (this is SearchableContainer) {
-            (this as SearchableContainer).serialize_search_parameters
+            ((SearchableContainer) this).serialize_search_parameters
                                         (didl_container);
         }
 
diff --git a/src/librygel-server/rygel-media-object.vala b/src/librygel-server/rygel-media-object.vala
index d3e450b4..c4300d87 100644
--- a/src/librygel-server/rygel-media-object.vala
+++ b/src/librygel-server/rygel-media-object.vala
@@ -260,8 +260,8 @@ public abstract class Rygel.MediaObject : GLib.Object {
                                                              -1,
                                                              -1,
                                                              res.get_name ());
-                if (this is MediaFileItem &&
-                    (this as MediaFileItem).place_holder) {
+                var media_item = this as MediaFileItem;
+                if (media_item != null && media_item.place_holder) {
                     res.import_uri = uri;
                 } else {
                     res.uri = uri;
@@ -374,7 +374,7 @@ public abstract class Rygel.MediaObject : GLib.Object {
             if (result == DIDLLiteFragmentResult.OK) {
                 this.apply_didl_lite (didl_object);
                 if (this is UpdatableObject) {
-                    yield (this as UpdatableObject).commit ();
+                    yield ((UpdatableObject) this).commit ();
                 }
             }
 
diff --git a/src/librygel-server/rygel-object-creator.vala b/src/librygel-server/rygel-object-creator.vala
index 7c5ca864..86dda358 100644
--- a/src/librygel-server/rygel-object-creator.vala
+++ b/src/librygel-server/rygel-object-creator.vala
@@ -152,10 +152,10 @@ internal class Rygel.ObjectCreator: GLib.Object, Rygel.StateMachine {
 
             yield this.create_object_from_didl (container);
             if (this.object is MediaFileItem) {
-                yield container.add_item (this.object as MediaFileItem,
+                yield container.add_item ((MediaFileItem) this.object,
                                           this.cancellable);
             } else {
-                yield container.add_container (this.object as MediaContainer,
+                yield container.add_container ((MediaContainer) this.object,
                                                this.cancellable);
             }
 
@@ -166,9 +166,11 @@ internal class Rygel.ObjectCreator: GLib.Object, Rygel.StateMachine {
             // Conclude the successful action
             this.conclude ();
 
+            var item = this.object as MediaFileItem;
+
             if (this.container_id == MediaContainer.ANY &&
-                (this.object is MediaFileItem &&
-                 (this.object as MediaFileItem).place_holder)) {
+                (item != null) &&
+                item.place_holder) {
                 var queue = ObjectRemovalQueue.get_default ();
 
                 queue.queue (this.object, this.cancellable);
@@ -518,16 +520,18 @@ internal class Rygel.ObjectCreator: GLib.Object, Rygel.StateMachine {
         }
 
         // extract_item_parameters could not find an uri
+        var item = this.object as MediaFileItem;
+
         if (this.object.get_uris ().is_empty) {
             var uri = yield this.create_uri (container, this.object.title);
             this.object.add_uri (uri);
-            if (this.object is MediaFileItem) {
-                (this.object as MediaFileItem).place_holder = true;
+            if (item != null) {
+                item.place_holder = true;
             }
         } else {
-            if (this.object is MediaFileItem) {
+            if (item != null) {
                 var file = File.new_for_uri (this.object.get_primary_uri ());
-                (this.object as MediaFileItem).place_holder = !file.is_native ();
+                item.place_holder = !file.is_native ();
             }
         }
 
@@ -579,19 +583,23 @@ internal class Rygel.ObjectCreator: GLib.Object, Rygel.StateMachine {
     }
 
     private void parse_and_verify_didl_date () throws Error {
-        if (!(this.didl_object is DIDLLiteItem)) {
+        var didl_item = this.didl_object as DIDLLiteItem;
+        if (didl_item == null) {
+            return;
+        }
+
+        var item = this.object as MediaFileItem;
+        if (item == null) {
             return;
         }
 
-        var didl_item = this.didl_object as DIDLLiteItem;
         if (didl_item.date == null) {
             return;
         }
 
         var parsed_date = new Soup.Date.from_string (didl_item.date);
         if (parsed_date != null) {
-            (this.object as MediaFileItem).date = parsed_date.to_string
-                                            (Soup.DateFormat.ISO8601);
+            item.date = parsed_date.to_string (Soup.DateFormat.ISO8601);
 
             return;
         }
@@ -616,7 +624,7 @@ internal class Rygel.ObjectCreator: GLib.Object, Rygel.StateMachine {
                                      didl_item.date);
         }
 
-        (this.object as MediaFileItem).date = didl_item.date + "T00:00:00";
+        item.date = didl_item.date + "T00:00:00";
     }
 
     private MediaObject create_object (string            id,
diff --git a/src/librygel-server/rygel-panasonic-hacks.vala b/src/librygel-server/rygel-panasonic-hacks.vala
index e7553380..16499b4d 100644
--- a/src/librygel-server/rygel-panasonic-hacks.vala
+++ b/src/librygel-server/rygel-panasonic-hacks.vala
@@ -43,13 +43,8 @@ internal class Rygel.PanasonicHacks : ClientHacks {
     }
 
     public override void apply (MediaObject object) {
-        if (!(object is MediaFileItem)) {
-            return;
-        }
-
-        var item = object as MediaFileItem;
-
-        if (!(item is VisualItem)) {
+        var item = object as VisualItem;
+        if (item == null) {
             return;
         }
 
@@ -57,7 +52,7 @@ internal class Rygel.PanasonicHacks : ClientHacks {
         // type JPEG. This is correct from a DLNA pov, but we usually only
         // supply PNG. When fooled into accepting it, they're rendered fine,
         // however.
-        foreach (var thumbnail in (item as VisualItem).thumbnails) {
+        foreach (var thumbnail in item.thumbnails) {
             try {
                 thumbnail.mime_type = mime_regex.replace_literal
                                         (thumbnail.mime_type, -1, 0, "jpeg");
diff --git a/src/librygel-server/rygel-playspeed-request.vala 
b/src/librygel-server/rygel-playspeed-request.vala
index f9580043..33fa1cd6 100644
--- a/src/librygel-server/rygel-playspeed-request.vala
+++ b/src/librygel-server/rygel-playspeed-request.vala
@@ -83,9 +83,10 @@ public class Rygel.PlaySpeedRequest : GLib.Object {
         // Normal rate is always valid. Just check for valid scaled rate
         if (!speed.is_normal_rate ()) {
             // Validate if playspeed is listed in the protocolInfo
-            if (request.handler is HTTPMediaResourceHandler) {
-                var resource = (request.handler as HTTPMediaResourceHandler)
-                                         .media_resource;
+            var resource_handler = request.handler as HTTPMediaResourceHandler;
+
+            if (resource_handler != null) {
+                var resource = resource_handler.media_resource;
                 var speeds = resource.play_speeds;
                 var found_speed = false;
                 foreach (var speed in speeds) {
diff --git a/src/librygel-server/rygel-relational-expression.vala 
b/src/librygel-server/rygel-relational-expression.vala
index a3f0eed6..ec55e9ce 100644
--- a/src/librygel-server/rygel-relational-expression.vala
+++ b/src/librygel-server/rygel-relational-expression.vala
@@ -79,31 +79,23 @@ public class Rygel.RelationalExpression :
 
             return this.compare_create_class
                                         (media_object as WritableContainer);
-        case "dc:creator":
-            if (!(media_object is PhotoItem)) {
-                return false;
-            }
+        case "dc:creator": {
+            var item = media_object as PhotoItem;
 
-            return this.compare_string ((media_object as PhotoItem).creator);
-        case "upnp:artist":
-            if (!(media_object is MusicItem)) {
-                return false;
-            }
+            return item != null && this.compare_string (item.creator);
+        }
+        case "upnp:artist": {
+            var item = media_object as MusicItem;
 
-            return this.compare_string ((media_object as MusicItem).artist);
+            return item != null && this.compare_string (item.artist);
+        }
         case "upnp:album":
-            if (!(media_object is MusicItem)) {
-                return false;
-            }
+            var item = media_object as MusicItem;
 
-            return this.compare_string ((media_object as MusicItem).album);
+            return item != null && this.compare_string (item.album);
         case "@childCount":
-            if (!(media_object is MediaContainer)) {
-                return false;
-            }
-
             var container = media_object as MediaContainer;
-            return this.compare_int (container.child_count);
+            return container != null && this.compare_int (container.child_count);
         default:
             return false;
         }
diff --git a/src/librygel-server/rygel-samsung-tv-hacks.vala b/src/librygel-server/rygel-samsung-tv-hacks.vala
index adc4146f..b005e656 100644
--- a/src/librygel-server/rygel-samsung-tv-hacks.vala
+++ b/src/librygel-server/rygel-samsung-tv-hacks.vala
@@ -57,13 +57,8 @@ internal class Rygel.SamsungTVHacks : ClientHacks {
             }
         }
 
-        if (!(object is MediaFileItem)) {
-            return;
-        }
-
-        var item = object as MediaFileItem;
-
-        if (!(item is VisualItem)) {
+        var item = object as VisualItem;
+        if (item == null) {
             return;
         }
 
@@ -72,7 +67,7 @@ internal class Rygel.SamsungTVHacks : ClientHacks {
         // supply PNG. When fooled into accepting it, they're rendered fine,
         // however.
         // TODO: Unifiy with Panasonic hack!
-        foreach (var thumbnail in (item as VisualItem).thumbnails) {
+        foreach (var thumbnail in item.thumbnails) {
             try {
                 thumbnail.mime_type = mime_regex.replace_literal
                                         (thumbnail.mime_type, -1, 0, "jpeg");
@@ -89,11 +84,13 @@ internal class Rygel.SamsungTVHacks : ClientHacks {
     }
 
     public override void modify_headers (HTTPRequest request) {
+        var item = request.object as VideoItem;
+
         if (request.msg.request_headers.get_one ("getCaptionInfo.sec") != null
-            && (request.object is VideoItem)
-            && (request.object as VideoItem).subtitles.size > 0) {
+            && item != null
+            && item.subtitles.size > 0) {
                 var caption_uri = request.http_server.create_uri_for_object
-                                        (request.object as MediaItem,
+                                        (item,
                                          -1,
                                          0, // FIXME: offer first subtitle only?
                                          null);
diff --git a/src/librygel-server/rygel-simple-container.vala b/src/librygel-server/rygel-simple-container.vala
index db24c8fa..929524d1 100644
--- a/src/librygel-server/rygel-simple-container.vala
+++ b/src/librygel-server/rygel-simple-container.vala
@@ -108,7 +108,7 @@ public class Rygel.SimpleContainer : Rygel.MediaContainer,
      */
     public void add_child_container (MediaContainer child) {
         if (child is SearchableContainer) {
-            var search_classes = (child as SearchableContainer).search_classes;
+            var search_classes = ((SearchableContainer) child).search_classes;
             this.search_classes.add_all (search_classes);
         }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]