Document FXSYS_SafeLT compare order FXSYS_SafeLT gives NaNs an order but it isn't clear without very close reading what that order is. This adds a comment and a test to clarify. Bug: 852273 Change-Id: I8456eec0f489f8e9b680cdcb5298f8e9f6314fc8 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/69390 Commit-Queue: Tom Sepez <tsepez@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_pageobjectholder_unittest.cpp b/core/fpdfapi/page/cpdf_pageobjectholder_unittest.cpp index 7760256..43f3811 100644 --- a/core/fpdfapi/page/cpdf_pageobjectholder_unittest.cpp +++ b/core/fpdfapi/page/cpdf_pageobjectholder_unittest.cpp
@@ -4,10 +4,17 @@ #include "core/fpdfapi/page/cpdf_pageobjectholder.h" +#include <algorithm> #include <limits> +#include <vector> +#include "core/fxcrt/fx_extension.h" #include "testing/gtest/include/gtest/gtest.h" +bool SafeCompare(const float& x, const float& y) { + return FXSYS_SafeLT(x, y); +} + // See https://crbug.com/852273 TEST(CPDFPageObjectHolder, GraphicsDataAsKey) { const float fMin = std::numeric_limits<float>::min(); @@ -23,6 +30,14 @@ } } + // Validate the documented sort order. + std::vector<float> data = {fMax, fInf, fNan, fMin}; + std::sort(data.begin(), data.end(), SafeCompare); + EXPECT_EQ(data[0], fMin); + EXPECT_EQ(data[1], fMax); + EXPECT_EQ(data[2], fInf); + EXPECT_EQ(std::isnan(data[3]), std::isnan(fNan)); + std::map<GraphicsData, int> graphics_map; // Insert in reverse index permuted order.
diff --git a/core/fxcrt/fx_extension.h b/core/fxcrt/fx_extension.h index 00b5ab7..f747ee4 100644 --- a/core/fxcrt/fx_extension.h +++ b/core/fxcrt/fx_extension.h
@@ -118,6 +118,7 @@ size_t FXSYS_ToUTF16BE(uint32_t unicode, char* buf); // Strict order over floating types where NaNs may be present. +// All NaNs are treated as equal to each other and greater than infinity. template <typename T> bool FXSYS_SafeEQ(const T& lhs, const T& rhs) { return (std::isnan(lhs) && std::isnan(rhs)) ||