From 956a272d622af303859d7239c21272116c108b58 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 11 Feb 2022 07:17:51 -0500 Subject: Remove abs calls and pick correct floating point epsilon values (fixes #641) --- README-maintainer | 5 +++++ qpdf/qpdf-ctest.c | 2 +- qpdf/test_driver.cc | 6 +++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README-maintainer b/README-maintainer index 76a4c59c..4b36cf14 100644 --- a/README-maintainer +++ b/README-maintainer @@ -98,6 +98,11 @@ CODING RULES * Avoid atoi. Use QUtil::string_to_int instead. It does overflow/underflow checking. +* Avoid certain functions that tend to be macros or create compilation + errors on some platforms. Known cases: strcasecmp, abs. Avoid min + and max. If needed, std::min and std::max are okay to use in C++ + code with included. + * Remember to avoid using `operator[]` with `std::string` or `std::vector`. Instead, use `at()`. See README-hardening.md for details. diff --git a/qpdf/qpdf-ctest.c b/qpdf/qpdf-ctest.c index fcaf3ce0..36a50f11 100644 --- a/qpdf/qpdf-ctest.c +++ b/qpdf/qpdf-ctest.c @@ -714,7 +714,7 @@ static void test25(char const* infile, double d = 0.0; assert(qpdf_oh_get_value_as_number(qpdf, p_bool, &d) == QPDF_FALSE); assert((qpdf_oh_get_value_as_number(qpdf, p_int, &d) == QPDF_TRUE) && - (((d - 1.0) * (d - 1.0)) < 1e-100)); + ((d - 1.0) < 1e-6) && ((d - 1.0) > -1e-6)); assert(qpdf_oh_get_type_code(qpdf, p_int) == ot_integer); assert(strcmp(qpdf_oh_get_type_name(qpdf, p_int), "integer") == 0); assert(qpdf_oh_is_real(qpdf, p_real) && diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index 65ad4e52..dcae002f 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -3293,11 +3293,11 @@ static void test_85(QPDF& pdf, char const* arg2) assert(s == "42.0"); double num = 0.0; assert(oh_i.getValueAsNumber(num)); - assert(abs(num - 1.0) < 1e-100); + assert(((num - 1.0) < 1e-6) && (num - 1.0 > -1e-6)); assert(oh_r.getValueAsNumber(num)); - assert(abs(num - 42.0) < 1e-100); + assert(((num - 42.0) < 1e-6) && (num - 42.0 > -1e-6)); assert(! oh_b.getValueAsNumber(num)); - assert(abs(num - 42.0) < 1e-100); + assert(((num - 42.0) < 1e-6) && (num - 42.0 > -1e-6)); s = ""; assert(oh_n.getValueAsName(s)); assert(s == "/Test") ; -- cgit v1.2.3-54-g00ecf