Skip to content
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

Fix 24855 - VRP fails to prevent overflow after division #17060

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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
7 changes: 6 additions & 1 deletion compiler/src/dmd/intrange.d
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ struct IntRange
return widest();

// Don't treat the whole range as divide by 0 if only one end of a range is 0.
// Issue 15289
// https://issues.dlang.org/show_bug.cgi?id=15289
if (rhs.imax.value == 0)
{
rhs.imax.value--;
Expand All @@ -681,6 +681,11 @@ struct IntRange
{
return IntRange(imin / rhs.imax, imax / rhs.imin);
}
else if (rhs.imin.negative && !rhs.imax.negative) // divisor spans [-1, 0, 1]
{
SignExtendedNumber[4] bdy = [-imin, imin, -imax, imax];
return IntRange.fromNumbers4(bdy.ptr);
}
else
{
// [a,b] / [c,d] = [min (a/c, a/d, b/c, b/d), max (a/c, a/d, b/c, b/d)]
Expand Down
5 changes: 5 additions & 0 deletions compiler/test/compilable/testVRP.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// https://issues.dlang.org/show_bug.cgi?id=3147
// https://issues.dlang.org/show_bug.cgi?id=6000
// https://issues.dlang.org/show_bug.cgi?id=5225
// https://issues.dlang.org/show_bug.cgi?id=24855

void add()
{
Expand Down Expand Up @@ -100,6 +101,10 @@ void divideFail()
short w;
byte y;
static assert(!__traits(compiles, y = w / -1));
static assert(!__traits(compiles, y = y / w));

short z;
static assert(!__traits(compiles, z = w / z + 1));
}

void plus1Fail()
Expand Down
2 changes: 1 addition & 1 deletion druntime/src/core/internal/gc/impl/conservative/gc.d
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ short[PAGESIZE / 16][Bins.B_NUMSMALL + 1] calcBinBase()

foreach (i, size; binsize)
{
short end = (PAGESIZE / size) * size;
short end = cast(short) ((PAGESIZE / size) * size);
short bsz = size / 16;
foreach (off; 0..PAGESIZE/16)
{
Expand Down
Loading