Why does g++ generate huge error log?

Viewed 181

Somewhere I saw this malformed C++ program could make g++ generate a huge error log:

int main()
{
    struct x struct z<x(x(x(x(x(x(x(y,x(y><y*,x(y*w>v<y*,w,x{};
    return 0;
}

By repeating (x many times, the error log can grow into >1GB. So this program can be used to drain a victim's system resources. While this problem can be remedied by --fmax-error, I'm curious what is happening inside g++ during its compilation that makes it spit such a log.

1 Answers

I can't really answer what particular set of things is going on, such that the errors are innumerable, but remember that C++ has possibly the most complex grammar known to man.

Part of the job of a compiler is informing the user what is wrong with a particular input, that it isn't valid (or in the case of warnings, good) C++.

You can get exponential growth of error messages when you recursively have an ambiguity, as the compiler will inform you of the set of things that it could mean. Suppose ? could mean A or B, ??? could be any of AAA, AAB, ABA, ABB, BAA, BAB, BBA, or BBB.

The compiler only knows that the 100000000th near-repetition isn't important to you when you tell it so with --fmax-error.

Related