summaryrefslogtreecommitdiff
path: root/tests/tiff/tiff_invalid_read.c
blob: 3c87bcf6d258cb7c525557375ee1529c996c1d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
We're testing that reading corrupt TIFF files doesn't cause any memory issues,
and that the operation gracefully fails (i.e. gdImageCreateFromTiffPtr() returns
NULL).
*/

#include "gd.h"
#include "gdtest.h"


static void check_file(char *basename);
static size_t read_test_file(char **buffer, char *basename);


int main()
{
    check_file("tiff_invalid_read_1.tiff");
    check_file("tiff_invalid_read_2.tiff");
    check_file("tiff_invalid_read_3.tiff");

    return gdNumFailures();
}


static void check_file(char *basename)
{
    gdImagePtr im;
    char *buffer = NULL;
    size_t size;

    size = read_test_file(&buffer, basename);
    im = gdImageCreateFromTiffPtr(size, (void *) buffer);
    gdTestAssert(im == NULL);
    if (buffer != NULL) {
        free(buffer);
    }
}


static size_t read_test_file(char **buffer, char *basename)
{
    char *filename;
    FILE *fp;

    size_t exp_size = 0, act_size = -1;

    filename = gdTestFilePath2("tiff", basename);
    fp = fopen(filename, "rb");
    if (!gdTestAssert(fp != NULL)) goto fail3;

    if (fseek(fp, 0, SEEK_END) != 0) {
        gdTestAssert(1==0); // only increase num failures used as return values in main
        goto fail2;
    }

    exp_size = ftell(fp);
    if (!gdTestAssert(exp_size > 0)) {
        gdTestAssert(1==0); // only increase num failures used as return values in main
        goto fail2;
    }

    if (fseek(fp, 0, SEEK_SET) != 0) {
        gdTestAssert(1==0); // only increase num failures used as return values in main
        goto fail2;
    }

    *buffer = malloc(exp_size);
    if (gdTestAssert(*buffer != NULL)) {
        act_size = fread(*buffer, sizeof(**buffer), exp_size, fp);
        gdTestAssert(act_size == exp_size);
    }


fail2:
    fclose(fp);
fail3:
    free(filename);

    return act_size;
}