File tree Expand file tree Collapse file tree 3 files changed +22
-4
lines changed Expand file tree Collapse file tree 3 files changed +22
-4
lines changed Original file line number Diff line number Diff line change 1
1
#pragma once
2
2
3
+ #include < cstddef>
3
4
#include < stdio.h>
4
5
#include < stdint.h>
5
6
#include < stdexcept>
@@ -121,19 +122,36 @@ namespace pcpp
121
122
m_Vector.clear ();
122
123
}
123
124
125
+ /* *
126
+ * Adding a nullptr to the vector is not allowed.
127
+ */
128
+ void pushBack (std::nullptr_t element, bool freeElementOnError = true ) = delete;
129
+
124
130
/* *
125
131
* Add a new (pointer to an) element to the vector
126
132
* @param[in] element A pointer to an element to assume ownership of.
133
+ * @param[in] freeElementOnError If set to true, the element is freed if an exception is thrown during the push.
127
134
* @throws std::invalid_argument The provided pointer is a nullptr.
128
135
*/
129
- void pushBack (T* element)
136
+ void pushBack (T* element, bool freeElementOnError = true )
130
137
{
131
138
if (element == nullptr )
132
139
{
133
140
throw std::invalid_argument (" Element is nullptr" );
134
141
}
135
142
136
- m_Vector.push_back (element);
143
+ try
144
+ {
145
+ m_Vector.push_back (element);
146
+ }
147
+ catch (const std::exception&)
148
+ {
149
+ if (freeElementOnError)
150
+ {
151
+ delete element;
152
+ }
153
+ throw ;
154
+ }
137
155
}
138
156
139
157
/* *
Original file line number Diff line number Diff line change @@ -321,7 +321,7 @@ namespace pcpp
321
321
{
322
322
auto encodedRecord = (*recordIter)->encode ();
323
323
auto copyRecord = Asn1Record::decode (encodedRecord.data (), encodedRecord.size (), false );
324
- m_SubRecords.pushBack (copyRecord. release ( ));
324
+ m_SubRecords.pushBack (std::move (copyRecord ));
325
325
recordValueLength += encodedRecord.size ();
326
326
}
327
327
Original file line number Diff line number Diff line change @@ -749,7 +749,7 @@ namespace pcpp {
749
749
Asn1OctetStringRecord typeRecord (attribute.type );
750
750
Asn1SetRecord valuesRecord (valuesSubRecords);
751
751
752
- attributesSubRecords.pushBack (new Asn1SequenceRecord ({&typeRecord, &valuesRecord}));
752
+ attributesSubRecords.pushBack (new Asn1SequenceRecord ({ &typeRecord, &valuesRecord }));
753
753
}
754
754
755
755
Asn1OctetStringRecord objectNameRecord (objectName);
You can’t perform that action at this time.
0 commit comments