mgorny created this revision.
mgorny added reviewers: labath, emaste, krytarowski, shafik.
mgorny requested review of this revision.

Fix the fill_clamp() function to handle signed source types.  Make sure
that the source value is always non-negative, and cast it to unsigned
when verifying the upper bound.  This fixes compiler warnings about
comparing unsigned and signed types.


https://reviews.llvm.org/D113519

Files:
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp


Index: 
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
===================================================================
--- 
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
+++ 
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
@@ -771,8 +771,9 @@
 
 template <typename T, typename U>
 static void fill_clamp(T &dest, U src, typename T::value_type fallback) {
-  dest = src <= std::numeric_limits<typename T::value_type>::max() ? src
-                                                                   : fallback;
+  using UU = typename std::make_unsigned<U>::type;
+  constexpr auto T_max = std::numeric_limits<typename T::value_type>::max();
+  dest = src >= 0 && static_cast<UU>(src) <= T_max ? src : fallback;
 }
 
 GDBRemoteCommunication::PacketResult


Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
===================================================================
--- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
+++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
@@ -771,8 +771,9 @@
 
 template <typename T, typename U>
 static void fill_clamp(T &dest, U src, typename T::value_type fallback) {
-  dest = src <= std::numeric_limits<typename T::value_type>::max() ? src
-                                                                   : fallback;
+  using UU = typename std::make_unsigned<U>::type;
+  constexpr auto T_max = std::numeric_limits<typename T::value_type>::max();
+  dest = src >= 0 && static_cast<UU>(src) <= T_max ? src : fallback;
 }
 
 GDBRemoteCommunication::PacketResult
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to