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

            Bug ID: 97287
           Summary: Warn for expanding range of an arithmetic type
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matthew at wil dot cx
  Target Milestone: ---

I've just fixed multiple instances of bugs that look a lot like function f()
when they should have been function g().  This affects filesystems in Linux
which have to remember to cast an unsigned long to an off_t before shifting (or
there will be a bug on 32-bit kernels when dealing with files that are larger
than 4GB).

When I looked for a warning option to add, I thought -Warith-conversions might
do the job, but it doesn't.  Maybe this functionality should be added there, or
maybe it should have its own warning.

I think what we're looking for is an operation which expands the range of the
type (left shift, multiplication, addition; maybe subtraction?) and the rules
of C require the operation to be done in a narrower type, but the result is
assigned to a wider type.

unsigned long long f(unsigned int i) { return i * 4096; }
unsigned long long g(unsigned int i) { return i * 4096ULL; }

Reply via email to