#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    FILE *pFile;
    char outData[16000];
    char *pOutData = outData;
    char *inData;
    long fileSize;
    long readCount;

    /****************/
    /* Working case */
    /****************/

    //Create a file of all 1's
    memset (outData, 1, sizeof(outData));
    outData[15999] = "\0";

    pFile = fopen ("test1.bin", "wb");
    fwrite (outData, 1, sizeof(outData), pFile);
    fclose (pFile);

    pFile = fopen ("test1.bin", "rb");
    /* Get the file length */
    fseek (pFile, 0, SEEK_END);
    fileSize = ftell (pFile);
    rewind (pFile);

    inData = (char*)malloc(sizeof(char)*fileSize);
    if (inData == NULL) {
        return 1;
    }

    // this should succeed
    readCount = fread (inData, 1, fileSize, pFile);

    free (inData);
    inData = NULL;


    /********************************/
    /* Case that crashes Qt Creator */
    /********************************/

    pFile = fopen ("test2.bin", "rb");
    /* Get the file length */
    fseek (pFile, 0, SEEK_END);
    fileSize = ftell (pFile);
    rewind (pFile);

    inData = (char*)malloc(sizeof(char)*fileSize);
    if (inData == NULL) {
        return 1;
    }

    // this should succeed
    readCount = fread (inData, 1, 2000, pFile);

    // this should fail
    readCount = fread (inData, 1, fileSize, pFile);

    free (inData);
    return 0;
}
