On 28/08/20 23:53 +0100, Jonathan Wakely wrote:
On 29/08/20 00:20 +0200, Daniel Krügler wrote:
Am Sa., 29. Aug. 2020 um 00:12 Uhr schrieb Jonathan Wakely via
Libstdc++ <libstd...@gcc.gnu.org>:

This fixes a bug with mixed signed and unsigned types, where converting
a negative value to the unsigned result type alters the value. The
solution is to obtain the absolute values of the arguments immediately
and to perform the actual GCD or LCM algorithm on two arguments of the
same type.

In order to operate on the most negative number without overflow when
taking its absolute, use an unsigned type for the result of the abs
operation. For example, -INT_MIN will overflow, but -(unsigned)INT_MIN
is (unsigned)INT_MAX+1U which is the correct value.

libstdc++-v3/ChangeLog:

       PR libstdc++/92978
       * include/std/numeric (__abs_integral): Replace with ...
       (__detail::__absu): New function template that returns an
       unsigned type, guaranteeing it can represent the most
       negative signed value.
       (__detail::__gcd, __detail::__lcm): Require arguments to
       be unsigned and therefore already non-negative.
       (gcd, lcm): Convert arguments to absolute value as unsigned
       type before calling __detail::__gcd or __detail::__lcm.
       * include/experimental/numeric (gcd, lcm): Likewise.
       * testsuite/26_numerics/gcd/gcd_neg.cc: Adjust expected
       errors.
       * testsuite/26_numerics/lcm/lcm_neg.cc: Likewise.
       * testsuite/26_numerics/gcd/92978.cc: New test.
       * testsuite/26_numerics/lcm/92978.cc: New test.
       * testsuite/experimental/numeric/92978.cc: New test.

Tested powerpc64le-linux. Committed to trunk.

Shouldn't the overload of __absu

void __absu(bool) = delete;

still also be a template or is just the diff presentation confusing me?

Good point! It's called as __absu<U>(v) so it needs to be a function
template for the deleted one to be a candidate.

I'm not sure we really need it, since all the callers have a
static_assert ensuring it's not a bool. But if we have it, it should
be correct.

I've just pushed this fix, after testing on powerpc64le-linux.

Thanks for the review, Daniel.


commit 0789600c597ff1f8ac06e84ffb584c853d1675d1
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Sat Aug 29 18:24:08 2020

    libstdc++: Fix deleted overload of __absu(bool)
    
    libstdc++-v3/ChangeLog:
    
            * include/std/numeric (__detail::__absu(bool)): Make deleted
            function a function template, so it will be chosen for calls
            with an explicit template argument list.
            * testsuite/26_numerics/gcd/gcd_neg.cc: Add dg-prune-output.
            * testsuite/26_numerics/lcm/lcm_neg.cc: Likewise.

diff --git a/libstdc++-v3/include/std/numeric b/libstdc++-v3/include/std/numeric
index 8f2ed5c6a5e..bd70a52019b 100644
--- a/libstdc++-v3/include/std/numeric
+++ b/libstdc++-v3/include/std/numeric
@@ -95,7 +95,7 @@ namespace __detail
       return __val < 0 ? -(_Up)__val : (_Up)__val;
     }
 
-  void __absu(bool) = delete;
+  template<typename _Up> void __absu(bool) = delete;
 
   // GCD implementation
   template<typename _Tp>
diff --git a/libstdc++-v3/testsuite/26_numerics/gcd/gcd_neg.cc b/libstdc++-v3/testsuite/26_numerics/gcd/gcd_neg.cc
index 707148a2670..2e56bc650a7 100644
--- a/libstdc++-v3/testsuite/26_numerics/gcd/gcd_neg.cc
+++ b/libstdc++-v3/testsuite/26_numerics/gcd/gcd_neg.cc
@@ -50,4 +50,5 @@ test01()
 // { dg-error "must be integers" "" { target *-*-* } 135 }
 // { dg-error "must not be bool" "" { target *-*-* } 136 }
 // { dg-error "must not be bool" "" { target *-*-* } 137 }
+// { dg-prune-output "deleted function" }
 // { dg-prune-output "incomplete type .*make_unsigned" }
diff --git a/libstdc++-v3/testsuite/26_numerics/lcm/lcm_neg.cc b/libstdc++-v3/testsuite/26_numerics/lcm/lcm_neg.cc
index d4aa6b59da8..9e83fdcae0b 100644
--- a/libstdc++-v3/testsuite/26_numerics/lcm/lcm_neg.cc
+++ b/libstdc++-v3/testsuite/26_numerics/lcm/lcm_neg.cc
@@ -50,4 +50,5 @@ test01()
 // { dg-error "must be integers" "" { target *-*-* } 149 }
 // { dg-error "must not be bool" "" { target *-*-* } 150 }
 // { dg-error "must not be bool" "" { target *-*-* } 151 }
+// { dg-prune-output "deleted function" }
 // { dg-prune-output "incomplete type .*make_unsigned" }

Reply via email to