Fix hangs and segfaults when egregiously long symbol is used.

Backport of #8241