Fix #6031

This is a patch to fix #6031.  I didn't see any tests for the C++ library code, so I didn't write a test for my changes.  Did I miss something, or are there really no tests?