### Bug Summary

File: | builds/wireshark/wireshark/ui/qt/main_application.cpp
Warning: | line 477, column 13 Potential memory leak

### Annotated Source Code

Press '?' to see keyboard shortcuts

Show analyzer invocation
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 main_application.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -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 -fhalf-no-semantic-interposition -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-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/build/ui/qt -isystem /builds/wireshark/wireshark/ui/qt -isystem /builds/wireshark/wireshark/ui/qt/lua_debugger -isystem /usr/include/x86_64-linux-gnu/qt6/QtConcurrent -isystem /usr/include/x86_64-linux-gnu/qt6 -isystem /usr/include/x86_64-linux-gnu/qt6/QtCore -isystem /usr/lib/x86_64-linux-gnu/qt6/mkspecs/linux-g++ -isystem /usr/include/x86_64-linux-gnu/qt6/QtNetwork -isystem /usr/include/x86_64-linux-gnu/qt6/QtPrintSupport -isystem /usr/include/x86_64-linux-gnu/qt6/QtGui -isystem /usr/include/x86_64-linux-gnu/qt6/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt6/QtSvg -isystem /usr/include/x86_64-linux-gnu/qt6/QtMultimedia -isystem /usr/include/x86_64-linux-gnu/qt6/QtDBus -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D QT_CONCURRENT_LIB -D QT_CORE_LIB -D QT_DBUS_LIB -D QT_GUI_LIB -D QT_MULTIMEDIA_LIB -D QT_NETWORK_LIB -D QT_PRINTSUPPORT_LIB -D QT_SVG_LIB -D QT_WIDGETS_LIB -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build/ui/qt/qtui_autogen/include -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/x86_64-linux-gnu/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/backward -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../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-nonliteral -std=c++17 -fdeprecated-macro -ferror-limit 19 -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-07-20-100411-3661-1 -x c++ /builds/wireshark/wireshark/ui/qt/main_application.cpp

Keyboard shortcuts:

- Use 'j/k' keys for keyboard navigation
- Use 'Shift+S' to show/hide relevant lines
- Use '?' to toggle this window

Close
Show only relevant lines Show control flow arrows

