Fixed the buffer-has-space check in write_buf().