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

            Bug ID: 120990
           Summary: Narrowing conversion in std::function return value is
                    not reported
           Product: gcc
           Version: 15.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rohan.a.suri at gmail dot com
  Target Milestone: ---

Created attachment 61815
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=61815&action=edit
Repro of hidden narrowing conversion with std::function

Hi team,

I'd like to report an undetected case of integer narrowing conversion with the
GCC compiler for C++.

A std::function can be defined to return an int32, but the actual lambda
implementation could be returning a larger integer say int64. In such a case,
there's a hidden narrowing conversion happening from int64 to int32 which
causes data loss.

I've attached a small repro of the issue to this report.

Here's also a live example to play around with on godbolt.org:
https://godbolt.org/z/G8xYh5c36

I have tried it on x86-64 (as well as ARM64) GCC 15.1 along with compiler
flags: -Wall -Wextra and no issues are reported.

Interestingly, if I try this on ARM 32-bit GCC 15.1, it does report:

<source>: In function 'size_t bar()':
<source>:7:12: warning: unsigned conversion from 'long long int' to 'size_t'
{aka 'unsigned int'} changes value from '10000000000' to '1410065408'
[-Woverflow]
    7 |     return 10000000000; // 10 billion
      |            ^~~~~~~~~~~


Please let me know if you need any more details from my side.

Thank you.

Reply via email to