https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117322

--- Comment #2 from Anton Kryukov <akryuk at gmail dot com> ---
(In reply to Andrew Pinski from comment #1)
> So adding:
>   if ((long)(len*4) <= 0) return;
> 
> Fixes the warning.
> 
> Looks like GCC does not realize len can't be too big to overflow.
> 
> I am not 100% sure if the warning should be better worded or there is a
> missed optimization here.

But if I remove the heap allocation branch, leaving only the one with alloca
with the possible overflow situation intact (https://godbolt.org/z/ne7bcsq6d),
the warning goes away:
//**********************************************************
#include <memory>
#include <alloca.h>
#include <type_traits>

using T = long long;
//----------------------------------------------------------
void f(T const* data, size_t len);
//----------------------------------------------------------
void h(int const* data, size_t len)
{
  T* arr;                                                 
  size_t nbytes = len * sizeof(T);                       
  arr = reinterpret_cast<T*>(alloca(nbytes));

  std::uninitialized_copy(data, data + len, arr);
  f(arr, len);
}
//*********************************************************

Reply via email to