File: bad_pointers_t.c

package info (click to toggle)
python-maxminddb 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,600 kB
  • sloc: ansic: 7,565; python: 1,711; perl: 987; makefile: 273; sh: 190
file content (60 lines) | stat: -rw-r--r-- 1,843 bytes parent folder | download | duplicates (2)
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
#include "maxminddb_test_helper.h"

void run_tests(int mode, const char *mode_desc) {
    const char *filename = "MaxMind-DB-test-broken-pointers-24.mmdb";
    char *path = test_database_path(filename);
    MMDB_s *mmdb = open_ok(path, mode, mode_desc);
    free(path);

    {
        const char *ip = "1.1.1.16";
        MMDB_lookup_result_s result =
            lookup_string_ok(mmdb, ip, filename, mode_desc);

        MMDB_entry_data_s entry_data;
        int status = MMDB_get_value(&result.entry, &entry_data, NULL);

        cmp_ok(status,
               "==",
               MMDB_INVALID_DATA_ERROR,
               "MMDB_get_value returns MMDB_INVALID_DATA_ERROR for bad pointer "
               "in data section");

        MMDB_entry_data_list_s *entry_data_list;
        status = MMDB_get_entry_data_list(&result.entry, &entry_data_list);

        cmp_ok(status,
               "==",
               MMDB_INVALID_DATA_ERROR,
               "MMDB_get_entry_data_list returns MMDB_INVALID_DATA_ERROR for "
               "bad pointer in data section");

        // This is not necessary as on error we should not need to free
        // anything. However test that it is safe to do so. See change in
        // 1.12.2.
        MMDB_free_entry_data_list(entry_data_list);
    }

    {
        const char *ip = "1.1.1.32";

        int gai_error, mmdb_error;
        MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);

        cmp_ok(mmdb_error,
               "==",
               MMDB_CORRUPT_SEARCH_TREE_ERROR,
               "MMDB_lookup_string sets mmdb_error to "
               "MMDB_CORRUPT_SEARCH_TREE_ERROR when a search tree record "
               "points outside the data section");
    }

    MMDB_close(mmdb);
    free(mmdb);
}

int main(void) {
    plan(NO_PLAN);
    for_all_modes(&run_tests);
    done_testing();
}