Bug 21116 – onArrayCastError is horribly unsafe

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-08-05T02:10:31Z
Last change time
2020-08-06T21:47:23Z
Keywords
pull
Assigned to
No Owner
Creator
Steven Schveighoffer

Comments

Comment #0 by schveiguy — 2020-08-05T02:10:31Z
If you cast an array incorrectly, you get a blank message, or an incomplete message, or you may get junk. That's because onArrayCastError (which is @trusted) uses a stack buffer for its message, and then throws an assert error with the reference to that message! Not only that, but it also incorrectly converts numbers into stack strings, and then references those in an array after they are gone. I think there are only 2 valid ways out -- 1: allocate the string in the static data segment, or 2: allocate it on the C heap. I'm not sure which one to do. Both have drawbacks. Obviously, we can't use the GC as this is a core language feature. Using the C heap isn't great either, but is a reasonable solution. Using a static data segment one has to do purity acrobatics. And it may be really difficult to only allocate the segment if an array cast occurs. I'm leaning towards doing a C heap allocation.
Comment #1 by elpenguino+D — 2020-08-05T02:23:34Z
It also assumes that assert() accepts null-terminated strings. On windows, it does not, so garbage gets printed fairly reliably there.
Comment #2 by dlang-bot — 2020-08-05T02:31:45Z
@schveiguy created dlang/druntime pull request #3185 "Fix issue 21116 - Do not squirrel stack pointers away in AssertErrors." fixing this issue: - Fix issue 21116 - Do not squirrel stack pointers away in AssertErrors. https://github.com/dlang/druntime/pull/3185
Comment #3 by dlang-bot — 2020-08-06T21:47:23Z
dlang/druntime pull request #3185 "Fix issue 21116 - Do not squirrel stack pointers away in AssertErrors." was merged into master: - d48c3b7d8475a18d3457e7a18929b104f906a82e by Steven Schveighoffer: Fix issue 21116 - Do not squirrel stack pointers away in AssertErrors. https://github.com/dlang/druntime/pull/3185