From 82ade3a9eb8e6ae24e2fca3c88a6465d7b7fbfd8 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <jnqnfe@gmail.com>
Date: Sat, 5 Sep 2020 19:10:53 +0100
Subject: [PATCH v3] qt: fix incorrect param type

which was causing a whole bunch of "'< 0' is always false" warnings.

review of the original fix ([1]) indicated that in fact the parameter was
wrong rather than the checks within the function being out of step with
the final design of the function interface, since apparently callers
are in fact passing in a signed index, where a negative represents an
invalid index which the function is expected to handle appropriately.

[1]: https://mailman.videolan.org/pipermail/vlc-devel/2020-September/137711.html
---
 modules/gui/qt/medialibrary/mlalbummodel.cpp        |  2 +-
 modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp   |  2 +-
 modules/gui/qt/medialibrary/mlartistmodel.cpp       |  2 +-
 modules/gui/qt/medialibrary/mlbasemodel.hpp         | 12 ++++++++----
 modules/gui/qt/medialibrary/mlgenremodel.cpp        |  2 +-
 modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp |  2 +-
 modules/gui/qt/medialibrary/mlvideomodel.cpp        |  2 +-
 7 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/modules/gui/qt/medialibrary/mlalbummodel.cpp b/modules/gui/qt/medialibrary/mlalbummodel.cpp
index dc044c28ef..2326c43718 100644
--- a/modules/gui/qt/medialibrary/mlalbummodel.cpp
+++ b/modules/gui/qt/medialibrary/mlalbummodel.cpp
@@ -38,7 +38,7 @@ QVariant MLAlbumModel::data(const QModelIndex &index, int role) const
     if (!index.isValid() || index.row() < 0)
         return QVariant();
 
-    const MLAlbum* ml_item = item(static_cast<unsigned int>(index.row()));
+    const MLAlbum* ml_item = item(index.row());
     if ( ml_item == NULL )
         return QVariant();
 
diff --git a/modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp b/modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp
index dc9800dd4c..d0efbf56f9 100644
--- a/modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp
+++ b/modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp
@@ -39,7 +39,7 @@ QVariant MLAlbumTrackModel::data(const QModelIndex &index, int role) const
     if (!index.isValid() || index.row() < 0)
         return QVariant();
 
-    const MLAlbumTrack* ml_track = item(static_cast<unsigned int>(index.row()));
+    const MLAlbumTrack* ml_track = item(index.row());
     if ( !ml_track )
         return QVariant();
 
diff --git a/modules/gui/qt/medialibrary/mlartistmodel.cpp b/modules/gui/qt/medialibrary/mlartistmodel.cpp
index f3e745c6cd..d9db186457 100644
--- a/modules/gui/qt/medialibrary/mlartistmodel.cpp
+++ b/modules/gui/qt/medialibrary/mlartistmodel.cpp
@@ -32,7 +32,7 @@ QVariant MLArtistModel::data(const QModelIndex &index, int role) const
     if (!index.isValid() || index.row() < 0)
         return QVariant();
 
-    const MLArtist* ml_artist = item(static_cast<unsigned int>(index.row()));
+    const MLArtist* ml_artist = item(index.row());
     if ( !ml_artist )
         return QVariant();
 
diff --git a/modules/gui/qt/medialibrary/mlbasemodel.hpp b/modules/gui/qt/medialibrary/mlbasemodel.hpp
index 71187c13d1..4d7c87b574 100644
--- a/modules/gui/qt/medialibrary/mlbasemodel.hpp
+++ b/modules/gui/qt/medialibrary/mlbasemodel.hpp
@@ -164,7 +164,7 @@ public:
         return m_total_count;
     }
 
-    virtual T* get(unsigned int idx) const
+    virtual T* get(int idx) const
     {
         vlc_mutex_locker lock( &m_item_lock );
         T* obj = item( idx );
@@ -241,8 +241,12 @@ public:
     }
 
 protected:
-    T* item(unsigned int idx) const
+    T* item(int signedidx) const
     {
+        if (signedidx < 0)
+            return nullptr;
+
+        unsigned int idx = static_cast<unsigned int>(signedidx);
         // Must be called in a locked context
         if ( m_initialized == false )
         {
@@ -253,7 +257,7 @@ protected:
             emit countChanged( static_cast<unsigned int>(m_total_count) );
         }
 
-        if ( m_total_count == 0 || idx >= m_total_count || idx < 0 )
+        if ( m_total_count == 0 || idx >= m_total_count  )
             return nullptr;
 
         if ( idx < m_query_param.i_offset ||  idx >= m_query_param.i_offset + m_item_list.size() )
@@ -266,7 +270,7 @@ protected:
         }
 
         //db has changed
-        if ( idx - m_query_param.i_offset >= m_item_list.size() || idx - m_query_param.i_offset < 0 )
+        if ( idx - m_query_param.i_offset >= m_item_list.size() || idx < m_query_param.i_offset )
             return nullptr;
         return m_item_list[idx - m_query_param.i_offset].get();
     }
diff --git a/modules/gui/qt/medialibrary/mlgenremodel.cpp b/modules/gui/qt/medialibrary/mlgenremodel.cpp
index 793205a141..9e83fbfdb5 100644
--- a/modules/gui/qt/medialibrary/mlgenremodel.cpp
+++ b/modules/gui/qt/medialibrary/mlgenremodel.cpp
@@ -35,7 +35,7 @@ QVariant MLGenreModel::data(const QModelIndex &index, int role) const
     if (!index.isValid() || index.row() < 0)
         return QVariant();
 
-    const MLGenre* ml_genre = item(static_cast<unsigned int>(index.row()));
+    const MLGenre* ml_genre = item(index.row());
     if (!ml_genre)
         return QVariant();
 
diff --git a/modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp b/modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
index 4b95098c55..35e8a544b1 100644
--- a/modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
+++ b/modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
@@ -43,7 +43,7 @@ MLRecentsVideoModel::MLRecentsVideoModel( QObject* parent )
 
 QVariant MLRecentsVideoModel::data( const QModelIndex& index , int role ) const
 {
-    const auto video = item( static_cast<unsigned int>( index.row() ) );
+    const auto video = item( index.row() );
     if ( video == nullptr )
         return {};
     switch ( role )
diff --git a/modules/gui/qt/medialibrary/mlvideomodel.cpp b/modules/gui/qt/medialibrary/mlvideomodel.cpp
index fa94dcf871..80fe7f15e7 100644
--- a/modules/gui/qt/medialibrary/mlvideomodel.cpp
+++ b/modules/gui/qt/medialibrary/mlvideomodel.cpp
@@ -33,7 +33,7 @@ MLVideoModel::MLVideoModel(QObject* parent)
 
 QVariant MLVideoModel::data(const QModelIndex& index, int role) const
 {
-    const auto video = item(static_cast<unsigned int>(index.row()));
+    const auto video = item(index.row());
     if ( video == nullptr )
         return {};
     switch (role)
-- 
2.25.1

