From b173a1e44c50003d60c3d08fd87c9d324e467764 Mon Sep 17 00:00:00 2001 From: James DeFelice Date: Sat, 4 Apr 2015 12:39:09 +0000 Subject: [PATCH] pointers that reference addresses of the internal circular buffer should not escape --- storage/memory/stats_buffer.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/storage/memory/stats_buffer.go b/storage/memory/stats_buffer.go index f5438c65..b71bfe2a 100644 --- a/storage/memory/stats_buffer.go +++ b/storage/memory/stats_buffer.go @@ -23,7 +23,7 @@ import ( // A circular buffer for ContainerStats. type StatsBuffer struct { - buffer []info.ContainerStats + buffer []*info.ContainerStats size int index int } @@ -31,7 +31,7 @@ type StatsBuffer struct { // Returns a new thread-compatible StatsBuffer. func NewStatsBuffer(size int) *StatsBuffer { return &StatsBuffer{ - buffer: make([]info.ContainerStats, size), + buffer: make([]*info.ContainerStats, size), size: 0, index: size - 1, } @@ -43,7 +43,8 @@ func (self *StatsBuffer) Add(item *info.ContainerStats) { self.size++ } self.index = (self.index + 1) % len(self.buffer) - self.buffer[self.index] = *item + copied := *item + self.buffer[self.index] = &copied } // Returns up to maxResult elements in the specified time period (inclusive). @@ -131,7 +132,7 @@ func (self *StatsBuffer) FirstN(n int) []*info.ContainerStats { res := make([]*info.ContainerStats, n) for i := 0; i < n; i++ { index := (start + i) % len(self.buffer) - res[i] = &self.buffer[index] + res[i] = self.buffer[index] } return res } @@ -142,7 +143,7 @@ func (self *StatsBuffer) Get(index int) *info.ContainerStats { if calculatedIndex < 0 { calculatedIndex += len(self.buffer) } - return &self.buffer[calculatedIndex] + return self.buffer[calculatedIndex] } func (self *StatsBuffer) Size() int {