Bug 24596 – Rebindable2 corrupts objects

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2024-06-10T09:48:16Z
Last change time
2024-06-16T06:49:22Z
Keywords
pull
Assigned to
No Owner
Creator
FeepingCreature

Comments

Comment #0 by default_357-line — 2024-06-10T09:48:16Z
object.destroy is, perhaps appropriately, the most destructive function ever written. It does something useful (resetting the value and calling the destructor) for structs and struct derivatives, but something horrible (destroying the *class data*) for objects. So you're always tempted to use it, but you have to remember, every time, to disable the cases where it causes silent future crashes. I forgot this time. ``` module test; import std.algorithm; class A { int i; int getI() => i; this(int i) { this.i = i; } } void main() { auto arr = [new A(2), new A(3)]; auto max = arr.maxElement!(a => a.getI); // Due to maxElement calling extremum which uses Rebindable2 // which calls destroy // the vtable pointer of arr[0] is now zeroed out. assert(arr[0].getI == 2); } ```
Comment #1 by dlang-bot — 2024-06-10T09:54:28Z
@FeepingCreature updated dlang/phobos pull request #9012 "Fix bugzilla 24596: std.typecons.Rebindable2: Don't destroy classes! Only destroy structs!" fixing this issue: - Fix bugzilla issue 24596: std.typecons.Rebindable2: Don't destroy classes! Only destroy structs! https://github.com/dlang/phobos/pull/9012
Comment #2 by dlang-bot — 2024-06-10T15:10:26Z
dlang/phobos pull request #9012 "Fix bugzilla 24596: std.typecons.Rebindable2: Don't destroy classes! Only destroy structs!" was merged into stable: - ed41b39f5e8cef51aa84ee781c0d77072f1dadf9 by Mathis Beer: Fix bugzilla issue 24596: std.typecons.Rebindable2: Don't destroy classes! Only destroy structs! https://github.com/dlang/phobos/pull/9012
Comment #3 by dlang-bot — 2024-06-16T06:49:22Z
dlang/phobos pull request #9015 "merge stable" was merged into master: - 539dc473ba79db0e5e8cf9ee059a5cc137b88bda by FeepingCreature: Fix bugzilla issue 24596: std.typecons.Rebindable2: Don't destroy classes! Only destroy structs! (#9012) https://github.com/dlang/phobos/pull/9015