[glibmm] ustring: Avoid warning due to type confusion



commit 27309b1eaae29221543c88b52b56d59c27c1974a
Author: Daniel Boles <dboles src gnome org>
Date:   Mon Sep 4 19:14:53 2017 +0100

    ustring: Avoid warning due to type confusion
    
    error: comparison of unsigned expression >= 0 is always true
      [-Werror=type-limits]
           if (index >= 0 && index < ilist.size())
    
    digit_value() - N is an int, but ilist.size() is an std::size_t. There’s
    rarely a nice way to handle such situations, but here we have an OK one:
    
    As ilist.size() is static_assert()ed in compose() to be in range of int,
    we can safely convert to int for use as the bound. For clarity, convert
    to a new int, rather than comparing to static_cast<int>(ilist.size()).
    
    Using an int is clearer than e.g. relying on std::size_t underflowing to
    its max if digit_value() returns < 0 and hence not being < ilist.size().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=784211

 glib/glibmm/ustring.cc |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
---
diff --git a/glib/glibmm/ustring.cc b/glib/glibmm/ustring.cc
index 2668347..976289f 100644
--- a/glib/glibmm/ustring.cc
+++ b/glib/glibmm/ustring.cc
@@ -1311,9 +1311,10 @@ ustring::compose_private(const Glib::ustring& fmt, std::initializer_list<const u
     }
     else
     {
-      const std::size_t index = Ascii::digit_value(stop[1]) - 1;
+      const int index = Ascii::digit_value(stop[1]) - 1;
+      const int size = ilist.size();
 
-      if (index >= 0 && index < ilist.size())
+      if (index >= 0 && index < size)
       {
         result.append(start, stop - start);
         result += (*(ilist.begin() + index))->raw();


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