From: Masud Hasan (mashasan) Date: Tue, 31 May 2022 16:26:13 +0000 (+0000) Subject: Pull request #3442: appid: Added lock_guard to prevent data race on reload X-Git-Tag: 3.1.31.0~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c9c9920addebb140ee34c69bd11b727f89b54f81;p=thirdparty%2Fsnort3.git Pull request #3442: appid: Added lock_guard to prevent data race on reload Merge in SNORT/snort3 from ~OSTEPANO/snort3:tasan_appid_reload to master Squashed commit of the following: commit 5af9c9ad1b0ed389fb35d0d3cfff45dae3df46a8 Author: ostepano Date: Tue May 24 09:15:25 2022 -0400 appid: Added lock_guard to prevent data race on reload --- diff --git a/src/network_inspectors/appid/appid_peg_counts.cc b/src/network_inspectors/appid/appid_peg_counts.cc index 8f52b4ada..9413a871d 100644 --- a/src/network_inspectors/appid/appid_peg_counts.cc +++ b/src/network_inspectors/appid/appid_peg_counts.cc @@ -92,18 +92,24 @@ void AppIdPegCounts::sum_stats() if (!appid_peg_counts) return; - const unsigned peg_num = appid_peg_counts->size() ? (appid_peg_counts->size() - 1) : 0; - const AppIdDynamicPeg* ptr = (AppIdDynamicPeg*)appid_peg_counts->data(); + static std::mutex r_mutex; - for ( unsigned i = 0; i < peg_num; ++i ) { + std::lock_guard _lock(r_mutex); + const unsigned peg_num = appid_peg_counts->size() ? (appid_peg_counts->size() - 1) : 0; + const AppIdDynamicPeg* ptr = (AppIdDynamicPeg*)appid_peg_counts->data(); + + for ( unsigned i = 0; i < peg_num; ++i ) + { + for (unsigned j = 0; j < DetectorPegs::NUM_APPID_DETECTOR_PEGS; ++j) + appid_dynamic_sum[i].stats[j] += ptr[i].stats[j]; + } + + // unknown_app stats for (unsigned j = 0; j < DetectorPegs::NUM_APPID_DETECTOR_PEGS; ++j) - appid_dynamic_sum[i].stats[j] += ptr[i].stats[j]; + appid_dynamic_sum[SF_APPID_MAX].stats[j] += ptr[peg_num].stats[j]; } - - // unknown_app stats - for (unsigned j = 0; j < DetectorPegs::NUM_APPID_DETECTOR_PEGS; ++j) - appid_dynamic_sum[SF_APPID_MAX].stats[j] += ptr[peg_num].stats[j]; + } void AppIdPegCounts::inc_service_count(AppId id)