Under certain circumstances, fwrite writes extra data (more bytes than requested). The result of a short demonstration is the easiest way to explain. The demo tries to create two files of 2048 bytes each and checks the offset after each fwrite call to determine the number of bytes written. The first fwrite call writes two additional bytes:
len: 2048 current offset = 0 wrote 1024 bytes current offset = 1026 EXITING: offset % BLOCKSIZE = 2 len: 2048 current offset = 0 wrote 1024 bytes current offset = 1024 wrote 1024 bytes SUCCESS
The program succeeds (it writes 2048 bytes to both files) when compiling as ELF (unix binary), but it crashes (as shown above) when compiling as PE (Windows binary / executable). I tried compiling and testing with:
Ubuntu 14.04 and gcc 4.8.2 - SUCCESS WINE 1.6.2 and mingw 4.8.2 - FAIL Windows 7 and mingw 4.8.2 - FAIL Windows 7 and Visual Studio 2013 - FAIL
The actual data in the buffer passed to fwrite affects the number of extra bytes written, but this happens almost every time (unless you write NULL bytes).
main.c:
#include <stdio.h>
stub.h and stub2.h generated from 2048 bytes / dev / urandom and 2048 bytes from / dev / zero (respectively) using xxd . For instance:
dd if=/dev/urandom of=stub2.exe bs=2048 count=1 xxd -i stub.exe stub.h dd if=/dev/zero of=stub2.exe bs=2048 count=1 xxd -i stub2.exe stub2.h
source share