Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Fix Issue 4587 - Assert exception should not allocate #1714

Closed
wants to merge 1 commit into from
Closed
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: 9 additions & 2 deletions src/core/exception.d
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,15 @@ deprecated void setAssertHandler( AssertHandler h ) @trusted nothrow @nogc
*/
extern (C) void onAssertError( string file = __FILE__, size_t line = __LINE__ ) nothrow
{
if( _assertHandler is null )
throw new AssertError( file, line );
import core.stdc.string : memcpy;
char[1024] buffer;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare this buffer just within the if

1024 is random and a magic number, you could be concrete with char[__traits(classInstanceSize, AssertError)] buffer;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to mention the alignment issue - I don't think there is guarantee that char[x] array is properly aligned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to also mention -- this is a local buffer which you are throwing outside the function. This results in corruption. Make it a static buffer.

if( _assertHandler is null ) {
enum size = __traits(classInstanceSize, AssertError);
memcpy(buffer.ptr, typeid(AssertError).initializer().ptr, size);
auto obj = cast(AssertError) buffer.ptr;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast is ugly, could emplace be used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emplace is from Phobos, cannot be used.

obj.__ctor( file, line );
throw obj;
}
_assertHandler( file, line, null);
}

Expand Down