1 | /* main_application.cpp
2 | *
3 | * Wireshark - Network traffic analyzer
4 | * By Gerald Combs <gerald@wireshark.org>
5 | * Copyright 1998 Gerald Combs
6 | *
7 | * SPDX-License-Identifier: GPL-2.0-or-later
8 | */
9 |
10 | // warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
11 | #ifdef _MSC_VER
12 | #pragma warning(push)
13 | #pragma warning(disable : 4267)
14 | #endif
15 |
16 | #define WS_LOG_DOMAIN"Main" LOG_DOMAIN_MAIN"Main"
17 |
18 | #include "main_application.h"
19 |
20 | #include <algorithm>
21 | #include <errno(*__errno_location ()).h>
22 |
23 | #include "wsutil/filesystem.h"
24 | #include "app/application_flavor.h"
25 |
26 | #include "epan/addr_resolv.h"
27 | #include "epan/column-utils.h"
28 | #include "epan/disabled_protos.h"
29 | #include "epan/ftypes/ftypes.h"
30 | #include "epan/prefs.h"
31 | #include "epan/proto.h"
32 | #include "epan/tap.h"
33 | #include "epan/timestamp.h"
34 | #include "epan/decode_as.h"
35 | #include "epan/dfilter/dfilter-macro.h"
36 |
37 | #include "ui/commandline.h"
38 | #include "ui/decode_as_utils.h"
39 | #include "ui/preference_utils.h"
40 | #include "ui/language.h"
41 | #include "ui/recent.h"
42 | #include "ui/simple_dialog.h"
43 | #include "ui/util.h"
44 |
45 | #include <ui/qt/utils/qt_ui_utils.h>
46 | #include <ui/qt/utils/color_utils.h>
47 | #include <ui/qt/utils/software_update.h>
48 | #include <ui/qt/utils/theme_manager.h>
49 | #include "coloring_rules_dialog.h"
50 |
51 | #include "epan/color_filters.h"
52 |
53 | #include "extcap.h"
54 | #ifdef HAVE_LIBPCAP1
55 | #include <capture/iface_monitor.h>
56 | #endif
57 |
58 | #include "wsutil/filter_files.h"
59 | #include "ui/capture_globals.h"
60 | #include "ui/file_dialog.h"
61 | #include "ui/recent_utils.h"
62 |
63 | #ifdef HAVE_LIBPCAP1
64 | #include "ui/capture.h"
65 | #endif
66 |
67 | #include "wsutil/utf8_entities.h"
68 |
69 | #ifdef _WIN32
70 | # include "wsutil/file_util.h"
71 | # include <QMessageBox>
72 | # include <QSettings>
73 | #endif /* _WIN32 */
74 |
75 | #include <ui/qt/capture_file.h>
76 |
77 | #include <ui/qt/main_window.h>
78 | #include <ui/qt/manager/interface_list_manager.h>
79 | #include <ui/qt/main_status_bar.h>
80 | #include <ui/qt/utils/workspace_state.h>
81 | #include <ui/qt/utils/theme_styler.h>
82 |
83 | #include <QAction>
84 | #include <QApplication>
85 | #include <QColorDialog>
86 | #include <QDesktopServices>
87 | #include <QDir>
88 | #include <QEvent>
89 | #include <QFileOpenEvent>
90 | #include <QFontInfo>
91 | #include <QFontMetrics>
92 | #include <QLibraryInfo>
93 | #include <QLocale>
94 | #include <QMainWindow>
95 | #include <QMutableListIterator>
96 | #include <QProxyStyle>
97 | #include <QSocketNotifier>
98 | #include <QThreadPool>
99 | #include <QUrl>
100 | #include <qmath.h>
101 |
102 | #include <QMimeDatabase>
103 |
104 | #include <QStyleHints>
105 |
106 | #if QT_VERSION((6<<16)|(10<<8)|(2)) >= QT_VERSION_CHECK(6, 5, 0)((6<<16)|(5<<8)|(0)) && defined(Q_OS_WIN)
107 | #include <QStyleFactory>
108 | #endif
109 |
110 | #ifdef _MSC_VER
111 | #pragma warning(pop)
112 | #endif
113 |
114 | MainApplication *mainApp;
115 |
116 | // XXX - Copied from ui/gtk/file_dlg.c
117 |
118 | static QHash<int, QList<QAction *> > dynamic_menu_groups_;
119 | static QHash<int, QList<QAction *> > added_menu_groups_;
120 | static QHash<int, QList<QAction *> > removed_menu_groups_;
121 |
122 | QString MainApplication::window_title_separator_ = QString::fromUtf8(" " UTF8_MIDDLE_DOT"\u00b7" " ");
123 |
124 | // QMimeDatabase parses a large-ish XML file and can be slow to initialize.
125 | // Do so in a worker thread as early as possible.
126 | // https://github.com/lxde/pcmanfm-qt/issues/415
127 | class MimeDatabaseInitThread : public QRunnable
128 | {
129 | private:
130 | void run() override
131 | {
132 | QMimeDatabase mime_db;
133 | mime_db.mimeTypeForData(QByteArray());
134 | }
135 | };
136 |
137 | void
138 | topic_action(topic_action_e action)
139 | {
140 | if (mainApp) mainApp->helpTopicAction(action);
141 | }
142 |
143 | /* write all capture filenames of the menu to the user's recent file */
144 | extern "C" void menu_recent_file_write_all(FILE *rf) {
145 |
146 | const QList<RecentFileInfo>& recentFiles = WorkspaceState::instance()->recentCaptureFiles();
147 | int rFSize = static_cast<int>(recentFiles.size());
148 | for (int i = 0; i < rFSize; i++) {
149 | const RecentFileInfo& rfi = recentFiles.at(i);
150 |
151 | QString cf_name = rfi.filename;
152 | if (!cf_name.isNull()) {
153 | fprintf (rf, RECENT_KEY_CAPTURE_FILE"recent.capture_file" ": %s\n", qUtf8Printable(cf_name)QtPrivate::asString(cf_name).toUtf8().constData());
154 | }
155 | }
156 | }
157 |
158 | void MainApplication::refreshPacketData()
159 | {
160 | if (host_name_lookup_process()) {
161 | emit addressResolutionChanged();
162 | } else if (col_data_changed()) {
163 | emit columnDataChanged();
164 | }
165 | }
166 |
167 | void MainApplication::updateTaps()
168 | {
169 | draw_tap_listeners(false);
170 | }
171 |
172 | QDir MainApplication::openDialogInitialDir() {
173 | return QDir(get_open_dialog_initial_dir());
174 | }
175 |
176 | void MainApplication::setLastOpenDirFromFilename(const QString file_name)
177 | {
178 | /* XXX - Use canonicalPath() instead of absolutePath()? */
179 | QString directory = QDir::toNativeSeparators(QFileInfo(file_name).absolutePath());
180 | /* XXX - printable? */
181 | set_last_open_dir(qUtf8Printable(directory)QtPrivate::asString(directory).toUtf8().constData());
182 | }
183 |
184 | void MainApplication::helpTopicAction(topic_action_e action)
185 | {
186 | QString url = gchar_free_to_qstring(topic_action_url(action));
187 |
188 | if (!url.isEmpty()) {
189 | QDesktopServices::openUrl(QUrl(QDir::fromNativeSeparators(url)));
190 | }
191 | }
192 |
193 | void MainApplication::setConfigurationProfile(const char *profile_name, bool write_recent_file)
194 | {
195 | char *rf_path;
196 | int rf_open_errno;
197 | char *err_msg = NULL__null;
198 | const char* env_prefix = application_configuration_environment_prefix();
199 |
200 | /* First check if profile exists */
201 | if (!profile_exists(env_prefix, profile_name, false)) {
202 | if (profile_exists(env_prefix, profile_name, true)) {
203 | char *pf_dir_path, *pf_dir_path2, *pf_filename;
204 | /* Copy from global profile */
205 | if (create_persconffile_profile(env_prefix, profile_name, &pf_dir_path) == -1) {
206 | simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK0x01,
207 | "Can't create directory\n\"%s\":\n%s.",
208 | pf_dir_path, g_strerror(errno(*__errno_location ())));
209 |
210 | g_free(pf_dir_path)(__builtin_object_size ((pf_dir_path), 0) != ((size_t) - 1)) ? g_free_sized (pf_dir_path, __builtin_object_size ((pf_dir_path ), 0)) : (g_free) (pf_dir_path);
211 | }
212 |
213 | if (copy_persconffile_profile(env_prefix, profile_name, profile_name, true, &pf_filename,
214 | &pf_dir_path, &pf_dir_path2) == -1) {
215 | simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK0x01,
216 | "Can't copy file \"%s\" in directory\n\"%s\" to\n\"%s\":\n%s.",
217 | pf_filename, pf_dir_path2, pf_dir_path, g_strerror(errno(*__errno_location ())));
218 |
219 | g_free(pf_filename)(__builtin_object_size ((pf_filename), 0) != ((size_t) - 1)) ? g_free_sized (pf_filename, __builtin_object_size ((pf_filename ), 0)) : (g_free) (pf_filename);
220 | g_free(pf_dir_path)(__builtin_object_size ((pf_dir_path), 0) != ((size_t) - 1)) ? g_free_sized (pf_dir_path, __builtin_object_size ((pf_dir_path ), 0)) : (g_free) (pf_dir_path);
221 | g_free(pf_dir_path2)(__builtin_object_size ((pf_dir_path2), 0) != ((size_t) - 1)) ? g_free_sized (pf_dir_path2, __builtin_object_size ((pf_dir_path2 ), 0)) : (g_free) (pf_dir_path2);
222 | }
223 | } else {
224 | /* No personal and no global profile exists */
225 | return;
226 | }
227 | }
228 |
229 | /* Then check if changing to another profile */
230 | if (profile_name && strcmp (profile_name, get_profile_name()) == 0) {
231 | return;
232 | }
233 |
234 | /* Get the current geometry, before writing it to disk */
235 | emit profileChanging();
236 |
237 | if (write_recent_file && profile_exists(env_prefix, get_profile_name(), false))
238 | {
239 | /* Write recent file for profile we are leaving, if it still exists */
240 | write_profile_recent();
241 | }
242 |
243 | // Freeze the packet list early to avoid updating column data before doing a
244 | // full redissection. The packet list will be thawed when redissection is done.
245 | emit freezePacketList(true);
246 |
247 | /* Set profile name and update the status bar */
248 | set_profile_name (profile_name);
249 | emit profileNameChanged(profile_name);
250 |
251 | /* Apply new preferences */
252 | readConfigurationFiles(true);
253 |
254 | /* Apply command-line preferences */
255 | commandline_options_reapply();
256 | extcap_register_preferences(NULL__null, NULL__null);
257 |
258 | /* Switching profile requires reloading the macro list. */
259 | reloadDisplayFilterMacros();
260 |
261 | if (!recent_read_profile_static(&rf_path, &rf_open_errno)) {
262 | simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK0x01,
263 | "Could not open common recent file\n\"%s\": %s.",
264 | rf_path, g_strerror(rf_open_errno));
265 | g_free(rf_path)(__builtin_object_size ((rf_path), 0) != ((size_t) - 1)) ? g_free_sized (rf_path, __builtin_object_size ((rf_path), 0)) : (g_free) ( rf_path);
266 | }
267 | if (recent.gui_fileopen_remembered_dir &&
268 | test_for_directory(recent.gui_fileopen_remembered_dir) == EISDIR21) {
269 | set_last_open_dir(recent.gui_fileopen_remembered_dir);
270 | }
271 | timestamp_set_type(recent.gui_time_format);
272 | timestamp_set_precision(recent.gui_time_precision);
273 | timestamp_set_seconds_type (recent.gui_seconds_format);
274 |
275 | prefs_to_capture_opts(&global_capture_opts);
276 | prefs_apply_all();
277 | #ifdef HAVE_LIBPCAP1
278 | /* Re-apply interface display attributes from the new profile's prefs before
279 | the preferencesChanged() emit below, so its listeners see fresh data. The
280 | manager owns interface enumeration/attributes now. */
281 | if (MainWindow *mw = mainWindow())
282 | if (InterfaceListManager *mgr = mw->interfaceListManager())
283 | mgr->reapplyInterfacePreferences();
284 | #endif
285 |
286 | emit columnsChanged();
287 | emit preferencesChanged();
288 | emit recentPreferencesRead();
289 | emit filterExpressionsChanged();
290 | emit checkDisplayFilter();
291 | emit captureFilterListChanged();
292 | emit displayFilterListChanged();
293 |
294 | /* Reload color filters */
295 | if (!color_filters_reload(&err_msg, color_filter_add_cb, application_configuration_environment_prefix())) {
296 | simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK0x01, "%s", err_msg);
297 | g_free(err_msg)(__builtin_object_size ((err_msg), 0) != ((size_t) - 1)) ? g_free_sized (err_msg, __builtin_object_size ((err_msg), 0)) : (g_free) ( err_msg);
298 | }
299 |
300 | /* Capture-interface prefs are now watched by InterfaceListManager, which
301 | rescans when capture_no_interface_load / capture_no_extcap flips. A profile
302 | switch can also change interface display attributes, so notify subscribers
303 | (the manager owns the interface-list-changed signal now). */
304 | MainWindow *mw = mainWindow();
305 | if (mw && mw->interfaceListManager())
306 | mw->interfaceListManager()->notifyListChanged();
307 | emit packetDissectionChanged();
308 |
309 | /* Write recent_common file to ensure last used profile setting is stored. */
310 | write_recent();
311 | }
312 |
313 | void MainApplication::reloadLuaPluginsDelayed()
314 | {
315 | QTimer::singleShot(0, this, [this]() {
316 | /* Clear the reloading flag so the re-triggered reload
317 | * is not blocked by the isReloadingLua() guard. */
318 | setReloadingLua(false);
319 | emit reloadLuaPlugins();
320 | });
321 | }
322 |
323 | const QIcon &MainApplication::normalIcon()
324 | {
325 | if (normal_icon_.isNull()) {
326 | initializeIcons();
327 | }
328 | return normal_icon_;
329 | }
330 |
331 | const QIcon &MainApplication::captureIcon()
332 | {
333 | if (capture_icon_.isNull()) {
334 | initializeIcons();
335 | }
336 | return capture_icon_;
337 | }
338 |
339 | const QString MainApplication::windowTitleString(QStringList title_parts)
340 | {
341 | QMutableStringListIterator tii(title_parts);
342 | while (tii.hasNext()) {
343 | QString ti = tii.next();
344 | if (ti.isEmpty()) tii.remove();
345 | }
346 | title_parts.prepend(applicationName());
347 | return title_parts.join(window_title_separator_);
348 | }
349 |
350 | void MainApplication::applyCustomColorsFromRecent()
351 | {
352 | int i = 0;
353 | bool ok;
354 | for (GList *custom_color = recent.custom_colors; custom_color; custom_color = custom_color->next) {
355 | QRgb rgb = QString((const char *)custom_color->data).toUInt(&ok, 16);
356 | if (ok) {
357 | QColorDialog::setCustomColor(i++, QColor(rgb));
358 | }
359 | }
360 | }
361 |
362 | // Return the first top-level MainWindow.
363 | MainWindow *MainApplication::mainWindow()
364 | {
365 | foreach (QWidget *tlw, topLevelWidgets())for (auto _container_365 = QtPrivate::qMakeForeachContainer(topLevelWidgets ()); _container_365.i != _container_365.e; ++_container_365.i ) if (QWidget *tlw = *_container_365.i; false) {} else {
366 | MainWindow *tlmw = qobject_cast<MainWindow *>(tlw);
367 | if (tlmw && tlmw->isVisible()) {
368 | return tlmw;
369 | }
370 | }
371 | return nullptr;
372 | }
373 |
374 | void MainApplication::storeCustomColorsInRecent()
375 | {
376 | if (QColorDialog::customCount()) {
377 | prefs_clear_string_list(recent.custom_colors);
378 | recent.custom_colors = NULL__null;
379 | for (int i = 0; i < QColorDialog::customCount(); i++) {
380 | QRgb rgb = QColorDialog::customColor(i).rgb();
381 | recent.custom_colors = g_list_append(recent.custom_colors, ws_strdup_printf("%08x", rgb)wmem_strdup_printf(__null, "%08x", rgb));
382 | }
383 | }
384 | }
385 |
386 | bool MainApplication::event(QEvent *event)
387 | {
388 | QString display_filter = NULL__null;
389 | if (event->type() == QEvent::FileOpen) {
390 | QFileOpenEvent *foe = static_cast<QFileOpenEvent *>(event);
391 | if (foe && foe->file().length() > 0) {
392 | QString cf_path(foe->file());
393 | if (initialized_) {
394 | emit openCaptureFile(cf_path, display_filter, WTAP_TYPE_AUTO0);
395 | } else {
396 | pending_open_files_.append(cf_path);
397 | }
398 | }
399 | return true;
400 | }
401 | return QApplication::event(event);
402 | }
403 |
404 | void MainApplication::cleanup()
405 | {
406 | SoftwareUpdate::instance()->cleanup();
407 | storeCustomColorsInRecent();
408 | // Write the user's recent file(s) to disk.
409 | write_profile_recent();
410 | write_recent();
411 |
412 | // We might end up here via exit_application.
413 | QThreadPool::globalInstance()->waitForDone();
414 | }
415 |
416 | MainApplication::MainApplication(int &argc, char **argv) :
417 | QApplication(argc, argv),
418 | initialized_(false),
419 | is_reloading_lua_(false),
420 | if_notifier_(NULL__null),
421 | active_captures_(0)
422 | #if defined(Q_OS_MAC) || defined(Q_OS_WIN)
423 | , normal_icon_(windowIcon())
424 | #endif
425 | {
426 | mainApp = this;
427 |
428 | MimeDatabaseInitThread *mime_db_init_thread = new(MimeDatabaseInitThread);
429 | QThreadPool::globalInstance()->start(mime_db_init_thread);
430 |
431 | Q_INIT_RESOURCE(about)do { extern int qInitResources_about (); qInitResources_about (); } while (false);
1 | Loop condition is false. Exiting loop | →
432 | Q_INIT_RESOURCE(i18n)do { extern int qInitResources_i18n (); qInitResources_i18n ( ); } while (false);
2 | ← | Loop condition is false. Exiting loop | →
433 | Q_INIT_RESOURCE(layout)do { extern int qInitResources_layout (); qInitResources_layout (); } while (false);
3 | ← | Loop condition is false. Exiting loop | →
434 | Q_INIT_RESOURCE(stock_icons)do { extern int qInitResources_stock_icons (); qInitResources_stock_icons (); } while (false);
4 | ← | Loop condition is false. Exiting loop | →
435 | Q_INIT_RESOURCE(languages)do { extern int qInitResources_languages (); qInitResources_languages (); } while (false);
5 | ← | Loop condition is false. Exiting loop | →
436 |
437 | // Initialize the ThemeManager as early as possible so that any
438 | // widget constructed afterwards can resolve themed stylesheets and
439 | // color tokens. This must run after QApplication's base ctor (so
440 | // that the palette is queryable for light/dark detection) but
441 | // before any UI is built. recent_common has already been read in
442 | // main()/stratoshark_main() prior to constructing this application
443 | // object, so we can read any configured themes as well.
444 | // Theme selection is persisted in recent_common (recent.gui_theme_name),
445 | // not in the preferences file — so it survives profile switches and
446 | // stays global to the install. Empty / missing value, or the legacy
447 | // "default" sentinel, get resolved by ThemeManager itself to the
448 | // current flavor's preferred default (wireshark / stratoshark).
449 | ThemeManager::init(ThemeManager::resolveThemeName(
450 | QString::fromUtf8(recent.gui_theme_name)));
451 |
452 | #ifdef Q_OS_WIN
453 | /* RichEd20.DLL is needed for native file dialog filter entries. */
454 | ws_load_library("riched20.dll");
455 | #endif // Q_OS_WIN
456 |
457 | // We use a lot of style sheets that base their colors on the main
458 | // application palette, so this works better.
459 | setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, true);
460 |
461 | // Throw various settings at the wall with the hope that one of them will
462 | // enable context menu shortcuts QTBUG-69452, QTBUG-109590
463 | setAttribute(Qt::AA_DontShowShortcutsInContextMenus, false);
464 | styleHints()->setShowShortcutsInContextMenus(true);
465 |
466 | packet_data_timer_.setParent(this);
467 | connect(&packet_data_timer_, &QTimer::timeout, this, &MainApplication::refreshPacketData);
468 | packet_data_timer_.start(1000);
469 |
470 | tap_update_timer_.setParent(this);
471 | // tap_update_timer interval is set when preferences are set before init
472 | connect(this, &MainApplication::appInitialized, &tap_update_timer_, [&]() { tap_update_timer_.start(); });
473 | connect(&tap_update_timer_, &QTimer::timeout, this, &MainApplication::updateTaps);
474 |
475 | setStyle(new ThemeStyler);
6 | ← | Memory is allocated | →
476 |
477 | connect(qApp(static_cast<QApplication *>(QCoreApplication::instance ())), &QApplication::aboutToQuit, this, &MainApplication::cleanup);
7 | ← | Potential memory leak
478 | }
479 |
480 | MainApplication::~MainApplication()
481 | {
482 | mainApp = NULL__null;
483 | clearDynamicMenuGroupItems();
484 | }
485 |
486 | void MainApplication::emitAppSignal(AppSignal signal)
487 | {
488 | switch (signal) {
489 | case ColumnsChanged:
490 | emit columnsChanged();
491 | break;
492 | case CaptureFilterListChanged:
493 | emit captureFilterListChanged();
494 | break;
495 | case DisplayFilterListChanged:
496 | emit displayFilterListChanged();
497 | break;
498 | case FilterExpressionsChanged:
499 | emit filterExpressionsChanged();
500 | break;
501 | case NameResolutionChanged:
502 | emit addressResolutionChanged();
503 | break;
504 | case PreferencesChanged:
505 | tap_update_timer_.setInterval(prefs.tap_update_interval);
506 | emit preferencesChanged();
507 | break;
508 | case PacketDissectionChanged:
509 | emit packetDissectionChanged();
510 | break;
511 | case RecentPreferencesRead:
512 | emit recentPreferencesRead();
513 | break;
514 | case FieldsChanged:
515 | emit fieldsChanged();
516 | break;
517 | case FreezePacketList:
518 | emit freezePacketList(false);
519 | break;
520 | case AggregationChanged:
521 | emit aggregationChanged();
522 | break;
523 | default:
524 | break;
525 | }
526 | }
527 |
528 | // Flush any collected app signals.
529 | //
530 | // On macOS emitting PacketDissectionChanged from a dialog can
531 | // render the application unusable:
532 | // https://gitlab.com/wireshark/wireshark/-/issues/11361
533 | // https://gitlab.com/wireshark/wireshark/-/issues/11448
534 | // Work around the problem by queueing up app signals and emitting them
535 | // after the dialog is closed.
536 | //
537 | // The following bugs might be related although they don't describe the
538 | // exact behavior we're working around here:
539 | // https://bugreports.qt.io/browse/QTBUG-38512
540 | // https://bugreports.qt.io/browse/QTBUG-38600
541 | void MainApplication::flushAppSignals()
542 | {
543 | while (!app_signals_.isEmpty()) {
544 | mainApp->emitAppSignal(app_signals_.takeFirst());
545 | }
546 | }
547 |
548 | void MainApplication::emitStatCommandSignal(const QString &menu_path, const char *arg, void *userdata)
549 | {
550 | emit openStatCommandDialog(menu_path, arg, userdata);
551 | }
552 |
553 | void MainApplication::emitTapParameterSignal(const QString cfg_abbr, const QString arg, void *userdata)
554 | {
555 | emit openTapParameterDialog(cfg_abbr, arg, userdata);
556 | }
557 |
558 | // XXX Combine statistics and funnel routines into addGroupItem + groupItems?
559 | void MainApplication::addDynamicMenuGroupItem(int group, QAction *sg_action)
560 | {
561 | if (!dynamic_menu_groups_.contains(group)) {
562 | dynamic_menu_groups_[group] = QList<QAction *>();
563 | }
564 | dynamic_menu_groups_[group] << sg_action;
565 | }
566 |
567 | void MainApplication::appendDynamicMenuGroupItem(int group, QAction *sg_action)
568 | {
569 | if (!added_menu_groups_.contains(group)) {
570 | added_menu_groups_[group] = QList<QAction *>();
571 | }
572 | added_menu_groups_[group] << sg_action;
573 | addDynamicMenuGroupItem(group, sg_action);
574 | }
575 |
576 | void MainApplication::removeDynamicMenuGroupItem(int group, QAction *sg_action)
577 | {
578 | if (!removed_menu_groups_.contains(group)) {
579 | removed_menu_groups_[group] = QList<QAction *>();
580 | }
581 | removed_menu_groups_[group] << sg_action;
582 | dynamic_menu_groups_[group].removeAll(sg_action);
583 | }
584 |
585 | void MainApplication::clearDynamicMenuGroupItems()
586 | {
587 | foreach (int group, dynamic_menu_groups_.keys())for (auto _container_587 = QtPrivate::qMakeForeachContainer(dynamic_menu_groups_ .keys()); _container_587.i != _container_587.e; ++_container_587 .i) if (int group = *_container_587.i; false) {} else {
588 | dynamic_menu_groups_[group].clear();
589 | }
590 | }
591 |
592 | QList<QAction *> MainApplication::dynamicMenuGroupItems(int group)
593 | {
594 | if (!dynamic_menu_groups_.contains(group)) {
595 | return QList<QAction *>();
596 | }
597 |
598 | QList<QAction *> sgi_list = dynamic_menu_groups_[group];
599 | std::sort(sgi_list.begin(), sgi_list.end(), qActionLessThan);
600 | return sgi_list;
601 | }
602 |
603 | QList<QAction *> MainApplication::addedMenuGroupItems(int group)
604 | {
605 | if (!added_menu_groups_.contains(group)) {
606 | return QList<QAction *>();
607 | }
608 |
609 | QList<QAction *> sgi_list = added_menu_groups_[group];
610 | std::sort(sgi_list.begin(), sgi_list.end(), qActionLessThan);
611 | return sgi_list;
612 | }
613 |
614 | QList<QAction *> MainApplication::removedMenuGroupItems(int group)
615 | {
616 | if (!removed_menu_groups_.contains(group)) {
617 | return QList<QAction *>();
618 | }
619 |
620 | QList<QAction *> sgi_list = removed_menu_groups_[group];
621 | std::sort(sgi_list.begin(), sgi_list.end(), qActionLessThan);
622 | return sgi_list;
623 | }
624 |
625 | void MainApplication::clearAddedMenuGroupItems()
626 | {
627 | foreach (int group, added_menu_groups_.keys())for (auto _container_627 = QtPrivate::qMakeForeachContainer(added_menu_groups_ .keys()); _container_627.i != _container_627.e; ++_container_627 .i) if (int group = *_container_627.i; false) {} else {
628 | added_menu_groups_[group].clear();
629 | }
630 | }
631 |
632 | void MainApplication::clearRemovedMenuGroupItems()
633 | {
634 | foreach (int group, removed_menu_groups_.keys())for (auto _container_634 = QtPrivate::qMakeForeachContainer(removed_menu_groups_ .keys()); _container_634.i != _container_634.e; ++_container_634 .i) if (int group = *_container_634.i; false) {} else {
635 | foreach (QAction *action, removed_menu_groups_[group])for (auto _container_635 = QtPrivate::qMakeForeachContainer(removed_menu_groups_ [group]); _container_635.i != _container_635.e; ++_container_635 .i) if (QAction *action = *_container_635.i; false) {} else {
636 | delete action;
637 | }
638 | removed_menu_groups_[group].clear();
639 | }
640 | }
641 |
642 | #ifdef HAVE_LIBPCAP1
643 |
644 | static void
645 | iface_mon_event_cb(const char *iface, int added, int up)
646 | {
647 | int present = 0;
648 | unsigned ifs, j;
649 | interface_t *device;
650 | interface_options *interface_opts;
651 |
652 | for (ifs = 0; ifs < global_capture_opts.all_ifaces->len; ifs++) {
653 | device = &g_array_index(global_capture_opts.all_ifaces, interface_t, ifs)(((interface_t*) (void *) (global_capture_opts.all_ifaces)-> data) [(ifs)]);
654 | if (strcmp(device->name, iface) == 0) {
655 | present = 1;
656 | if (!up) {
657 | /*
658 | * Interface went down or disappeared; remove all instances
659 | * of it from the current list of interfaces selected
660 | * for capturing.
661 | */
662 | for (j = 0; j < global_capture_opts.ifaces->len; j++) {
663 | interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, j)(((interface_options*) (void *) (global_capture_opts.ifaces)-> data) [(j)]);
664 | if (strcmp(interface_opts->name, device->name) == 0) {
665 | capture_opts_del_iface(&global_capture_opts, j);
666 | }
667 | }
668 | }
669 | }
670 | }
671 |
672 | mainApp->emitLocalInterfaceEvent(iface, added, up);
673 | if (present != up) {
674 | /*
675 | * We've been told that there's a new interface or that an old
676 | * interface is gone; reload the local interface list.
677 | *
678 | * XXX: We also want to reload the local interface list if [what
679 | * we can retrieve about] the capabilities of the device have changed.
680 | * Ideally we'd update the capabilities of just the one device in
681 | * the cache and signal that the list has been updated, instead of
682 | * freeing the entire cache and scanning again - but some extcaps
683 | * depend on other interfaces being up; e.g. by default androiddump
684 | * tries to connect to the loopback interface to look for adb running,
685 | * so if the loopback interface changes so does the status of
686 | * androiddump.
687 | *
688 | * On Linux, at least, you can't get the capabilities from a down
689 | * interface, but it's still present in all_ifaces - dumpcap returns
690 | * it in the list, and we show it so the user can get a status / error
691 | * message when trying to capture on it instead of it vanishing.
692 | * So if both present and up are true, then we still want to refresh
693 | * to update the capabilities and restart the stats.
694 | *
695 | * We also store the address in all_ifaces and show them to the user,
696 | * so we probably should monitor those events as well and update
697 | * the interface list appropriately when those change.
698 | */
699 | MainWindow *mainWindow = mainApp->mainWindow();
700 | if (mainWindow && mainWindow->interfaceListManager())
701 | mainWindow->interfaceListManager()->requestRefresh();
702 | }
703 | }
704 |
705 | #endif
706 |
707 | void MainApplication::ifChangeEventsAvailable()
708 | {
709 | #ifdef HAVE_LIBPCAP1
710 | /*
711 | * Something's readable from the descriptor for interface
712 | * monitoring.
713 | *
714 | * Have the interface-monitoring code Read whatever interface-change
715 | * events are available, and call the callback for them.
716 | */
717 | iface_mon_event();
718 | #endif
719 | }
720 |
721 | void MainApplication::emitLocalInterfaceEvent(const char *ifname, int added, int up)
722 | {
723 | emit localInterfaceEvent(ifname, added, up);
724 | }
725 |
726 | void MainApplication::whenInitializedDispatch(const QObject *context, std::function<void()> fn)
727 | {
728 | // POLICY DECISION (your input shapes behavior here):
729 | //
730 | // We get here only when a caller registers via whenInitialized() *after*
731 | // appInitialized() has already fired. The not-yet-initialized path runs the
732 | // callback later, from the event loop, once construction is long finished.
733 | // How should the already-initialized path behave?
734 | //
735 | // A) Synchronous: fn(); right now. Simplest. But the callback runs while
736 | // the caller's constructor is still on the stack -- the object may be
737 | // only partly built, and any code after the whenInitialized() call in
738 | // that constructor runs *after* the callback. Timing differs from the
739 | // deferred path.
740 | //
741 | // B) Deferred to the event loop (e.g. QTimer::singleShot(0, context, fn)
742 | // or QMetaObject::invokeMethod(..., Qt::QueuedConnection)): callback
743 | // always runs after the current call returns, matching the signal path
744 | // exactly, so callers see one consistent ordering. Costs one event-loop
745 | // turn and needs the context-still-alive guarantee.
746 | //
747 | // Option A (current behavior) is intentional for now: it exactly reproduces
748 | // the old open-coded `if (isInitialized()) doThing();` -- a synchronous call
749 | // on the caller's stack -- so routing those sites through whenInitialized()
750 | // is a pure no-op refactor. Option B is the intended end state, but it
751 | // shifts the already-initialized callback by one event-loop turn and so
752 | // changes ordering for every converted site at once; switch to it only
753 | // behind broader ordering/lifetime tests (dialogs opened mid-session,
754 | // welcome/interface frames at cold start, context destroyed same-turn).
755 | //
756 | // To switch, delete the fn() below and enable the deferred dispatch:
757 | //
758 | // QTimer::singleShot(0, const_cast<QObject *>(context), std::move(fn));
759 | //
760 | // (context is the receiver, so the call is dropped if it dies first, and
761 | // runs on context's thread -- matching the connect() branch.)
762 | Q_UNUSED(context)(void)context; // option A ignores context; option B uses it.
763 | fn(); // option A: synchronous, matches legacy behavior.
764 | }
765 |
766 | void MainApplication::allSystemsGo()
767 | {
768 | QString display_filter = NULL__null;
769 | initialized_ = true;
770 | emit appInitialized();
771 | while (pending_open_files_.length() > 0) {
772 | emit openCaptureFile(pending_open_files_.front(), display_filter, WTAP_TYPE_AUTO0);
773 | pending_open_files_.pop_front();
774 | }
775 |
776 | bool sideBarVisible = recent.gui_welcome_page_sidebar_tips_visible ||
777 | recent.gui_welcome_page_sidebar_learn_visible;
778 | SoftwareUpdate::instance()->init(!sideBarVisible);
779 |
780 | #ifdef HAVE_LIBPCAP1
781 | int err;
782 | err = iface_mon_start(&iface_mon_event_cb);
783 | if (err == 0) {
784 | if_notifier_ = new QSocketNotifier(iface_mon_get_sock(),
785 | QSocketNotifier::Read, this);
786 | connect(if_notifier_, &QSocketNotifier::activated, this, &MainApplication::ifChangeEventsAvailable);
787 | }
788 | #endif
789 | }
790 |
791 | _e_prefs *MainApplication::readConfigurationFiles(bool reset)
792 | {
793 | e_prefs *prefs_p;
794 |
795 | if (reset) {
796 | //
797 | // Reset current preferences and enabled/disabled protocols and
798 | // heuristic dissectors before reading.
799 | // (Needed except when this is called at startup.)
800 | //
801 | prefs_reset(application_configuration_environment_prefix(), application_columns(), application_num_columns());
802 | proto_reenable_all();
803 | }
804 |
805 | /* Load libwireshark settings from the current profile. */
806 | prefs_p = epan_load_settings();
807 |
808 | return prefs_p;
809 | }
810 |
811 | static void switchTranslator(QTranslator& myTranslator, const QLocale &locale, const QString& filename, const QStringList &searchPath)
812 | {
813 | mainApp->removeTranslator(&myTranslator);
814 | for (const QString &path : searchPath) {
815 | if (myTranslator.load(locale, filename, QStringLiteral("_")(QString(QtPrivate::qMakeStringPrivate(u"" "_"))), path)) {
816 | mainApp->installTranslator(&myTranslator);
817 | return;
818 | }
819 | }
820 | if (locale.language() != QLocale::C) {
821 | /* Don't compare the locale itself, see:
822 | * https://doc.qt.io/qt-6/qlocale.html#operator-eq-eq
823 | *
824 | * Note that the ordered list of languages that were tried is that of
825 | * locale.uiLanguages(); the first language in that list is not
826 | * necessarily locale.language(), especially on Windows (See #17221.)
827 | */
828 | qWarningQMessageLogger(static_cast<const char *>("ui/qt/main_application.cpp" ), 828, static_cast<const char *>(__PRETTY_FUNCTION__)) .warning() << "Couldn't load" << filename << "translations!" << "Searched:" << searchPath;
829 | }
830 | }
831 |
832 | void MainApplication::loadLanguage(const QString newLanguage)
833 | {
834 | QLocale locale;
835 | const char* env_prefix = application_configuration_environment_prefix();
836 |
837 | if (newLanguage.isEmpty() || newLanguage == USE_SYSTEM_LANGUAGE"system") {
838 | locale = QLocale::system();
839 | } else {
840 | locale = QLocale(newLanguage);
841 | }
842 |
843 | QLocale::setDefault(locale);
844 |
845 | // Search path list ordered by priority. Prefer personal configuration
846 | // to global datadir to embedded resources to Qt global directory.
847 | QStringList searchPath;
848 | searchPath.emplaceBack(gchar_free_to_qstring(get_persconffile_path("languages", false, env_prefix)));
849 | searchPath.emplaceBack(QStringLiteral("%1/languages")(QString(QtPrivate::qMakeStringPrivate(u"" "%1/languages"))).arg(get_datafile_dir(env_prefix)));
850 | searchPath.emplaceBack(QStringLiteral(":/i18n/")(QString(QtPrivate::qMakeStringPrivate(u"" ":/i18n/"))));
851 |
852 | #if QT_VERSION((6<<16)|(10<<8)|(2)) >= QT_VERSION_CHECK(6, 8, 0)((6<<16)|(8<<8)|(0))
853 | searchPath.append(QLibraryInfo::paths(QLibraryInfo::TranslationsPath));
854 | #else
855 | searchPath.emplaceBack(QLibraryInfo::path(QLibraryInfo::TranslationsPath));
856 | #endif
857 |
858 | // Translations are searched for in the reverse order in which they were
859 | // installed, so install the Qt generic translator first and ours last.
860 | switchTranslator(mainApp->translatorQt, locale, QStringLiteral("qt")(QString(QtPrivate::qMakeStringPrivate(u"" "qt"))), searchPath);
861 |
862 | // XXX - Yes, the translation files are also wireshark_%1.qm for Stratoshark.
863 | // There is a stratoshark_en.[ts|qm] file too (for plurals?) though I'm
864 | // not sure if it's used properly.
865 | switchTranslator(mainApp->translator, locale, QStringLiteral("wireshark")(QString(QtPrivate::qMakeStringPrivate(u"" "wireshark"))), searchPath);
866 | }
867 |
868 | void MainApplication::doTriggerMenuItem(MainMenuItem menuItem)
869 | {
870 | switch (menuItem)
871 | {
872 | case FileOpenDialog:
873 | emit openCaptureFile(QString(), QString(), WTAP_TYPE_AUTO0);
874 | break;
875 | case CaptureOptionsDialog:
876 | emit openCaptureOptions();
877 | break;
878 | }
879 | }
880 |
881 | void MainApplication::captureEventHandler(CaptureEvent ev)
882 | {
883 | switch(ev.captureContext())
884 | {
885 | #ifdef HAVE_LIBPCAP1
886 | case CaptureEvent::Update:
887 | case CaptureEvent::Fixed:
888 | switch (ev.eventType())
889 | {
890 | case CaptureEvent::Prepared:
891 | iface_mon_enable(true);
892 | break;
893 | case CaptureEvent::Started:
894 | active_captures_++;
895 | emit captureActive(active_captures_);
896 | break;
897 | case CaptureEvent::Finished:
898 | active_captures_--;
899 | emit captureActive(active_captures_);
900 | // A refresh requested during the capture was deferred by
901 | // InterfaceListManager (capture-active guard) and is serviced now via
902 | // the captureActive signal above; no explicit re-trigger needed.
903 | break;
904 | default:
905 | break;
906 | }
907 | break;
908 | #endif
909 | case CaptureEvent::File:
910 | case CaptureEvent::Reload:
911 | case CaptureEvent::Rescan:
912 | switch (ev.eventType())
913 | {
914 | case CaptureEvent::Started:
915 | QTimer::singleShot(TAP_UPDATE_DEFAULT_INTERVAL3000 / 5, this, SLOT(updateTaps())qFlagLocation("1" "updateTaps()" "\0" "ui/qt/main_application.cpp" ":" "915"));
916 | QTimer::singleShot(TAP_UPDATE_DEFAULT_INTERVAL3000 / 2, this, SLOT(updateTaps())qFlagLocation("1" "updateTaps()" "\0" "ui/qt/main_application.cpp" ":" "916"));
917 | break;
918 | case CaptureEvent::Finished:
919 | updateTaps();
920 | break;
921 | default:
922 | break;
923 | }
924 | break;
925 | default:
926 | break;
927 | }
928 | }
929 |
930 | void MainApplication::pushStatus(StatusInfo status, const QString &message, const QString &messagetip)
931 | {
932 | MainWindow * mw = mainWindow();
933 | if (! mw) {
934 | return;
935 | }
936 |
937 | MainStatusBar * bar = mw->statusBar();
938 | if (! bar) {
939 | return;
940 | }
941 |
942 | switch(status)
943 | {
944 | case FilterSyntax:
945 | bar->pushGenericStatus(MainStatusBar::STATUS_CTX_FILTER, message);
946 | break;
947 | case FieldStatus:
948 | bar->pushGenericStatus(MainStatusBar::STATUS_CTX_FIELD, message);
949 | break;
950 | case FileStatus:
951 | bar->pushGenericStatus(MainStatusBar::STATUS_CTX_FILE, message, messagetip);
952 | break;
953 | case ByteStatus:
954 | bar->pushGenericStatus(MainStatusBar::STATUS_CTX_BYTE, message);
955 | break;
956 | case BusyStatus:
957 | bar->pushGenericStatus(MainStatusBar::STATUS_CTX_PROGRESS, message, messagetip);
958 | break;
959 | case TemporaryStatus:
960 | bar->pushGenericStatus(MainStatusBar::STATUS_CTX_TEMPORARY, message);
961 | break;
962 | }
963 | }
964 |
965 | void MainApplication::popStatus(StatusInfo status)
966 | {
967 | MainWindow * mw = mainWindow();
968 | if (! mw) {
969 | return;
970 | }
971 |
972 | MainStatusBar * bar = mw->statusBar();
973 | if (! bar) {
974 | return;
975 | }
976 |
977 | switch(status)
978 | {
979 | case FilterSyntax:
980 | bar->popGenericStatus(MainStatusBar::STATUS_CTX_FILTER);
981 | break;
982 | case FieldStatus:
983 | bar->popGenericStatus(MainStatusBar::STATUS_CTX_FIELD);
984 | break;
985 | case FileStatus:
986 | bar->popGenericStatus(MainStatusBar::STATUS_CTX_FILE);
987 | break;
988 | case ByteStatus:
989 | bar->popGenericStatus(MainStatusBar::STATUS_CTX_BYTE);
990 | break;
991 | case BusyStatus:
992 | bar->popGenericStatus(MainStatusBar::STATUS_CTX_PROGRESS);
993 | break;
994 | case TemporaryStatus:
995 | bar->popGenericStatus(MainStatusBar::STATUS_CTX_TEMPORARY);
996 | break;
997 | }
998 | }
999 |
1000 | void MainApplication::gotoFrame(int frame)
1001 | {
1002 | MainWindow * mw = mainWindow();
1003 | if (! mw) {
1004 | return;
1005 | }
1006 |
1007 | mw->gotoFrame(frame);
1008 | }
1009 |
1010 | void MainApplication::reloadDisplayFilterMacros()
1011 | {
1012 | dfilter_macro_reload(application_configuration_environment_prefix());
1013 | // The signal is needed when the display filter grammar changes for
1014 | // any reason (not just "fields".)
1015 | mainApp->emitAppSignal(MainApplication::FieldsChanged);
1016 | }