The first mistake made was that when adding the fread result to the buffer, the actual size of each read byte needs to be taken into mind.
If the characters were wide characters with a byte size of 2 instead of 1, then the buffer->used counter would increase at a rate of 2x of what was actually in use.
The second mistake made was that the fread max read size was being set to the entire buffer size instead of what was available.
This should have been producing some sort of buffer overflow, but none was reported.
Instead of request a read of (buffer->size - 1), only request a size
equal to that of the available allocated space (buffer->size - buffer->used - 1).
The third mistake made was not performing sanity checking on the buffer->used and buffer->size variables.