Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cb59cdcb authored by Lloyd Pique's avatar Lloyd Pique Committed by Automerger Merge Worker
Browse files

libui: Fix clamping of input values in ui::Size am: 1e101369

Change-Id: Ifc53d5aef597619afc4054e306df96cdf3748e9b
parents 9c269200 1e101369
Loading
Loading
Loading
Loading
+19 −11
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@
#include <algorithm>
#include <algorithm>
#include <cstdint>
#include <cstdint>
#include <limits>
#include <limits>
#include <ostream>
#include <type_traits>
#include <type_traits>
#include <utility>
#include <utility>


@@ -113,13 +114,12 @@ struct Size {
                    std::numeric_limits<Size::remove_cv_reference_t<ToType>>::is_bounded &&
                    std::numeric_limits<Size::remove_cv_reference_t<ToType>>::is_bounded &&
                            std::numeric_limits<Size::remove_cv_reference_t<FromType>>::is_bounded,
                            std::numeric_limits<Size::remove_cv_reference_t<FromType>>::is_bounded,
                    FromType&&>::type v) {
                    FromType&&>::type v) {
        static constexpr auto toHighest = std::numeric_limits<remove_cv_reference_t<ToType>>::max();
        using BareToType = remove_cv_reference_t<ToType>;
        static constexpr auto toLowest =
        using BareFromType = remove_cv_reference_t<FromType>;
                std::numeric_limits<remove_cv_reference_t<ToType>>::lowest();
        static constexpr auto toHighest = std::numeric_limits<BareToType>::max();
        static constexpr auto fromHighest =
        static constexpr auto toLowest = std::numeric_limits<BareToType>::lowest();
                std::numeric_limits<remove_cv_reference_t<FromType>>::max();
        static constexpr auto fromHighest = std::numeric_limits<BareFromType>::max();
        static constexpr auto fromLowest =
        static constexpr auto fromLowest = std::numeric_limits<BareFromType>::lowest();
                std::numeric_limits<remove_cv_reference_t<FromType>>::lowest();


        // A clamp is needed if the range of FromType is not a subset of the range of ToType
        // A clamp is needed if the range of FromType is not a subset of the range of ToType
        static constexpr bool isClampNeeded = (toLowest > fromLowest) || (toHighest < fromHighest);
        static constexpr bool isClampNeeded = (toLowest > fromLowest) || (toHighest < fromHighest);
@@ -129,10 +129,13 @@ struct Size {
            return static_cast<ToType>(v);
            return static_cast<ToType>(v);
        }
        }


        // Otherwise we leverage implicit conversion to safely compare values of
        // Otherwise we need to carefully compare the limits of ToType (casted
        // different types, to ensure we return a value clamped to the range of
        // for the comparisons to be warning free to FromType) while still
        // ToType.
        // ensuring we return a value clamped to the range of ToType.
        return v < toLowest ? toLowest : (static_cast<ToType>(v) > toHighest ? toHighest : static_cast<ToType>(v));
        return v < static_cast<const BareFromType>(toLowest)
                ? toLowest
                : (v > static_cast<const BareFromType>(toHighest) ? toHighest
                                                                  : static_cast<ToType>(v));
    }
    }
};
};


@@ -154,5 +157,10 @@ inline bool operator<(const Size& lhs, const Size& rhs) {
    return lhs.height < rhs.height;
    return lhs.height < rhs.height;
}
}


// Defining PrintTo helps with Google Tests.
static inline void PrintTo(const Size& size, ::std::ostream* os) {
    *os << "Size(" << size.width << ", " << size.height << ")";
}

} // namespace ui
} // namespace ui
} // namespace android
} // namespace android
+1 −0
Original line number Original line Diff line number Diff line
@@ -111,6 +111,7 @@ cc_test {


cc_test {
cc_test {
    name: "Size_test",
    name: "Size_test",
    test_suites: ["device-tests"],
    shared_libs: ["libui"],
    shared_libs: ["libui"],
    srcs: ["Size_test.cpp"],
    srcs: ["Size_test.cpp"],
    cflags: ["-Wall", "-Werror"],
    cflags: ["-Wall", "-Werror"],
+10 −0
Original line number Original line Diff line number Diff line
@@ -19,8 +19,18 @@
#include <cmath>
#include <cmath>
#include <cstdlib>
#include <cstdlib>


#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic error "-Wimplicit-int-float-conversion"
#pragma clang diagnostic error "-Wconversion"
#endif // __clang__

#include <ui/Size.h>
#include <ui/Size.h>


#ifdef __clang__
#pragma clang diagnostic pop
#endif // __clang__

#include <gtest/gtest.h>
#include <gtest/gtest.h>


namespace android {
namespace android {
+7 −0
Original line number Original line Diff line number Diff line
{
  "presubmit": [
    {
      "name": "Size_test"
    }
  ]
}