fix the possibility of writing one byte past the end of a buffer.