Bug Summary

File:builds/wireshark/wireshark/ui/qt/models/supported_protocols_model.cpp
Warning:line 185, column 5
Potential leak of memory pointed to by 'protoItem'

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 supported_protocols_model.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-18/lib/clang/18 -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 /usr/include/x86_64-linux-gnu/qt6/QtWidgets -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/QtGui -isystem /usr/include/x86_64-linux-gnu/qt6/QtCore5Compat -isystem /usr/include/x86_64-linux-gnu/qt6/QtConcurrent -isystem /usr/include/x86_64-linux-gnu/qt6/QtPrintSupport -isystem /usr/include/x86_64-linux-gnu/qt6/QtMultimedia -isystem /usr/include/x86_64-linux-gnu/qt6/QtNetwork -isystem /usr/include/x86_64-linux-gnu/qt6/QtDBus -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_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/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -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 -std=c++17 -fdeprecated-macro -ferror-limit 19 -fwrapv -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 -dwarf-debug-flags /usr/lib/llvm-18/bin/clang --driver-mode=g++ -### --analyze -x c++ -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_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 -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 /usr/include/x86_64-linux-gnu/qt6/QtWidgets -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/QtGui -isystem /usr/include/x86_64-linux-gnu/qt6/QtCore5Compat -isystem /usr/include/x86_64-linux-gnu/qt6/QtConcurrent -isystem /usr/include/x86_64-linux-gnu/qt6/QtPrintSupport -isystem /usr/include/x86_64-linux-gnu/qt6/QtMultimedia -isystem /usr/include/x86_64-linux-gnu/qt6/QtNetwork -isystem /usr/include/x86_64-linux-gnu/qt6/QtDBus -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 -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -std=c++17 -fPIC -fPIC /builds/wireshark/wireshark/ui/qt/models/supported_protocols_model.cpp -o /builds/wireshark/wireshark/sbout/2024-11-20-100252-3912-1 -Xclang -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /builds/wireshark/wireshark/sbout/2024-11-20-100252-3912-1 -x c++ /builds/wireshark/wireshark/ui/qt/models/supported_protocols_model.cpp
1/* supported_protocols_model.cpp
2 *
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <[email protected]>
5 * Copyright 1998 Gerald Combs
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10#include <QSortFilterProxyModel>
11#include <QStringList>
12#include <QPalette>
13#include <QApplication>
14#include <QBrush>
15#include <QRegularExpression>
16
17#include <ui/qt/models/supported_protocols_model.h>
18
19SupportedProtocolsItem::SupportedProtocolsItem(protocol_t* proto, const char *name, const char* filter, ftenum_t ftype, const char* descr, SupportedProtocolsItem* parent)
20 : ModelHelperTreeItem<SupportedProtocolsItem>(parent),
21 proto_(proto),
22 name_(name),
23 filter_(filter),
24 ftype_(ftype),
25 descr_(descr)
26{
27}
28
29SupportedProtocolsItem::~SupportedProtocolsItem()
30{
31}
32
33
34SupportedProtocolsModel::SupportedProtocolsModel(QObject *parent) :
35 QAbstractItemModel(parent),
36 root_(new SupportedProtocolsItem(NULL__null, NULL__null, NULL__null, FT_NONE, NULL__null, NULL__null)),
37 field_count_(0)
38{
39}
40
41SupportedProtocolsModel::~SupportedProtocolsModel()
42{
43 delete root_;
44}
45
46int SupportedProtocolsModel::rowCount(const QModelIndex &parent) const
47{
48 SupportedProtocolsItem *parent_item;
49 if (parent.column() > 0)
50 return 0;
51
52 if (!parent.isValid())
53 parent_item = root_;
54 else
55 parent_item = static_cast<SupportedProtocolsItem*>(parent.internalPointer());
56
57 if (parent_item == NULL__null)
58 return 0;
59
60 return parent_item->childCount();
61}
62
63int SupportedProtocolsModel::columnCount(const QModelIndex&) const
64{
65 return colLast;
66}
67
68QVariant SupportedProtocolsModel::headerData(int section, Qt::Orientation orientation, int role) const
69{
70 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
71
72 switch ((enum SupportedProtocolsColumn)section) {
73 case colName:
74 return tr("Name");
75 case colFilter:
76 return tr("Filter");
77 case colType:
78 return tr("Type");
79 case colDescription:
80 return tr("Description");
81 default:
82 break;
83 }
84 }
85 return QVariant();
86}
87
88QModelIndex SupportedProtocolsModel::parent(const QModelIndex& index) const
89{
90 if (!index.isValid())
91 return QModelIndex();
92
93 SupportedProtocolsItem* item = static_cast<SupportedProtocolsItem*>(index.internalPointer());
94 if (item != NULL__null) {
95 SupportedProtocolsItem* parent_item = item->parentItem();
96 if (parent_item != NULL__null) {
97 if (parent_item == root_)
98 return QModelIndex();
99
100 return createIndex(parent_item->row(), 0, parent_item);
101 }
102 }
103
104 return QModelIndex();
105}
106
107QModelIndex SupportedProtocolsModel::index(int row, int column, const QModelIndex& parent) const
108{
109 if (!hasIndex(row, column, parent))
110 return QModelIndex();
111
112 SupportedProtocolsItem *parent_item, *child_item;
113
114 if (!parent.isValid())
115 parent_item = root_;
116 else
117 parent_item = static_cast<SupportedProtocolsItem*>(parent.internalPointer());
118
119 Q_ASSERT(parent_item)((parent_item) ? static_cast<void>(0) : qt_assert("parent_item"
, "ui/qt/models/supported_protocols_model.cpp", 119))
;
120
121 child_item = parent_item->child(row);
122 if (child_item) {
123 return createIndex(row, column, child_item);
124 }
125
126 return QModelIndex();
127}
128
129QVariant SupportedProtocolsModel::data(const QModelIndex &index, int role) const
130{
131 if (!index.isValid() || role != Qt::DisplayRole)
132 return QVariant();
133
134 SupportedProtocolsItem* item = static_cast<SupportedProtocolsItem*>(index.internalPointer());
135 if (item == NULL__null)
136 return QVariant();
137
138 switch ((enum SupportedProtocolsColumn)index.column()) {
139 case colName:
140 return item->name();
141 case colFilter:
142 return item->filter();
143 case colType:
144 if (index.parent().isValid())
145 return QString(ftype_pretty_name(item->type()));
146
147 return QVariant();
148 case colDescription:
149 return item->description();
150 default:
151 break;
152 }
153
154 return QVariant();
155}
156
157void SupportedProtocolsModel::populate()
158{
159 void *proto_cookie;
160 void *field_cookie;
161
162 beginResetModel();
163
164 SupportedProtocolsItem *protoItem, *fieldItem;
165 protocol_t *protocol;
166
167 for (int proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1;
1
Assuming the condition is true
2
Loop condition is true. Entering loop body
6
Assuming the condition is false
7
Loop condition is false. Execution continues on line 185
168 proto_id = proto_get_next_protocol(&proto_cookie)) {
169
170 protocol = find_protocol_by_id(proto_id);
171 protoItem = new SupportedProtocolsItem(protocol, proto_get_protocol_short_name(protocol), proto_get_protocol_filter_name(proto_id), FT_PROTOCOL, proto_get_protocol_long_name(protocol), root_);
3
Memory is allocated
172 root_->prependChild(protoItem);
173
174 for (header_field_info *hfinfo = proto_get_first_protocol_field(proto_id, &field_cookie); hfinfo != NULL__null;
4
Assuming 'hfinfo' is equal to NULL
5
Loop condition is false. Execution continues on line 168
175 hfinfo = proto_get_next_protocol_field(proto_id, &field_cookie)) {
176 if (hfinfo->same_name_prev_id != -1)
177 continue;
178
179 fieldItem = new SupportedProtocolsItem(protocol, hfinfo->name, hfinfo->abbrev, hfinfo->type, hfinfo->blurb, protoItem);
180 protoItem->prependChild(fieldItem);
181 field_count_++;
182 }
183 }
184
185 endResetModel();
8
Potential leak of memory pointed to by 'protoItem'
186}
187
188
189
190SupportedProtocolsProxyModel::SupportedProtocolsProxyModel(QObject * parent)
191: QSortFilterProxyModel(parent),
192filter_()
193{
194}
195
196bool SupportedProtocolsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
197{
198 //Use SupportedProtocolsItem directly for better performance
199 SupportedProtocolsItem* left_item = static_cast<SupportedProtocolsItem*>(left.internalPointer());
200 SupportedProtocolsItem* right_item = static_cast<SupportedProtocolsItem*>(right.internalPointer());
201
202 if ((left_item != NULL__null) && (right_item != NULL__null)) {
203 int compare_ret = QString::compare(left_item->name(), right_item->name(), Qt::CaseInsensitive);
204 if (compare_ret < 0)
205 return true;
206 }
207
208 return false;
209}
210
211bool SupportedProtocolsProxyModel::filterAcceptItem(SupportedProtocolsItem& item) const
212{
213 QRegularExpression regex(filter_, QRegularExpression::CaseInsensitiveOption);
214 if (! regex.isValid())
215 return false;
216
217 if (item.name().contains(regex))
218 return true;
219
220 if (item.filter().contains(regex))
221 return true;
222
223 if (item.description().contains(regex))
224 return true;
225
226 return false;
227}
228
229bool SupportedProtocolsProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
230{
231 QModelIndex nameIdx = sourceModel()->index(sourceRow, SupportedProtocolsModel::colName, sourceParent);
232 SupportedProtocolsItem* item = static_cast<SupportedProtocolsItem*>(nameIdx.internalPointer());
233 if (item == NULL__null)
234 return true;
235
236 if (!filter_.isEmpty()) {
237 if (filterAcceptItem(*item))
238 return true;
239
240 if (!nameIdx.parent().isValid())
241 {
242 SupportedProtocolsItem* child_item;
243 for (int row = 0; row < item->childCount(); row++)
244 {
245 child_item = item->child(row);
246 if ((child_item != NULL__null) && (filterAcceptItem(*child_item)))
247 return true;
248 }
249 }
250
251 return false;
252 }
253
254 return true;
255}
256
257void SupportedProtocolsProxyModel::setFilter(const QString& filter)
258{
259 filter_ = filter;
260 invalidateFilter();
261}