Thread overview
[Issue 12118] New: Modify immutable data using throw
Feb 09, 2014
Tim
Feb 09, 2014
Andrej Mitrovic
Feb 09, 2014
Andrej Mitrovic
Feb 10, 2014
Kenji Hara
February 09, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12118

           Summary: Modify immutable data using throw
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: tim.dlang@t-online.de


--- Comment #0 from Tim <tim.dlang@t-online.de> 2014-02-09 04:56:54 PST ---
It is possible to throw an immutable Exception and catch it as mutable. Using this immutable data can be modified.

Example Code:

class Dummy: Exception
{
    int[] data;
    @safe pure nothrow this(immutable int[] data) immutable
    {
        super("Dummy");
        this.data = data;
    }
}
@safe pure void modifyImmutable(immutable int[] data)
{
    try
    {
        immutable Dummy e = new immutable Dummy(data);
        throw e;
    }
    catch(Dummy e)
    {
        e.data[1] = 42;
    }
}
@safe pure void main()
{
    immutable int[] data = [1,2,3];
    assert(data == [1,2,3]);
    modifyImmutable(data);
    assert(data == [1,42,3]);
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 09, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12118


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-02-09 12:48:38 PST ---
Interesting.. but I don't see how this can be solved except disallowing throwing non-mutable objects. Otherwise you'd end up in a situation where "try { } catch (Throwable) { }" could let an Exception through because it's non-mutable.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 09, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12118



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-02-09 12:49:16 PST ---
Also since Exceptions can be chained I don't think it makes sense to allow throwing non-mutable objects.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 10, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12118



--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2014-02-09 23:01:11 PST ---
(In reply to comment #0)
At least, defining immutable constructor in derived classes of Throwable should
be disallowed.

> class Dummy: Exception
> {
>     int[] data;
>     @safe pure nothrow this(immutable int[] data) immutable
>     {
>         super("Dummy");

   --> during immutable object construction, mutable super constructor should
not
be invoked.

>         this.data = data;
>     }
> }

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------