Backport possible NULL pointer crash fix from master branch