Backport fix to bug #18749 to Emacs-24 branch.