From: Mike Stepanek (mstepane) Date: Mon, 31 Aug 2020 12:20:25 +0000 (+0000) Subject: Merge pull request #2438 in SNORT/snort3 from ~KATHARVE/snort3:cppcheck_fix to master X-Git-Tag: 3.0.2-6~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ed183be4d19a780a0d73265f97b8fbf502df4e54;p=thirdparty%2Fsnort3.git Merge pull request #2438 in SNORT/snort3 from ~KATHARVE/snort3:cppcheck_fix to master Squashed commit of the following: commit ff30d14526f064e5f5960b68e718585df543ed85 Author: Katura Harvey Date: Sun Aug 30 14:18:30 2020 -0400 http2_inspect: convert circular_array to std:vector --- diff --git a/src/service_inspectors/http2_inspect/http2_hpack_dynamic_table.cc b/src/service_inspectors/http2_inspect/http2_hpack_dynamic_table.cc index f90d77a41..ee1c34903 100644 --- a/src/service_inspectors/http2_inspect/http2_hpack_dynamic_table.cc +++ b/src/service_inspectors/http2_inspect/http2_hpack_dynamic_table.cc @@ -32,9 +32,11 @@ using namespace Http2Enums; HpackDynamicTable::~HpackDynamicTable() { - for (unsigned i = 0; i < ARRAY_CAPACITY; i++) - delete circular_array[i]; - delete[] circular_array; + for (std::vector::iterator it = circular_buf.begin(); + it != circular_buf.end(); ++it) + { + delete *it; + } } bool HpackDynamicTable::add_entry(const Field& name, const Field& value) @@ -55,14 +57,14 @@ bool HpackDynamicTable::add_entry(const Field& name, const Field& value) // Create new entry. This is done before pruning because the entry referenced by the new name // may be pruned. - HpackTableEntry *new_entry = new HpackTableEntry(name, value); + HpackTableEntry* new_entry = new HpackTableEntry(name, value); // If add entry would exceed max table size, evict old entries prune_to_size(max_size - new_entry_size); // Add new entry to the front of the table (newest entry = lowest index) start = (start + ARRAY_CAPACITY - 1) % ARRAY_CAPACITY; - circular_array[start] = new_entry; + circular_buf[start] = new_entry; num_entries++; if (num_entries > Http2Module::get_peg_counts(PEG_MAX_TABLE_ENTRIES)) @@ -80,7 +82,7 @@ const HpackTableEntry* HpackDynamicTable::get_entry(uint32_t virtual_index) cons return nullptr; const uint32_t arr_index = (start + dyn_index) % ARRAY_CAPACITY; - return circular_array[arr_index]; + return circular_buf[arr_index]; } /* This is called when adding a new entry and when receiving a dynamic table size update. @@ -95,10 +97,10 @@ void HpackDynamicTable::prune_to_size(uint32_t new_max_size) { const uint32_t last_index = (start + num_entries - 1 + ARRAY_CAPACITY) % ARRAY_CAPACITY; num_entries--; - rfc_table_size -= circular_array[last_index]->name.length() + - circular_array[last_index]->value.length() + RFC_ENTRY_OVERHEAD; - delete circular_array[last_index]; - circular_array[last_index] = nullptr; + rfc_table_size -= circular_buf[last_index]->name.length() + + circular_buf[last_index]->value.length() + RFC_ENTRY_OVERHEAD; + delete circular_buf[last_index]; + circular_buf[last_index] = nullptr; } } diff --git a/src/service_inspectors/http2_inspect/http2_hpack_dynamic_table.h b/src/service_inspectors/http2_inspect/http2_hpack_dynamic_table.h index 7a1f03bc4..f062fb1a7 100644 --- a/src/service_inspectors/http2_inspect/http2_hpack_dynamic_table.h +++ b/src/service_inspectors/http2_inspect/http2_hpack_dynamic_table.h @@ -25,13 +25,15 @@ #include "http2_enum.h" +#include + struct HpackTableEntry; class HpackDynamicTable { public: // FIXIT-P This array can be optimized to start smaller and grow on demand - HpackDynamicTable() : circular_array(new HpackTableEntry*[ARRAY_CAPACITY]()) { } + HpackDynamicTable() : circular_buf(4096, nullptr) {} ~HpackDynamicTable(); const HpackTableEntry* get_entry(uint32_t index) const; bool add_entry(const Field& name, const Field& value); @@ -39,8 +41,6 @@ public: uint32_t get_max_size() { return max_size; } private: - void expand_array(); - const static uint32_t RFC_ENTRY_OVERHEAD = 32; const static uint32_t DEFAULT_MAX_SIZE = 4096; @@ -50,7 +50,7 @@ private: uint32_t start = 0; uint32_t num_entries = 0; uint32_t rfc_table_size = 0; - HpackTableEntry** circular_array; + std::vector circular_buf; void prune_to_size(uint32_t new_max_size); };