D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7926 - stack overflow on recursive string mixin
Summary: stack overflow on recursive string mixin
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: ice
Depends on:
Blocks:
 
Reported: 2012-04-16 09:11 UTC by Artem Borisovskiy
Modified: 2014-04-17 10:12 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Artem Borisovskiy 2012-04-16 09:11:16 UTC
$ cat > x.d
enum string a = "mixin(a);";
mixin(a);
$ dmd x.d
Segmentation fault

Tested on recent DMD >2.059 from master branch of GIT repository: HEAD is at 778aea7fa8fe9ce3be6f70c7c99b69fd17d6e0b1
Comment 1 SomeDude 2012-04-21 12:36:27 UTC
On 2.059 Win32
PS E:\DigitalMars\dmd2\samples> dmd -c bug.d
Stack overflow

which I guess is what's to be expected.
Comment 2 Kenji Hara 2014-04-17 08:33:46 UTC
I don't think this is a bug. The infinite recursive expansion is the intended behavior of the code. We can write arbitrary code in this kind.

// mutual recursion version
enum string a = "mixin(b);";
enum string b = "mixin(a);";
mixin(a);


// infinite code bloating version
string a(string num = "1")
{
    string inc(string num)
    {
        return (num.length == 0 ? "1" :
                num[$-1] == '9' ? inc(num[0..$-1]) ~ "0" :
                num[0..$-1] ~ cast(char)(num[$-1] + 1));
    }
    return `enum a`~num~` = a("`~inc(num)~`"); `~
           `/*pragma(msg, a`~num~`);*/` ~
           `mixin(a`~num~`);`;
}
mixin(a);
Comment 3 Ivan Kazmenko 2014-04-17 09:52:45 UTC
Still, the diagnostics can be better than just "Error: out of memory".  At least showing a [reasonably-sized tail of] what consumed all that memory.

On the other hand, it is tricky to log when you already have no memory, and adding logging information for such cases will probably increase the memory footprint in the general case, too.
Comment 4 Artem Borisovskiy 2014-04-17 10:12:25 UTC
(In reply to Ivan Kazmenko from comment #3)
> Still, the diagnostics can be better than just "Error: out of memory".  At
> least showing a [reasonably-sized tail of] what consumed all that memory.
That's right. "Segmentation fault" gives no clue on what's went bad, and it happens at compile time, so it's pretty hard to find the source of the problem. You might know your own sources well, but digging into the compiler sources is not an option for most, even experienced, developers.