Skip to content
Snippets Groups Projects
Commit b2455c62 authored by Kevin Jeon's avatar Kevin Jeon
Browse files

Explicitly initialize Stats fields

This change adds InitStats for explictly zero-initializing rx/tx
bytes/packets, and -1-initializing rx/tx tcp packets.

Test: Build, existing unit tests.
Bug: 241098920
Change-Id: Ib9598dab924b48240461b31906a4b5698db255d8
parent 50f07be9
No related branches found
No related tags found
No related merge requests found
...@@ -40,8 +40,19 @@ namespace bpf { ...@@ -40,8 +40,19 @@ namespace bpf {
using base::Result; using base::Result;
// This explicitly zero-initializes the relevant Stats fields.
void InitStats(Stats* stats) {
stats->rxBytes = 0;
stats->rxPackets = 0;
stats->txBytes = 0;
stats->txPackets = 0;
stats->tcpRxPackets = -1;
stats->tcpTxPackets = -1;
}
int bpfGetUidStatsInternal(uid_t uid, Stats* stats, int bpfGetUidStatsInternal(uid_t uid, Stats* stats,
const BpfMap<uint32_t, StatsValue>& appUidStatsMap) { const BpfMap<uint32_t, StatsValue>& appUidStatsMap) {
InitStats(stats);
auto statsEntry = appUidStatsMap.readValue(uid); auto statsEntry = appUidStatsMap.readValue(uid);
if (statsEntry.ok()) { if (statsEntry.ok()) {
stats->rxPackets = statsEntry.value().rxPackets; stats->rxPackets = statsEntry.value().rxPackets;
...@@ -61,9 +72,8 @@ int bpfGetUidStats(uid_t uid, Stats* stats) { ...@@ -61,9 +72,8 @@ int bpfGetUidStats(uid_t uid, Stats* stats) {
int bpfGetIfaceStatsInternal(const char* iface, Stats* stats, int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
const BpfMap<uint32_t, StatsValue>& ifaceStatsMap, const BpfMap<uint32_t, StatsValue>& ifaceStatsMap,
const BpfMap<uint32_t, IfaceValue>& ifaceNameMap) { const BpfMap<uint32_t, IfaceValue>& ifaceNameMap) {
InitStats(stats);
int64_t unknownIfaceBytesTotal = 0; int64_t unknownIfaceBytesTotal = 0;
stats->tcpRxPackets = -1;
stats->tcpTxPackets = -1;
const auto processIfaceStats = const auto processIfaceStats =
[iface, stats, &ifaceNameMap, &unknownIfaceBytesTotal]( [iface, stats, &ifaceNameMap, &unknownIfaceBytesTotal](
const uint32_t& key, const uint32_t& key,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment