From a9e6a71f9435a50c582993e946be4d53828bb48d Mon Sep 17 00:00:00 2001
From: maminjie <maminjie1@huawei.com>
Date: Sat, 13 Mar 2021 16:03:03 +0800
Subject: [PATCH] Fix the invalid memory address or nil pointer reference

---
 libpod/stats.go | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libpod/stats.go b/libpod/stats.go
index c58a461..97a2169 100644
--- a/libpod/stats.go
+++ b/libpod/stats.go
@@ -68,9 +68,11 @@
 	stats.CPU = calculateCPUPercent(cgroupStats, previousCPU, now, previousStats.SystemNano)
 	stats.AvgCPU = calculateAvgCPU(stats.CPU, previousStats.AvgCPU, previousStats.DataPoints)
 	stats.DataPoints = previousStats.DataPoints + 1
-	stats.MemUsage = cgroupStats.Memory.Usage.Usage
-	stats.MemLimit = c.getMemLimit()
-	stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
+	if cgroupStats.Memory != (cgroups.MemoryMetrics{}){
+		stats.MemUsage = cgroupStats.Memory.Usage.Usage
+		stats.MemLimit = c.getMemLimit()
+		stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
+	}
 	stats.PIDs = 0
 	if conState == define.ContainerStateRunning || conState == define.ContainerStatePaused {
 		stats.PIDs = cgroupStats.Pids.Current
-- 
2.23.0

