Branch Coverage

c/tree.c
Criterion Covered Total %
branch 431 600 71.8


line true false branch
209 0 203 if (merge_strategy == MMDBW_MERGE_STRATEGY_UNKNOWN) {
212 0 203 if (ip_version != 4 && ip_version != 6) {
215 10 193 if (record_size != 24 && record_size != 28 && record_size != 32) {
0 10 if (record_size != 24 && record_size != 28 && record_size != 32) {
232 23 180 if (alias_ipv6) {
236 151 52 if (remove_reserved_networks) {
239 0 151 if (status != MMDBW_SUCCESS) {
281 3044 103923 if (tree->ip_version == 4 && strchr(ipstr, ':')) {
2 3042 if (tree->ip_version == 4 && strchr(ipstr, ':')) {
299 1 846 if (end_ip < start_ip) {
308 745 101 uint8_t bytes[tree->ip_version == 6 ? 16 : 4];
316 890 844 while (start_ip <= end_ip) {
334 890 0 if (MMDBW_SUCCESS != status) {
341 888 2 if (start_ip == 0) {
349 0 846 if (MMDBW_SUCCESS != status) {
358 1490 205 uint8_t bytes[family == 6 ? 16 : 4];
359 1694 1 if (resolve_ip(family, ipstr, bytes) != MMDBW_SUCCESS) {
369 9224 1694 for (int i = 0; i < length; i++) {
377 757 133 int bytes_length = tree_ip_version == 6 ? 16 : 4;
378 0 0 for (int i = 1; i <= bytes_length; i++) {
5156 890 for (int i = 1; i <= bytes_length; i++) {
388 0 0 uint8_t bytes[tree_ip_version == 6 ? 16 : 4];
391 0 0 if (NULL == inet_ntop(tree_ip_version == 6 ? AF_INET6 : AF_INET,
0 0 if (NULL == inet_ntop(tree_ip_version == 6 ? AF_INET6 : AF_INET,
404 0 890 if (start_ip > end_ip) {
408 757 133 int prefix_length = family == 6 ? 128 : 32;
415 14579 425 && (start_ip | *reverse_mask) <= end_ip
417 15004 463 && prefix_length > 0) {
14577 2 && prefix_length > 0) {
457 342079 202 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
684158 342079 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
341045 1034 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
468872 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
130028 338844 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
127827 2201 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
468872 1034 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
461 3235 338844 if (NULL == data) {
468 202 3235 strcpy((char *)data->key, key);
470 202 3235 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
0 202 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
0 202 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
6874 3437 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
2201 1236 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
3428 9 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
0 9 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
0 9 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
0 9 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
189 1260 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
900 549 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
1449 288 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
288 9 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
0 9 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
9 0 HASH_ADD_KEYPTR(hh, tree->data_table, data->key, SHA1_KEY_LENGTH, data);
481 107874 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
215748 107874 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
107874 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
152327 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
44453 107874 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
44453 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
152327 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
483 0 107874 if (NULL == data) {
487 3437 104437 if (NULL != data->data_sv) {
498 342281 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
684562 342281 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
342281 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
469988 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
127707 342281 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
127707 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
469988 0 HASH_FIND(hh, tree->data_table, key, SHA1_KEY_LENGTH, data);
500 0 342281 if (NULL == data) {
505 3437 338844 if (0 == data->reference_count) {
506 3049 388 HASH_DEL(tree->data_table, data);
202 2847 HASH_DEL(tree->data_table, data);
188 3047 HASH_DEL(tree->data_table, data);
388 2847 HASH_DEL(tree->data_table, data);
3047 188 HASH_DEL(tree->data_table, data);
1485 1750 HASH_DEL(tree->data_table, data);
1750 1485 HASH_DEL(tree->data_table, data);
974 2261 HASH_DEL(tree->data_table, data);
516 14885 108534 uint8_t *bytes = checked_malloc(tree->ip_version == 6 ? 16 : 4);
518 1 123418 if (resolve_ip(tree->ip_version, ipstr, bytes) != MMDBW_SUCCESS) {
523 16214 107204 if (NULL == strchr(ipstr, ':')) {
525 0 16214 if (prefix_length > 32) {
531 1329 14885 if (tree->ip_version == 6) {
535 0 107204 } else if (prefix_length > 128) {
553 107400 17714 int family = is_ipv4_address ? AF_INET : AF_INET6;
554 1339 123775 if (tree_ip_version == 6 && is_ipv4_address) {
565 2 125112 if (!inet_pton(family, ipstr, bytes)) {
584 4 19 if (tree->ip_version == 4) {
603 0 19 if (status != MMDBW_SUCCESS) {
608 57 19 for (size_t i = 0; i < sizeof(ipv4_aliases) / sizeof(struct network); i++) {
626 0 57 if (MMDBW_SUCCESS != status) {
716 151 0 if (status != MMDBW_SUCCESS) {
720 80 71 if (tree->ip_version == 6) {
725 80 0 if (status != MMDBW_SUCCESS) {
738 3385 231 for (size_t i = 0; i < num_networks; i++) {
751 0 3385 if (status != MMDBW_SUCCESS) {
765 106135 21 if (merge_strategy == MMDBW_MERGE_STRATEGY_UNKNOWN) {
788 249460 26335951 if (current_bit >= network->prefix_length &&
789 18433 231027 (current_record->type == MMDBW_RECORD_TYPE_EMPTY ||
790 229 18204 current_record->type == MMDBW_RECORD_TYPE_DATA ||
791 11 218 (current_record->type == MMDBW_RECORD_TYPE_NODE &&
797 93 26354280 if (current_bit == network->prefix_length &&
798 63 30 current_record->type == MMDBW_RECORD_TYPE_FIXED_NODE &&
799 63 0 new_record->type == MMDBW_RECORD_TYPE_FIXED_NODE) {
849 18 5 if (current_bit == network->prefix_length) {
855 15 3 if (current_bit > network->prefix_length) {
880 183726 26169235 new_record->type == MMDBW_RECORD_TYPE_FIXED_NODE ||
900 1600865 44 if (status != MMDBW_SUCCESS) {
905 24769062 1583851 if (!next_is_right || insert_into_both) {
914 24768678 276 if (status != MMDBW_SUCCESS) {
926 24711043 1641486 if (next_node->left_record.type == next_node->right_record.type &&
928 69560 1571926 current_record->type == MMDBW_RECORD_TYPE_NODE) {
933 0 0 if (status != MMDBW_SUCCESS) {
942 99683 3997 if (strcmp(next_node->left_record.value.key,
950 0 3997 if (status != MMDBW_SUCCESS) {
978 11 231027 if (current_record->type != MMDBW_RECORD_TYPE_EMPTY &&
979 11 0 current_record->type != MMDBW_RECORD_TYPE_DATA &&
984 221900 9138 if (current_record->type == MMDBW_RECORD_TYPE_EMPTY &&
657 221243 if (current_record->type == MMDBW_RECORD_TYPE_EMPTY &&
994 230377 0 if (status != MMDBW_SUCCESS) {
1000 226594 3783 if (new_record->type == MMDBW_RECORD_TYPE_DATA) {
1001 221179 5415 const char *const key = increment_data_reference_count(
1005 76 3707 new_record->type == MMDBW_RECORD_TYPE_NODE ||
1008 3707 0 } else if (new_record->type == MMDBW_RECORD_TYPE_EMPTY ||
1016 224962 5415 if (merged_key) {
1028 226598 3783 if (MMDBW_RECORD_TYPE_DATA != new_record->type ||
107565 119033 if (MMDBW_RECORD_TYPE_DATA != new_record->type ||
1036 5427 102138 if (MMDBW_RECORD_TYPE_DATA != record_to_set->type
1039 5419 8 || strcmp(new_record->value.key, record_to_set->value.key) == 0) {
1052 5317 102 if (cached_key != NULL) {
1078 1600913 24752048 return network->bytes[current_bit >> 3] & (1U << (~current_bit & 7));
1094 100 2 if (!(SvROK(data_from) && SvROK(data_into) &&
100 0 if (!(SvROK(data_from) && SvROK(data_into) &&
1095 99 1 SvTYPE(SvRV(data_from)) == SVt_PVHV &&
1096 1 98 SvTYPE(SvRV(data_into)) == SVt_PVHV)) {
1104 4 0 char address_string[is_ipv6 ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN];
1106 4 0 network->bytes,
1141 319 232 while (NULL != (he = hv_iternext(from))) {
1143 0 319 const char *const key = HePV(he, key_length);
1146 46 273 if (hv_exists(to, key, key_length)) {
1147 32 14 if (merge_strategy == MMDBW_MERGE_STRATEGY_RECURSE ||
1151 0 32 if (existing_value == NULL) {
1163 18 255 } else if (merge_strategy ==
1165 10 8 SvROK(value)) {
1182 0 38 if (SvROK(from) != SvROK(into)) {
1186 10 28 if (!SvROK(from)) {
1193 18 10 if (SvTYPE(SvRV(from)) == SVt_PVHV && SvTYPE(SvRV(into)) == SVt_PVHV) {
18 0 if (SvTYPE(SvRV(from)) == SVt_PVHV && SvTYPE(SvRV(into)) == SVt_PVHV) {
1197 10 0 if (SvTYPE(SvRV(from)) == SVt_PVAV && SvTYPE(SvRV(into)) == SVt_PVAV) {
10 0 if (SvTYPE(SvRV(from)) == SVt_PVAV && SvTYPE(SvRV(into)) == SVt_PVAV) {
1220 14 8 for (SSize_t i = 0; i <= new_top_index; i++) {
1224 6 8 if (from_value != NULL && into_value != NULL) {
1227 6 2 } else if (from_value != NULL) {
1228 4 2 if (merge_strategy ==
1230 2 2 SvROK(*from_value)) {
1235 2 0 } else if (into_value != NULL) {
1249 12328 2421 if (tree->ip_version == 4 && is_ipv6_address) {
60 12268 if (tree->ip_version == 4 && is_ipv6_address) {
1253 118 2303 resolve_network(tree, ipstr, is_ipv6_address ? 128 : 32);
1261 0 14689 if (MMDBW_SUCCESS != status) {
1268 0 14689 record_for_address->type == MMDBW_RECORD_TYPE_FIXED_NODE ||
1276 283 14406 if (record_for_address->type == MMDBW_RECORD_TYPE_EMPTY ||
1289 616004 416 for (int current_bit = 0; current_bit < network->prefix_length;
1294 601731 14273 (*record)->type == MMDBW_RECORD_TYPE_FIXED_NODE ||
1301 47571 554160 if (network_bit_value(network, current_bit)) {
1314 163 258556 if (record->type == MMDBW_RECORD_TYPE_DATA) {
1343 258738 0 if (status != MMDBW_SUCCESS) {
1349 258738 0 if (status != MMDBW_SUCCESS) {
1360 37021 711035 if (record->type == MMDBW_RECORD_TYPE_FIXED_NODE &&
37021 0 if (record->type == MMDBW_RECORD_TYPE_FIXED_NODE &&
1365 3385 744671 if (record->type == MMDBW_RECORD_TYPE_FIXED_EMPTY &&
3385 0 if (record->type == MMDBW_RECORD_TYPE_FIXED_EMPTY &&
1370 254741 493315 if (record->type == MMDBW_RECORD_TYPE_NODE ||
1376 230754 262561 if (record->type == MMDBW_RECORD_TYPE_DATA) {
1383 57 493258 if (record->type == MMDBW_RECORD_TYPE_ALIAS &&
0 57 if (record->type == MMDBW_RECORD_TYPE_ALIAS &&
1421 0 30 if (!file) {
1440 0 30 if (fclose(file) != 0) {
1450 0 30 if (tree->root_record.type == MMDBW_RECORD_TYPE_DATA) {
1455 30 0 if (tree->root_record.type == MMDBW_RECORD_TYPE_NODE ||
1474 51741 58826 if (node->left_record.type == MMDBW_RECORD_TYPE_DATA) {
1479 54077 56490 if (node->right_record.type == MMDBW_RECORD_TYPE_DATA) {
1509 30 0 HASH_ITER(hh, tree->data_table, item, tmp) {
1635 30 HASH_ITER(hh, tree->data_table, item, tmp) {
1665 30 HASH_ITER(hh, tree->data_table, item, tmp) {
1534 0 30 PUSHMARK(SP);
1535 0 30 EXTEND(SP, 1);
1543 0 30 if (count != 1) {
1548 0 30 if (!SvPOK(frozen)) {
1558 30 0 FREETMPS;
1576 0 29 if (fd == -1) {
1581 0 29 if (fstat(fd, &fileinfo) == -1) {
1599 105817 29 while (NULL != (thawed = thaw_network(tree, &buffer))) {
1609 105817 0 if (thawed->record->type == MMDBW_RECORD_TYPE_DATA) {
1614 0 105817 if (status != MMDBW_SUCCESS) {
1629 1664 29 while (NULL != (value = hv_iternextsv(data_hash, &key, &keylen))) {
1651 29 0 if (memcmp(maybe_separator,
1664 846536 105817 for (int i = 0; i < 8; i++) {
1673 1538 104279 if (tree->ip_version == 4) {
1708 29 105817 memcpy(&value, *buffer, 16);
1710 29 105817 return value;
1733 0 29 PUSHMARK(SP);
1734 0 29 EXTEND(SP, 1);
1742 0 29 if (count != 1) {
1747 0 29 if (!SvROK(thawed)) {
1755 29 0 FREETMPS;
1797 217471 1699 htonl(record_value_as_number(tree, &(node->right_record), args));
1802 217471 1699 if (tree->record_size == 24) {
1813 774 925 } else if (tree->record_size == 28) {
1846 219133 219207 if (record->type == MMDBW_RECORD_TYPE_NODE ||
1848 0 219133 if (record->value.node->number == node->number) {
1854 0 219133 if (record->value.node->number < node->number) {
1864 30 438310 if (record->type == MMDBW_RECORD_TYPE_ALIAS) {
1865 0 30 if (record->value.node->number == 0) {
1896 202020 1927 if (cache_record) {
1904 0 1927 if (!SvOK(data)) {
1912 0 1927 PUSHMARK(SP);
1913 0 1927 EXTEND(SP, 5);
1925 0 1927 if (count != 1) {
1930 0 1927 if (!(SvIOK(rval) || SvUOK(rval))) {
1937 1927 0 FREETMPS;
1953 0 236320 if (record_value > max_record_value(tree)) {
1965 234520 1857 return record_size == 32 ? UINT32_MAX : (uint32_t)(1 << record_size) - 1;
1978 2 184 if (MMDBW_RECORD_TYPE_NODE != tree->root_record.type &&
1998 21402 1550006 if (depth > tree_depth0(tree) + 1) {
0 1571408 if (depth > tree_depth0(tree) + 1) {
2007 785612 785796 if (record->type == MMDBW_RECORD_TYPE_NODE ||
2011 779530 6082 if (!depth_first) {
2023 6082 779530 if (depth_first) {
2039 11805 833850 return network | ((uint128_t)1 << (tree_depth0(tree) - depth));
2047 0 98 PUSHMARK(SP);
2048 0 98 EXTEND(SP, 1);
2057 0 98 if (count != 1) {
2065 98 0 FREETMPS;
2073 17666 0 HASH_FIND(hh, tree->data_table, key, strlen(key), data);
35332 17666 HASH_FIND(hh, tree->data_table, key, strlen(key), data);
17666 0 HASH_FIND(hh, tree->data_table, key, strlen(key), data);
28430 0 HASH_FIND(hh, tree->data_table, key, strlen(key), data);
10764 17666 HASH_FIND(hh, tree->data_table, key, strlen(key), data);
10764 0 HASH_FIND(hh, tree->data_table, key, strlen(key), data);
28430 0 HASH_FIND(hh, tree->data_table, key, strlen(key), data);
2075 17666 0 if (NULL != data) {
2085 5359 60 HASH_FIND(hh, tree->merge_cache, merge_cache_key, MERGE_KEY_SIZE, cache);
21436 5359 HASH_FIND(hh, tree->merge_cache, merge_cache_key, MERGE_KEY_SIZE, cache);
5317 42 HASH_FIND(hh, tree->merge_cache, merge_cache_key, MERGE_KEY_SIZE, cache);
5317 0 HASH_FIND(hh, tree->merge_cache, merge_cache_key, MERGE_KEY_SIZE, cache);
0 5317 HASH_FIND(hh, tree->merge_cache, merge_cache_key, MERGE_KEY_SIZE, cache);
0 0 HASH_FIND(hh, tree->merge_cache, merge_cache_key, MERGE_KEY_SIZE, cache);
5317 42 HASH_FIND(hh, tree->merge_cache, merge_cache_key, MERGE_KEY_SIZE, cache);
2087 42 5317 if (!cache) {
2094 5317 0 HASH_FIND(hh, tree->data_table, cache->value, SHA1_KEY_LENGTH, data);
10634 5317 HASH_FIND(hh, tree->data_table, cache->value, SHA1_KEY_LENGTH, data);
5317 0 HASH_FIND(hh, tree->data_table, cache->value, SHA1_KEY_LENGTH, data);
5319 0 HASH_FIND(hh, tree->data_table, cache->value, SHA1_KEY_LENGTH, data);
2 5317 HASH_FIND(hh, tree->data_table, cache->value, SHA1_KEY_LENGTH, data);
2 0 HASH_FIND(hh, tree->data_table, cache->value, SHA1_KEY_LENGTH, data);
5319 0 HASH_FIND(hh, tree->data_table, cache->value, SHA1_KEY_LENGTH, data);
2095 0 5317 if (data != NULL) {
2100 0 0 HASH_DEL(tree->merge_cache, cache);
0 0 HASH_DEL(tree->merge_cache, cache);
0 0 HASH_DEL(tree->merge_cache, cache);
0 0 HASH_DEL(tree->merge_cache, cache);
0 0 HASH_DEL(tree->merge_cache, cache);
0 0 HASH_DEL(tree->merge_cache, cache);
0 0 HASH_DEL(tree->merge_cache, cache);
0 0 HASH_DEL(tree->merge_cache, cache);
2112 56 42 strncpy((char *)data->key, merge_cache_key, MERGE_KEY_SIZE + 1);
2114 56 42 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 56 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 56 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
392 98 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 98 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 98 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 0 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 0 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 0 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 0 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 0 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 0 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 0 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 0 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
0 0 HASH_ADD_KEYPTR(hh, tree->merge_cache, data->key, MERGE_KEY_SIZE, data);
2121 0 203 int hash_count = HASH_COUNT(tree->data_table);
2122 0 0 if (0 != hash_count) {
2132 56 147 HASH_ITER(hh, tree->merge_cache, cache, tmp) {
98 203 HASH_ITER(hh, tree->merge_cache, cache, tmp) {
2133 98 0 HASH_DEL(tree->merge_cache, cache);
56 42 HASH_DEL(tree->merge_cache, cache);
0 42 HASH_DEL(tree->merge_cache, cache);
0 42 HASH_DEL(tree->merge_cache, cache);
42 0 HASH_DEL(tree->merge_cache, cache);
42 0 HASH_DEL(tree->merge_cache, cache);
0 42 HASH_DEL(tree->merge_cache, cache);
0 42 HASH_DEL(tree->merge_cache, cache);
2136 42 56 free(cache);
2142 0 918642 if (!ptr) {
2152 0 317634 if (result != count) {
2164 0 219170 if (result < 0) {
2166 0 219170 } else if (result != expected) {