When appending to a file, the buffer size is increased in chunks, and so the actual length of the file could be less than the length of the allocated buffer.
When using mmap on an such a file, content from the over-allocated buffer is being written beyond the allocated memory - causing corruption.