Cherry-pick upstream patch to fix a segfault.