Skip to content

Commit

Permalink
fix bugzilla 21995 Struct with size uint.max or greater causes ICE
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored and dlang-bot committed Oct 2, 2024
1 parent ef02f08 commit c2fb0d4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
12 changes: 9 additions & 3 deletions compiler/src/dmd/aggregate.d
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ public uint alignmember(structalign_t alignment, uint memalignsize, uint offset)
/****************************************
* Place a field (mem) into an aggregate (agg), which can be a struct, union or class
* Params:
* loc = source location for error messages
* nextoffset = location just past the end of the previous field in the aggregate.
* Updated to be just past the end of this field to be placed, i.e. the future nextoffset
* memsize = size of field
Expand All @@ -805,8 +806,8 @@ public uint alignmember(structalign_t alignment, uint memalignsize, uint offset)
* aligned offset to place field at
*
*/
public uint placeField(ref uint nextoffset, uint memsize, uint memalignsize,
structalign_t alignment, ref uint aggsize, ref uint aggalignsize, bool isunion) @safe pure nothrow
public uint placeField(Loc loc, ref uint nextoffset, uint memsize, uint memalignsize,
structalign_t alignment, ref uint aggsize, ref uint aggalignsize, bool isunion) @trusted nothrow
{
static if (0)
{
Expand All @@ -829,7 +830,12 @@ public uint placeField(ref uint nextoffset, uint memsize, uint memalignsize,
bool overflow;
const sz = addu(memsize, actualAlignment, overflow);
addu(ofs, sz, overflow);
if (overflow) assert(0);
if (overflow)
{
error(loc, "max object size %u exceeded from adding field size %u + alignment adjustment %u + field offset %u when placing field in aggregate",
uint.max, memsize, actualAlignment, ofs);
return 0;
}

// Skip no-op for noreturn without custom aligment
if (memalignsize != 0 || !alignment.isDefault())
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -7126,7 +7126,7 @@ private extern(C++) class SetFieldOffsetVisitor : Visitor
assert(sz != SIZE_INVALID && sz < uint.max);
uint memsize = cast(uint)sz; // size of member
uint memalignsize = target.fieldalign(t); // size of member for alignment purposes
vd.offset = placeField(
vd.offset = placeField(vd.loc,
fieldState.offset,
memsize, memalignsize, vd.alignment,
ad.structsize, ad.alignsize,
Expand Down Expand Up @@ -7193,7 +7193,7 @@ private extern(C++) class SetFieldOffsetVisitor : Visitor
alignsize = memsize; // not memalignsize

uint dummy;
bfd.offset = placeField(
bfd.offset = placeField(bfd.loc,
fieldState.offset,
memsize, alignsize, bfd.alignment,
ad.structsize,
Expand Down Expand Up @@ -7395,7 +7395,7 @@ private extern(C++) class SetFieldOffsetVisitor : Visitor
/* Given the anon 'member's size and alignment,
* go ahead and place it.
*/
anond.anonoffset = placeField(
anond.anonoffset = placeField(anond.loc,
fieldState.offset,
anond.anonstructsize, anond.anonalignsize, alignment,
ad.structsize, ad.alignsize,
Expand Down
15 changes: 0 additions & 15 deletions compiler/test/fail_compilation/failCopyCtor.d

This file was deleted.

11 changes: 11 additions & 0 deletions compiler/test/fail_compilation/test21995.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* TEST_OUTPUT:
---
fail_compilation/test21995.d(10): Error: max object size 4294967295 exceeded from adding field size 3 + alignment adjustment 1 + field offset 4294967292 when placing field in aggregate
---
*/
struct S
{
ubyte[0x7ffffffe] a;
ubyte[0x7ffffffe] b;
ubyte[3] c;
}

0 comments on commit c2fb0d4

Please sign in to comment.