Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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/QtCore5Compat -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_CORE5COMPAT_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-06-04-100425-3529-1 -x c++ /builds/wireshark/wireshark/ui/qt/main_application.cpp
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/iface_lists.h"
41#include "ui/language.h"
42#include "ui/recent.h"
43#include "ui/simple_dialog.h"
44#include "ui/util.h"
45
46#include <ui/qt/utils/qt_ui_utils.h>
47#include <ui/qt/utils/color_utils.h>
48#include <ui/qt/utils/software_update.h>
49#include <ui/qt/utils/theme_manager.h>
50#include "coloring_rules_dialog.h"
51
52#include "epan/color_filters.h"
53
54#include "extcap.h"
55#ifdef HAVE_LIBPCAP1
56#include <capture/iface_monitor.h>
57#endif
58
59#include "wsutil/filter_files.h"
60#include "ui/capture_globals.h"
61#include "ui/file_dialog.h"
62#include "ui/recent_utils.h"
63
64#ifdef HAVE_LIBPCAP1
65#include "ui/capture.h"
66#endif
67
68#include "wsutil/utf8_entities.h"
69
70#ifdef _WIN32
71# include "wsutil/file_util.h"
72# include <QMessageBox>
73# include <QSettings>
74#endif /* _WIN32 */
75
76#include <ui/qt/capture_file.h>
77
78#include <ui/qt/main_window.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
114MainApplication *mainApp;
115
116// XXX - Copied from ui/gtk/file_dlg.c
117
118static QHash<int, QList<QAction *> > dynamic_menu_groups_;
119static QHash<int, QList<QAction *> > added_menu_groups_;
120static QHash<int, QList<QAction *> > removed_menu_groups_;
121
122QString 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
127class MimeDatabaseInitThread : public QRunnable
128{
129private:
130 void run()
131 {
132 QMimeDatabase mime_db;
133 mime_db.mimeTypeForData(QByteArray());
134 }
135};
136
137void
138topic_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 */
144extern "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
158void MainApplication::refreshPacketData()
159{
160 if (host_name_lookup_process()) {
161 emit addressResolutionChanged();
162 } else if (col_data_changed()) {
163 emit columnDataChanged();
164 }
165}
166
167void MainApplication::updateTaps()
168{
169 draw_tap_listeners(false);
170}
171
172QDir MainApplication::openDialogInitialDir() {
173 return QDir(get_open_dialog_initial_dir());
174}
175
176void 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
184void 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
193void 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 bool prev_capture_no_interface_load;
201 bool prev_capture_no_extcap;
202
203 /* First check if profile exists */
204 if (!profile_exists(env_prefix, profile_name, false)) {
205 if (profile_exists(env_prefix, profile_name, true)) {
206 char *pf_dir_path, *pf_dir_path2, *pf_filename;
207 /* Copy from global profile */
208 if (create_persconffile_profile(env_prefix, profile_name, &pf_dir_path) == -1) {
209 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK0x01,
210 "Can't create directory\n\"%s\":\n%s.",
211 pf_dir_path, g_strerror(errno(*__errno_location ())));
212
213 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)
;
214 }
215
216 if (copy_persconffile_profile(env_prefix, profile_name, profile_name, true, &pf_filename,
217 &pf_dir_path, &pf_dir_path2) == -1) {
218 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK0x01,
219 "Can't copy file \"%s\" in directory\n\"%s\" to\n\"%s\":\n%s.",
220 pf_filename, pf_dir_path2, pf_dir_path, g_strerror(errno(*__errno_location ())));
221
222 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)
;
223 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)
;
224 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)
;
225 }
226 } else {
227 /* No personal and no global profile exists */
228 return;
229 }
230 }
231
232 /* Then check if changing to another profile */
233 if (profile_name && strcmp (profile_name, get_profile_name()) == 0) {
234 return;
235 }
236
237 prev_capture_no_interface_load = prefs.capture_no_interface_load;
238 prev_capture_no_extcap = prefs.capture_no_extcap;
239
240 /* Get the current geometry, before writing it to disk */
241 emit profileChanging();
242
243 if (write_recent_file && profile_exists(env_prefix, get_profile_name(), false))
244 {
245 /* Write recent file for profile we are leaving, if it still exists */
246 write_profile_recent();
247 }
248
249 // Freeze the packet list early to avoid updating column data before doing a
250 // full redissection. The packet list will be thawed when redissection is done.
251 emit freezePacketList(true);
252
253 /* Set profile name and update the status bar */
254 set_profile_name (profile_name);
255 emit profileNameChanged(profile_name);
256
257 /* Apply new preferences */
258 readConfigurationFiles(true);
259
260 /* Apply command-line preferences */
261 commandline_options_reapply();
262 extcap_register_preferences(NULL__null, NULL__null);
263
264 /* Switching profile requires reloading the macro list. */
265 reloadDisplayFilterMacros();
266
267 if (!recent_read_profile_static(&rf_path, &rf_open_errno)) {
268 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK0x01,
269 "Could not open common recent file\n\"%s\": %s.",
270 rf_path, g_strerror(rf_open_errno));
271 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)
;
272 }
273 if (recent.gui_fileopen_remembered_dir &&
274 test_for_directory(recent.gui_fileopen_remembered_dir) == EISDIR21) {
275 set_last_open_dir(recent.gui_fileopen_remembered_dir);
276 }
277 timestamp_set_type(recent.gui_time_format);
278 timestamp_set_precision(recent.gui_time_precision);
279 timestamp_set_seconds_type (recent.gui_seconds_format);
280
281 prefs_to_capture_opts(&global_capture_opts);
282 prefs_apply_all();
283#ifdef HAVE_LIBPCAP1
284 update_local_interfaces(&global_capture_opts);
285#endif
286
287 emit columnsChanged();
288 emit preferencesChanged();
289 emit recentPreferencesRead();
290 emit filterExpressionsChanged();
291 emit checkDisplayFilter();
292 emit captureFilterListChanged();
293 emit displayFilterListChanged();
294
295 /* Reload color filters */
296 if (!color_filters_reload(&err_msg, color_filter_add_cb, application_configuration_environment_prefix())) {
297 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK0x01, "%s", err_msg);
298 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)
;
299 }
300
301 /* Load interfaces if settings have changed */
302 if (!prefs.capture_no_interface_load &&
303 ((prefs.capture_no_interface_load != prev_capture_no_interface_load) ||
304 (prefs.capture_no_extcap != prev_capture_no_extcap))) {
305 refreshLocalInterfaces();
306 }
307
308 emit localInterfaceListChanged();
309 emit packetDissectionChanged();
310
311 /* Write recent_common file to ensure last used profile setting is stored. */
312 write_recent();
313}
314
315void MainApplication::reloadLuaPluginsDelayed()
316{
317 QTimer::singleShot(0, this, [this]() {
318 /* Clear the reloading flag so the re-triggered reload
319 * is not blocked by the isReloadingLua() guard. */
320 setReloadingLua(false);
321 emit reloadLuaPlugins();
322 });
323}
324
325const QIcon &MainApplication::normalIcon()
326{
327 if (normal_icon_.isNull()) {
328 initializeIcons();
329 }
330 return normal_icon_;
331}
332
333const QIcon &MainApplication::captureIcon()
334{
335 if (capture_icon_.isNull()) {
336 initializeIcons();
337 }
338 return capture_icon_;
339}
340
341const QString MainApplication::windowTitleString(QStringList title_parts)
342{
343 QMutableStringListIterator tii(title_parts);
344 while (tii.hasNext()) {
345 QString ti = tii.next();
346 if (ti.isEmpty()) tii.remove();
347 }
348 title_parts.prepend(applicationName());
349 return title_parts.join(window_title_separator_);
350}
351
352void MainApplication::applyCustomColorsFromRecent()
353{
354 int i = 0;
355 bool ok;
356 for (GList *custom_color = recent.custom_colors; custom_color; custom_color = custom_color->next) {
357 QRgb rgb = QString((const char *)custom_color->data).toUInt(&ok, 16);
358 if (ok) {
359 QColorDialog::setCustomColor(i++, QColor(rgb));
360 }
361 }
362}
363
364// Return the first top-level MainWindow.
365MainWindow *MainApplication::mainWindow()
366{
367 foreach (QWidget *tlw, topLevelWidgets())for (auto _container_367 = QtPrivate::qMakeForeachContainer(topLevelWidgets
()); _container_367.i != _container_367.e; ++_container_367.i
) if (QWidget *tlw = *_container_367.i; false) {} else
{
368 MainWindow *tlmw = qobject_cast<MainWindow *>(tlw);
369 if (tlmw && tlmw->isVisible()) {
370 return tlmw;
371 }
372 }
373 return nullptr;
374}
375
376void MainApplication::storeCustomColorsInRecent()
377{
378 if (QColorDialog::customCount()) {
379 prefs_clear_string_list(recent.custom_colors);
380 recent.custom_colors = NULL__null;
381 for (int i = 0; i < QColorDialog::customCount(); i++) {
382 QRgb rgb = QColorDialog::customColor(i).rgb();
383 recent.custom_colors = g_list_append(recent.custom_colors, ws_strdup_printf("%08x", rgb)wmem_strdup_printf(__null, "%08x", rgb));
384 }
385 }
386}
387
388bool MainApplication::event(QEvent *event)
389{
390 QString display_filter = NULL__null;
391 if (event->type() == QEvent::FileOpen) {
392 QFileOpenEvent *foe = static_cast<QFileOpenEvent *>(event);
393 if (foe && foe->file().length() > 0) {
394 QString cf_path(foe->file());
395 if (initialized_) {
396 emit openCaptureFile(cf_path, display_filter, WTAP_TYPE_AUTO0);
397 } else {
398 pending_open_files_.append(cf_path);
399 }
400 }
401 return true;
402 }
403 return QApplication::event(event);
404}
405
406void MainApplication::cleanup()
407{
408 SoftwareUpdate::instance()->cleanup();
409 storeCustomColorsInRecent();
410 // Write the user's recent file(s) to disk.
411 write_profile_recent();
412 write_recent();
413
414 // We might end up here via exit_application.
415 QThreadPool::globalInstance()->waitForDone();
416}
417
418MainApplication::MainApplication(int &argc, char **argv) :
419 QApplication(argc, argv),
420 initialized_(false),
421 is_reloading_lua_(false),
422 if_notifier_(NULL__null),
423 active_captures_(0),
424 refresh_interfaces_pending_(false)
425#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
426 , normal_icon_(windowIcon())
427#endif
428#ifdef HAVE_LIBPCAP1
429 , cached_if_list_(NULL__null)
430#endif
431{
432 mainApp = this;
433
434 MimeDatabaseInitThread *mime_db_init_thread = new(MimeDatabaseInitThread);
435 QThreadPool::globalInstance()->start(mime_db_init_thread);
436
437 Q_INIT_RESOURCE(about)do { extern int qInitResources_about (); qInitResources_about
(); } while (false)
;
1
Loop condition is false. Exiting loop
438 Q_INIT_RESOURCE(i18n)do { extern int qInitResources_i18n (); qInitResources_i18n (
); } while (false)
;
2
Loop condition is false. Exiting loop
439 Q_INIT_RESOURCE(layout)do { extern int qInitResources_layout (); qInitResources_layout
(); } while (false)
;
3
Loop condition is false. Exiting loop
440 Q_INIT_RESOURCE(stock_icons)do { extern int qInitResources_stock_icons (); qInitResources_stock_icons
(); } while (false)
;
4
Loop condition is false. Exiting loop
441 Q_INIT_RESOURCE(languages)do { extern int qInitResources_languages (); qInitResources_languages
(); } while (false)
;
5
Loop condition is false. Exiting loop
442
443 // Initialize the ThemeManager as early as possible so that any
444 // widget constructed afterwards can resolve themed stylesheets and
445 // color tokens. This must run after QApplication's base ctor (so
446 // that the palette is queryable for light/dark detection) but
447 // before any UI is built. recent_common has already been read in
448 // main()/stratoshark_main() prior to constructing this application
449 // object, so we can read any configured themes as well.
450 // Theme selection is persisted in recent_common (recent.gui_theme_name),
451 // not in the preferences file — so it survives profile switches and
452 // stays global to the install. Empty / missing value, or the legacy
453 // "default" sentinel, get resolved by ThemeManager itself to the
454 // current flavor's preferred default (wireshark / stratoshark).
455 ThemeManager::init(ThemeManager::resolveThemeName(
456 QString::fromUtf8(recent.gui_theme_name)));
457
458#ifdef Q_OS_WIN
459 /* RichEd20.DLL is needed for native file dialog filter entries. */
460 ws_load_library("riched20.dll");
461#endif // Q_OS_WIN
462
463 // We use a lot of style sheets that base their colors on the main
464 // application palette, so this works better.
465 setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, true);
466
467 // Throw various settings at the wall with the hope that one of them will
468 // enable context menu shortcuts QTBUG-69452, QTBUG-109590
469 setAttribute(Qt::AA_DontShowShortcutsInContextMenus, false);
470 styleHints()->setShowShortcutsInContextMenus(true);
471
472 packet_data_timer_.setParent(this);
473 connect(&packet_data_timer_, &QTimer::timeout, this, &MainApplication::refreshPacketData);
474 packet_data_timer_.start(1000);
475
476 tap_update_timer_.setParent(this);
477 // tap_update_timer interval is set when preferences are set before init
478 connect(this, &MainApplication::appInitialized, &tap_update_timer_, [&]() { tap_update_timer_.start(); });
479 connect(&tap_update_timer_, &QTimer::timeout, this, &MainApplication::updateTaps);
480
481 setStyle(new ThemeStyler);
6
Memory is allocated
482
483 connect(qApp(static_cast<QApplication *>(QCoreApplication::instance
()))
, &QApplication::aboutToQuit, this, &MainApplication::cleanup);
7
Potential memory leak
484}
485
486MainApplication::~MainApplication()
487{
488 mainApp = NULL__null;
489#ifdef HAVE_LIBPCAP1
490 free_interface_list(cached_if_list_);
491#endif
492 clearDynamicMenuGroupItems();
493}
494
495void MainApplication::emitAppSignal(AppSignal signal)
496{
497 switch (signal) {
498 case ColumnsChanged:
499 emit columnsChanged();
500 break;
501 case CaptureFilterListChanged:
502 emit captureFilterListChanged();
503 break;
504 case DisplayFilterListChanged:
505 emit displayFilterListChanged();
506 break;
507 case FilterExpressionsChanged:
508 emit filterExpressionsChanged();
509 break;
510 case LocalInterfacesChanged:
511 emit localInterfaceListChanged();
512 break;
513 case NameResolutionChanged:
514 emit addressResolutionChanged();
515 break;
516 case PreferencesChanged:
517 tap_update_timer_.setInterval(prefs.tap_update_interval);
518 emit preferencesChanged();
519 break;
520 case PacketDissectionChanged:
521 emit packetDissectionChanged();
522 break;
523 case RecentPreferencesRead:
524 emit recentPreferencesRead();
525 break;
526 case FieldsChanged:
527 emit fieldsChanged();
528 break;
529 case FreezePacketList:
530 emit freezePacketList(false);
531 break;
532 case AggregationChanged:
533 emit aggregationChanged();
534 break;
535 default:
536 break;
537 }
538}
539
540// Flush any collected app signals.
541//
542// On macOS emitting PacketDissectionChanged from a dialog can
543// render the application unusable:
544// https://gitlab.com/wireshark/wireshark/-/issues/11361
545// https://gitlab.com/wireshark/wireshark/-/issues/11448
546// Work around the problem by queueing up app signals and emitting them
547// after the dialog is closed.
548//
549// The following bugs might be related although they don't describe the
550// exact behavior we're working around here:
551// https://bugreports.qt.io/browse/QTBUG-38512
552// https://bugreports.qt.io/browse/QTBUG-38600
553void MainApplication::flushAppSignals()
554{
555 while (!app_signals_.isEmpty()) {
556 mainApp->emitAppSignal(app_signals_.takeFirst());
557 }
558}
559
560void MainApplication::emitStatCommandSignal(const QString &menu_path, const char *arg, void *userdata)
561{
562 emit openStatCommandDialog(menu_path, arg, userdata);
563}
564
565void MainApplication::emitTapParameterSignal(const QString cfg_abbr, const QString arg, void *userdata)
566{
567 emit openTapParameterDialog(cfg_abbr, arg, userdata);
568}
569
570// XXX Combine statistics and funnel routines into addGroupItem + groupItems?
571void MainApplication::addDynamicMenuGroupItem(int group, QAction *sg_action)
572{
573 if (!dynamic_menu_groups_.contains(group)) {
574 dynamic_menu_groups_[group] = QList<QAction *>();
575 }
576 dynamic_menu_groups_[group] << sg_action;
577}
578
579void MainApplication::appendDynamicMenuGroupItem(int group, QAction *sg_action)
580{
581 if (!added_menu_groups_.contains(group)) {
582 added_menu_groups_[group] = QList<QAction *>();
583 }
584 added_menu_groups_[group] << sg_action;
585 addDynamicMenuGroupItem(group, sg_action);
586}
587
588void MainApplication::removeDynamicMenuGroupItem(int group, QAction *sg_action)
589{
590 if (!removed_menu_groups_.contains(group)) {
591 removed_menu_groups_[group] = QList<QAction *>();
592 }
593 removed_menu_groups_[group] << sg_action;
594 dynamic_menu_groups_[group].removeAll(sg_action);
595}
596
597void MainApplication::clearDynamicMenuGroupItems()
598{
599 foreach (int group, dynamic_menu_groups_.keys())for (auto _container_599 = QtPrivate::qMakeForeachContainer(dynamic_menu_groups_
.keys()); _container_599.i != _container_599.e; ++_container_599
.i) if (int group = *_container_599.i; false) {} else
{
600 dynamic_menu_groups_[group].clear();
601 }
602}
603
604QList<QAction *> MainApplication::dynamicMenuGroupItems(int group)
605{
606 if (!dynamic_menu_groups_.contains(group)) {
607 return QList<QAction *>();
608 }
609
610 QList<QAction *> sgi_list = dynamic_menu_groups_[group];
611 std::sort(sgi_list.begin(), sgi_list.end(), qActionLessThan);
612 return sgi_list;
613}
614
615QList<QAction *> MainApplication::addedMenuGroupItems(int group)
616{
617 if (!added_menu_groups_.contains(group)) {
618 return QList<QAction *>();
619 }
620
621 QList<QAction *> sgi_list = added_menu_groups_[group];
622 std::sort(sgi_list.begin(), sgi_list.end(), qActionLessThan);
623 return sgi_list;
624}
625
626QList<QAction *> MainApplication::removedMenuGroupItems(int group)
627{
628 if (!removed_menu_groups_.contains(group)) {
629 return QList<QAction *>();
630 }
631
632 QList<QAction *> sgi_list = removed_menu_groups_[group];
633 std::sort(sgi_list.begin(), sgi_list.end(), qActionLessThan);
634 return sgi_list;
635}
636
637void MainApplication::clearAddedMenuGroupItems()
638{
639 foreach (int group, added_menu_groups_.keys())for (auto _container_639 = QtPrivate::qMakeForeachContainer(added_menu_groups_
.keys()); _container_639.i != _container_639.e; ++_container_639
.i) if (int group = *_container_639.i; false) {} else
{
640 added_menu_groups_[group].clear();
641 }
642}
643
644void MainApplication::clearRemovedMenuGroupItems()
645{
646 foreach (int group, removed_menu_groups_.keys())for (auto _container_646 = QtPrivate::qMakeForeachContainer(removed_menu_groups_
.keys()); _container_646.i != _container_646.e; ++_container_646
.i) if (int group = *_container_646.i; false) {} else
{
647 foreach (QAction *action, removed_menu_groups_[group])for (auto _container_647 = QtPrivate::qMakeForeachContainer(removed_menu_groups_
[group]); _container_647.i != _container_647.e; ++_container_647
.i) if (QAction *action = *_container_647.i; false) {} else
{
648 delete action;
649 }
650 removed_menu_groups_[group].clear();
651 }
652}
653
654#ifdef HAVE_LIBPCAP1
655
656static void
657iface_mon_event_cb(const char *iface, int added, int up)
658{
659 int present = 0;
660 unsigned ifs, j;
661 interface_t *device;
662 interface_options *interface_opts;
663
664 for (ifs = 0; ifs < global_capture_opts.all_ifaces->len; ifs++) {
665 device = &g_array_index(global_capture_opts.all_ifaces, interface_t, ifs)(((interface_t*) (void *) (global_capture_opts.all_ifaces)->
data) [(ifs)])
;
666 if (strcmp(device->name, iface) == 0) {
667 present = 1;
668 if (!up) {
669 /*
670 * Interface went down or disappeared; remove all instances
671 * of it from the current list of interfaces selected
672 * for capturing.
673 */
674 for (j = 0; j < global_capture_opts.ifaces->len; j++) {
675 interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, j)(((interface_options*) (void *) (global_capture_opts.ifaces)->
data) [(j)])
;
676 if (strcmp(interface_opts->name, device->name) == 0) {
677 capture_opts_del_iface(&global_capture_opts, j);
678 }
679 }
680 }
681 }
682 }
683
684 mainApp->emitLocalInterfaceEvent(iface, added, up);
685 if (present != up) {
686 /*
687 * We've been told that there's a new interface or that an old
688 * interface is gone; reload the local interface list.
689 *
690 * XXX: We also want to reload the local interface list if [what
691 * we can retrieve about] the capabilities of the device have changed.
692 * Ideally we'd update the capabilities of just the one device in
693 * the cache and signal that the list has been updated, instead of
694 * freeing the entire cache and scanning again - but some extcaps
695 * depend on other interfaces being up; e.g. by default androiddump
696 * tries to connect to the loopback interface to look for adb running,
697 * so if the loopback interface changes so does the status of
698 * androiddump.
699 *
700 * On Linux, at least, you can't get the capabilities from a down
701 * interface, but it's still present in all_ifaces - dumpcap returns
702 * it in the list, and we show it so the user can get a status / error
703 * message when trying to capture on it instead of it vanishing.
704 * So if both present and up are true, then we still want to refresh
705 * to update the capabilities and restart the stats.
706 *
707 * We also store the address in all_ifaces and show them to the user,
708 * so we probably should monitor those events as well and update
709 * the interface list appropriately when those change.
710 */
711 mainApp->refreshLocalInterfaces();
712 }
713}
714
715#endif
716
717void MainApplication::ifChangeEventsAvailable()
718{
719#ifdef HAVE_LIBPCAP1
720 /*
721 * Something's readable from the descriptor for interface
722 * monitoring.
723 *
724 * Have the interface-monitoring code Read whatever interface-change
725 * events are available, and call the callback for them.
726 */
727 iface_mon_event();
728#endif
729}
730
731void MainApplication::emitLocalInterfaceEvent(const char *ifname, int added, int up)
732{
733 emit localInterfaceEvent(ifname, added, up);
734}
735
736void MainApplication::refreshLocalInterfaces()
737{
738 if (active_captures_ > 0) {
739 refresh_interfaces_pending_ = true;
740 return;
741 }
742
743 refresh_interfaces_pending_ = false;
744 extcap_clear_interfaces();
745
746#ifdef HAVE_LIBPCAP1
747 emit scanLocalInterfaces(nullptr);
748#endif
749}
750
751#ifdef HAVE_LIBPCAP1
752GList* MainApplication::getInterfaceList() const
753{
754 return interface_list_copy(cached_if_list_);
755}
756
757void MainApplication::setInterfaceList(GList *if_list)
758{
759 free_interface_list(cached_if_list_);
760 cached_if_list_ = interface_list_copy(if_list);
761}
762#endif
763
764void MainApplication::allSystemsGo()
765{
766 QString display_filter = NULL__null;
767 initialized_ = true;
768 emit appInitialized();
769 while (pending_open_files_.length() > 0) {
770 emit openCaptureFile(pending_open_files_.front(), display_filter, WTAP_TYPE_AUTO0);
771 pending_open_files_.pop_front();
772 }
773
774 bool sideBarVisible = recent.gui_welcome_page_sidebar_tips_visible ||
775 recent.gui_welcome_page_sidebar_learn_visible;
776 SoftwareUpdate::instance()->init(!sideBarVisible);
777
778#ifdef HAVE_LIBPCAP1
779 int err;
780 err = iface_mon_start(&iface_mon_event_cb);
781 if (err == 0) {
782 if_notifier_ = new QSocketNotifier(iface_mon_get_sock(),
783 QSocketNotifier::Read, this);
784 connect(if_notifier_, &QSocketNotifier::activated, this, &MainApplication::ifChangeEventsAvailable);
785 }
786#endif
787}
788
789_e_prefs *MainApplication::readConfigurationFiles(bool reset)
790{
791 e_prefs *prefs_p;
792
793 if (reset) {
794 //
795 // Reset current preferences and enabled/disabled protocols and
796 // heuristic dissectors before reading.
797 // (Needed except when this is called at startup.)
798 //
799 prefs_reset(application_configuration_environment_prefix(), application_columns(), application_num_columns());
800 proto_reenable_all();
801 }
802
803 /* Load libwireshark settings from the current profile. */
804 prefs_p = epan_load_settings();
805
806 return prefs_p;
807}
808
809static void switchTranslator(QTranslator& myTranslator, const QString& filename,
810 const QString& searchPath)
811{
812 mainApp->removeTranslator(&myTranslator);
813
814 if (myTranslator.load(filename, searchPath))
815 mainApp->installTranslator(&myTranslator);
816}
817
818void MainApplication::loadLanguage(const QString newLanguage)
819{
820 QLocale locale;
821 QString localeLanguage;
822 const char* env_prefix = application_configuration_environment_prefix();
823
824 if (newLanguage.isEmpty() || newLanguage == USE_SYSTEM_LANGUAGE"system") {
825 locale = QLocale::system();
826 localeLanguage = locale.name();
827 } else {
828 localeLanguage = newLanguage;
829 locale = QLocale(localeLanguage);
830 }
831
832 QLocale::setDefault(locale);
833 switchTranslator(mainApp->translator,
834 QStringLiteral("wireshark_%1.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "wireshark_%1.qm")
))
.arg(localeLanguage), QStringLiteral(":/i18n/")(QString(QtPrivate::qMakeStringPrivate(u"" ":/i18n/"))));
835 if (QFile::exists(QStringLiteral("%1/%2/wireshark_%3.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "%1/%2/wireshark_%3.qm"
)))
836 .arg(get_datafile_dir(env_prefix)).arg("languages").arg(localeLanguage)))
837 switchTranslator(mainApp->translator,
838 QStringLiteral("wireshark_%1.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "wireshark_%1.qm")
))
.arg(localeLanguage), QStringLiteral("%1/languages")(QString(QtPrivate::qMakeStringPrivate(u"" "%1/languages"))).arg(get_datafile_dir(env_prefix)));
839 if (QFile::exists(QStringLiteral("%1/wireshark_%3.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "%1/wireshark_%3.qm"
)))
840 .arg(gchar_free_to_qstring(get_persconffile_path("languages", false, env_prefix))).arg(localeLanguage)))
841 switchTranslator(mainApp->translator,
842 QStringLiteral("wireshark_%1.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "wireshark_%1.qm")
))
.arg(localeLanguage), gchar_free_to_qstring(get_persconffile_path("languages", false, env_prefix)));
843 if (QFile::exists(QStringLiteral("%1/qt_%2.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "%1/qt_%2.qm")))
844 .arg(get_datafile_dir(env_prefix)).arg(localeLanguage))) {
845 switchTranslator(mainApp->translatorQt,
846 QStringLiteral("qt_%1.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "qt_%1.qm"))).arg(localeLanguage), QString(get_datafile_dir(env_prefix)));
847 } else if (QFile::exists(QStringLiteral("%1/qt_%2.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "%1/qt_%2.qm")))
848 .arg(get_datafile_dir(env_prefix)).arg(localeLanguage.left(localeLanguage.lastIndexOf('_'))))) {
849 switchTranslator(mainApp->translatorQt,
850 QStringLiteral("qt_%1.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "qt_%1.qm"))).arg(localeLanguage.left(localeLanguage.lastIndexOf('_'))), QString(get_datafile_dir(env_prefix)));
851 } else {
852 QString translationPath = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
853 switchTranslator(mainApp->translatorQt, QStringLiteral("qt_%1.qm")(QString(QtPrivate::qMakeStringPrivate(u"" "qt_%1.qm"))).arg(localeLanguage), translationPath);
854 }
855}
856
857void MainApplication::doTriggerMenuItem(MainMenuItem menuItem)
858{
859 switch (menuItem)
860 {
861 case FileOpenDialog:
862 emit openCaptureFile(QString(), QString(), WTAP_TYPE_AUTO0);
863 break;
864 case CaptureOptionsDialog:
865 emit openCaptureOptions();
866 break;
867 }
868}
869
870void MainApplication::captureEventHandler(CaptureEvent ev)
871{
872 switch(ev.captureContext())
873 {
874#ifdef HAVE_LIBPCAP1
875 case CaptureEvent::Update:
876 case CaptureEvent::Fixed:
877 switch (ev.eventType())
878 {
879 case CaptureEvent::Prepared:
880 iface_mon_enable(true);
881 break;
882 case CaptureEvent::Started:
883 active_captures_++;
884 emit captureActive(active_captures_);
885 break;
886 case CaptureEvent::Finished:
887 active_captures_--;
888 emit captureActive(active_captures_);
889 if (refresh_interfaces_pending_ && !global_capture_opts.restart) {
890 refreshLocalInterfaces();
891 }
892 break;
893 default:
894 break;
895 }
896 break;
897#endif
898 case CaptureEvent::File:
899 case CaptureEvent::Reload:
900 case CaptureEvent::Rescan:
901 switch (ev.eventType())
902 {
903 case CaptureEvent::Started:
904 QTimer::singleShot(TAP_UPDATE_DEFAULT_INTERVAL3000 / 5, this, SLOT(updateTaps())qFlagLocation("1" "updateTaps()" "\0" "ui/qt/main_application.cpp"
":" "904")
);
905 QTimer::singleShot(TAP_UPDATE_DEFAULT_INTERVAL3000 / 2, this, SLOT(updateTaps())qFlagLocation("1" "updateTaps()" "\0" "ui/qt/main_application.cpp"
":" "905")
);
906 break;
907 case CaptureEvent::Finished:
908 updateTaps();
909 break;
910 default:
911 break;
912 }
913 break;
914 default:
915 break;
916 }
917}
918
919void MainApplication::pushStatus(StatusInfo status, const QString &message, const QString &messagetip)
920{
921 MainWindow * mw = mainWindow();
922 if (! mw) {
923 return;
924 }
925
926 MainStatusBar * bar = mw->statusBar();
927 if (! bar) {
928 return;
929 }
930
931 switch(status)
932 {
933 case FilterSyntax:
934 bar->pushGenericStatus(MainStatusBar::STATUS_CTX_FILTER, message);
935 break;
936 case FieldStatus:
937 bar->pushGenericStatus(MainStatusBar::STATUS_CTX_FIELD, message);
938 break;
939 case FileStatus:
940 bar->pushGenericStatus(MainStatusBar::STATUS_CTX_FILE, message, messagetip);
941 break;
942 case ByteStatus:
943 bar->pushGenericStatus(MainStatusBar::STATUS_CTX_BYTE, message);
944 break;
945 case BusyStatus:
946 bar->pushGenericStatus(MainStatusBar::STATUS_CTX_PROGRESS, message, messagetip);
947 break;
948 case TemporaryStatus:
949 bar->pushGenericStatus(MainStatusBar::STATUS_CTX_TEMPORARY, message);
950 break;
951 }
952}
953
954void MainApplication::popStatus(StatusInfo status)
955{
956 MainWindow * mw = mainWindow();
957 if (! mw) {
958 return;
959 }
960
961 MainStatusBar * bar = mw->statusBar();
962 if (! bar) {
963 return;
964 }
965
966 switch(status)
967 {
968 case FilterSyntax:
969 bar->popGenericStatus(MainStatusBar::STATUS_CTX_FILTER);
970 break;
971 case FieldStatus:
972 bar->popGenericStatus(MainStatusBar::STATUS_CTX_FIELD);
973 break;
974 case FileStatus:
975 bar->popGenericStatus(MainStatusBar::STATUS_CTX_FILE);
976 break;
977 case ByteStatus:
978 bar->popGenericStatus(MainStatusBar::STATUS_CTX_BYTE);
979 break;
980 case BusyStatus:
981 bar->popGenericStatus(MainStatusBar::STATUS_CTX_PROGRESS);
982 break;
983 case TemporaryStatus:
984 bar->popGenericStatus(MainStatusBar::STATUS_CTX_TEMPORARY);
985 break;
986 }
987}
988
989void MainApplication::gotoFrame(int frame)
990{
991 MainWindow * mw = mainWindow();
992 if (! mw) {
993 return;
994 }
995
996 mw->gotoFrame(frame);
997}
998
999void MainApplication::reloadDisplayFilterMacros()
1000{
1001 dfilter_macro_reload(application_configuration_environment_prefix());
1002 // The signal is needed when the display filter grammar changes for
1003 // any reason (not just "fields".)
1004 mainApp->emitAppSignal(MainApplication::FieldsChanged);
1005}