Backport fix for bug#18745 from master.