Fix bug#9418.

Merge it with changes in bug#10395, and fix a UnitTest.