www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14734] New: Throwing a nested class should not be legal

https://issues.dlang.org/show_bug.cgi?id=14734

          Issue ID: 14734
           Summary: Throwing a nested class should not be legal
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: pro.mathias.lang gmail.com

Throwing a nested Exception has the potential to trigger memory errors, as it
is not guaranteed that the context object will outlive the Exception. Given
Exceptions' nature, and unless the GC is used, it's actually more likely that
it won't.


Example:

````
import std.stdio;

enum LoremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse ci\
llum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

class Foo
{
    char[LoremIpsum.length] str;

    class LocalException : Exception
    {
        void print()  safe
        {
            this.outer.str[] = LoremIpsum[];
            writeln(this.outer.str);
        }

        public this(string msg, string f = __FILE__, int l = __LINE__)  safe
nothrow
        { super(msg, f, l); }
    }

    void act()  safe
    {
        throw new LocalException("Oops");
    }
}

void bar()  safe
{
    scope baz = new Foo();
    baz.act();
}

void main()  safe
{
    try bar();
    catch (Foo.LocalException e) {
        "Catched".writeln;
        e.print();
        "Foobar".writeln;
    }
}
````

This triggers a segmentation fault.

--
Jun 25 2015