Bug 97287 - Warn for expanding range of an arithmetic type
Summary: Warn for expanding range of an arithmetic type
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Keywords: diagnostic
Depends on:
Reported: 2020-10-04 17:21 UTC by Matthew Wilcox
Modified: 2020-10-06 08:56 UTC (History)
0 users

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Matthew Wilcox 2020-10-04 17:21:58 UTC
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; }