www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12118] New: Modify immutable data using throw

reply d-bugmail puremagic.com writes:
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



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: -------
Feb 09 2014
next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Feb 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12118




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: -------
Feb 09 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12118





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: -------
Feb 09 2014