Don't crash if write_impl() leaves less buffer space available than expected.

This was tickled by a Clang diagnostic; Clang test case to follow.
Test for LLVM r176911