clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name mmdbresolve.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-18/lib/clang/18 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-truncation -Wno-format-nonliteral -Wno-pointer-sign -std=gnu11 -ferror-limit 19 -fvisibility=hidden -fwrapv -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -dwarf-debug-flags /usr/lib/llvm-18/bin/clang -### --analyze -x c -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -fvisibility=hidden -fexcess-precision=fast -fstrict-flex-arrays=3 -fstack-clash-protection -fcf-protection=full -D _GLIBCXX_ASSERTIONS -fstack-protector-strong -fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing -fexceptions -Wno-format-truncation -Wno-format-nonliteral -fdiagnostics-color=always -Wno-pointer-sign -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -std=gnu11 -fPIE /builds/wireshark/wireshark/mmdbresolve.c -o /builds/wireshark/wireshark/sbout/2025-05-08-100309-3825-1 -Xclang -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /builds/wireshark/wireshark/sbout/2025-05-08-100309-3825-1 -x c /builds/wireshark/wireshark/mmdbresolve.c
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include <stdio.h> |
15 | #include <stdlib.h> |
16 | #include <string.h> |
17 | |
18 | #include <maxminddb.h> |
19 | |
20 | #define MAX_ADDR_LEN 46 |
21 | #define MMDBR_STRINGIFY(x) MMDBR_STRINGIFY_S(x) |
22 | #define MMDBR_STRINGIFY_S(s) #s |
23 | #define OUT_BUF_SIZE 65536 |
24 | |
25 | |
26 | |
27 | |
28 | #ifdef MMDB_DEBUG_SLOW |
29 | #ifdef _WIN32 |
30 | #include <Windows.h> |
31 | #endif |
32 | #endif |
33 | |
34 | static const char *co_iso_key[] = {"country", "iso_code", NULL}; |
35 | static const char *co_name_key[] = {"country", "names", "en", NULL}; |
36 | static const char *ci_name_key[] = {"city", "names", "en", NULL}; |
37 | static const char *asn_o_key[] = {"autonomous_system_organization", NULL}; |
38 | static const char *asn_key[] = {"autonomous_system_number", NULL}; |
39 | static const char *l_lat_key[] = {"location", "latitude", NULL}; |
40 | static const char *l_lon_key[] = {"location", "longitude", NULL}; |
41 | static const char *l_accuracy_key[] = {"location", "accuracy_radius", NULL}; |
42 | static const char *empty_key[] = {NULL}; |
43 | |
44 | static const char **lookup_keys[] = { |
45 | co_iso_key, |
46 | co_name_key, |
47 | ci_name_key, |
48 | asn_o_key, |
49 | asn_key, |
50 | l_lat_key, |
51 | l_lon_key, |
52 | l_accuracy_key, |
53 | empty_key |
54 | }; |
55 | |
56 | int |
57 | main(int argc, char *argv[]) |
58 | { |
59 | char addr_str[MAX_ADDR_LEN+1]; |
60 | size_t mmdb_count = 0; |
61 | MMDB_s *mmdbs = NULL, *new_mmdbs; |
62 | int mmdb_err; |
63 | |
64 | char *out_buf = (char *) malloc(OUT_BUF_SIZE); |
65 | if (out_buf == NULL) { |
| 1 | Assuming 'out_buf' is not equal to NULL | |
|
| |
66 | fprintf(stdout, "ERROR: malloc failed\n"); |
67 | return 1; |
68 | } |
69 | setvbuf(stdout, out_buf, _IOFBF, OUT_BUF_SIZE); |
70 | |
71 | fprintf(stdout, "[init]\n"); |
72 | |
73 | |
74 | |
75 | int arg_idx = 0; |
76 | while (arg_idx < argc - 1) { |
| 3 | | Assuming the condition is true | |
|
| 4 | | Loop condition is true. Entering loop body | |
|
| 11 | | Assuming the condition is false | |
|
| 12 | | Loop condition is false. Execution continues on line 103 | |
|
77 | if (strcmp(argv[arg_idx], "-f") == 0) { |
| |
78 | arg_idx++; |
79 | const char *db_arg = argv[arg_idx]; |
80 | MMDB_s try_mmdb; |
81 | mmdb_err = MMDB_open(db_arg, 0, &try_mmdb); |
82 | fprintf(stdout, "db.%zd.path: %s\n", mmdb_count, db_arg); |
83 | fprintf(stdout, "db.%zd.status: ", mmdb_count); |
84 | if (mmdb_err == MMDB_SUCCESS) { |
| 6 | | Assuming 'mmdb_err' is equal to MMDB_SUCCESS | |
|
| |
85 | mmdb_count++; |
86 | new_mmdbs = (MMDB_s *) realloc(mmdbs, mmdb_count * sizeof(MMDB_s)); |
| |
87 | if (new_mmdbs == NULL) { |
| 9 | | Assuming 'new_mmdbs' is not equal to NULL | |
|
| |
88 | free(mmdbs); |
89 | fprintf(stdout, "ERROR out of memory\n"); |
90 | return 1; |
91 | } |
92 | mmdbs = new_mmdbs; |
93 | mmdbs[mmdb_count - 1] = try_mmdb; |
94 | fprintf(stdout, "OK\n"); |
95 | fprintf(stdout, "db.%zd.type: %s\n", mmdb_count, mmdbs[mmdb_count - 1].metadata.database_type); |
96 | } else { |
97 | fprintf(stdout, "ERROR %s\n", MMDB_strerror(mmdb_err)); |
98 | } |
99 | } |
100 | arg_idx++; |
101 | } |
102 | |
103 | fprintf(stdout, "mmdbresolve.status: %s\n", mmdb_count > 0 ? "true": "false"); |
| |
104 | fprintf(stdout, "# End init\n"); |
105 | fflush(stdout); |
106 | |
107 | if (arg_idx != argc || mmdb_count < 1) { |
| 14 | | Assuming 'arg_idx' is not equal to 'argc' | |
|
108 | fprintf(stderr, "Usage: mmdbresolve -f db_file [-f db_file ...]\n"); |
| 15 | | Potential leak of memory pointed to by 'mmdbs' |
|
109 | return EXIT_FAILURE; |
110 | } |
111 | |
112 | int in_items = 0; |
113 | while (in_items != EOF) { |
114 | int gai_err; |
115 | |
116 | in_items = fscanf(stdin, "%" MMDBR_STRINGIFY(MAX_ADDR_LEN) "s", addr_str); |
117 | |
118 | if (in_items < 1) { |
119 | continue; |
120 | } |
121 | |
122 | fprintf(stdout, "[%s]\n", addr_str); |
123 | |
124 | #ifdef MMDB_DEBUG_SLOW |
125 | #ifdef _WIN32 |
126 | Sleep(1000); |
127 | #endif |
128 | #endif |
129 | |
130 | for (size_t mmdb_idx = 0; mmdb_idx < mmdb_count; mmdb_idx++) { |
131 | fprintf(stdout, "# %s\n", mmdbs[mmdb_idx].metadata.database_type); |
132 | MMDB_lookup_result_s result = MMDB_lookup_string(&mmdbs[mmdb_idx], addr_str, &gai_err, &mmdb_err); |
133 | |
134 | if (result.found_entry && gai_err == 0 && mmdb_err == MMDB_SUCCESS) { |
135 | for (size_t key_idx = 0; lookup_keys[key_idx][0]; key_idx++) { |
136 | MMDB_entry_data_s entry_data; |
137 | int status = MMDB_aget_value(&result.entry, &entry_data, lookup_keys[key_idx]); |
138 | if (status == MMDB_SUCCESS && entry_data.has_data) { |
139 | char *sep = ""; |
140 | for (int idx = 0; lookup_keys[key_idx][idx] != 0; idx++) { |
141 | fprintf(stdout, "%s%s", sep, lookup_keys[key_idx][idx]); |
142 | sep = "."; |
143 | } |
144 | switch (entry_data.type) { |
145 | case MMDB_DATA_TYPE_UTF8_STRING: |
146 | { |
147 | char len_fmt[12]; |
148 | snprintf(len_fmt, 11, ": %%.%us\n", entry_data.data_size); |
149 | fprintf(stdout, len_fmt, entry_data.utf8_string); |
150 | } |
151 | break; |
152 | case MMDB_DATA_TYPE_UINT16: |
153 | fprintf(stdout, ": %u\n", entry_data.uint16); |
154 | break; |
155 | case MMDB_DATA_TYPE_UINT32: |
156 | fprintf(stdout, ": %u\n", entry_data.uint32); |
157 | break; |
158 | case MMDB_DATA_TYPE_INT32: |
159 | fprintf(stdout, ": %d\n", entry_data.int32); |
160 | break; |
161 | case MMDB_DATA_TYPE_BOOLEAN: |
162 | fprintf(stdout, ": %s\n", entry_data.boolean ? "True" : "False"); |
163 | break; |
164 | case MMDB_DATA_TYPE_DOUBLE: |
165 | fprintf(stdout, ": %f\n", entry_data.double_value); |
166 | break; |
167 | case MMDB_DATA_TYPE_FLOAT: |
168 | fprintf(stdout, ": %f\n", entry_data.float_value); |
169 | break; |
170 | default: |
171 | fprintf(stdout, ": UNKNOWN (%u)\n", entry_data.type); |
172 | } |
173 | } |
174 | } |
175 | } else { |
176 | |
177 | } |
178 | } |
179 | fprintf(stdout, "# End %s\n", addr_str); |
180 | fflush(stdout); |
181 | } |
182 | |
183 | free(mmdbs); |
184 | |
185 | return EXIT_SUCCESS; |
186 | } |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | |
199 | |