[OT] OT: Null checks.
Walter Bright
newshound2 at digitalmars.com
Tue May 6 01:43:07 UTC 2025
On 5/4/2025 1:28 PM, Timon Gehr wrote:
> ```
> private void _enforceNoOverlap(const char[] action,
> uintptr_t ptr1, uintptr_t ptr2, const size_t bytes)
> {
> const d = ptr1 > ptr2 ? ptr1 - ptr2 : ptr2 - ptr1;
> if (d >= bytes)
> return;
> const overlappedBytes = bytes - d;
>
> UnsignedStringBuf tmpBuff = void;
> string msg = "Overlapping arrays in ";
> msg ~= action;
> msg ~= ": ";
> msg ~= overlappedBytes.unsignedToTempString(tmpBuff);
> msg ~= " byte(s) overlap of ";
> msg ~= bytes.unsignedToTempString(tmpBuff);
> assert(0, msg);
> }
> ```
I would not write logging code like that that relied on the gc being in a
working state.
> Unfortunately I had accidentally detached my debugger instead of getting the
> backtrace at the time. It is possible that this is what is happening on the
> user's machine, but I don't know. And I also have no idea where this overlap
> might occur.
>
> This just gives an invalid instruction even in dubs "release-debug" builds by
> default unless using a custom druntime build. I just don't think this should
> ever happen to anyone, even if there is some workaround, by default people will
> run into this at least once and perhaps they will not be as lucky as me and see
> it with a debugger attached.
>
The -release switch causes `assert(0, "message")` to be replaced with "ud2"
which generates a breakpoint for the debugger. It triggers an invalid opcode
exception.
You could write a signal handler to intercept that. See my other post for how to
write one.
More information about the Digitalmars-d
mailing list