Skip to content

Cap IntRange::Width to MaxWidth #145356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10805,10 +10805,9 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
return std::nullopt;

// If the range was previously non-negative, we need an extra bit for the
// sign bit. If the range was not non-negative, we need an extra bit
// because the negation of the most-negative value is one bit wider than
// that value.
return IntRange(SubRange->Width + 1, false);
// sign bit. Otherwise, we need an extra bit because the negation of the
// most-negative value is one bit wider than that value.
return IntRange(std::min(SubRange->Width + 1, MaxWidth), false);
}

case UO_Not: {
Expand All @@ -10825,7 +10824,9 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,

// The width increments by 1 if the sub-expression cannot be negative
// since it now can be.
return IntRange(SubRange->Width + (int)SubRange->NonNegative, false);
return IntRange(
std::min(SubRange->Width + (int)SubRange->NonNegative, MaxWidth),
false);
}

default:
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Sema/implicit-int-conversion-on-int.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ unsigned long long test_ull(unsigned long long x) {
unsigned _BitInt(16) test_unsigned_bit_int(unsigned _BitInt(16) x) {
return -x;
}

unsigned test_shift_minus(int i) {
return -(1 << i);
}

unsigned test_shift_not(int i) {
return ~(1 << i);
}