From fec355f53687dfbee962f611297355059efabd48 Mon Sep 17 00:00:00 2001 From: ardnew Date: Wed, 25 May 2022 15:57:46 -0500 Subject: [PATCH] x/term: prevent invalid indexing into stRingBuffer The exported method (*stRingBuffer).NthPreviousEntry does not correctly handle arguments with negative values. A negative value will index beyond slice boundaries in most cases (unless size = max = INT_MAX) and cause an access violation at runtime. This change adds a condition to return ok = false for all negatively valued arguments, which is the same behavior that occurs with positively valued arguments exceeding buffer length. Adding the capability to index backwards (from the end of the slice) does not seem like the intent of this method, and it would not improve or simplify existing functionality. It would also be inconsistent with the handling of positive values out-of-bounds. --- terminal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terminal.go b/terminal.go index 535ab82..4b48a58 100644 --- a/terminal.go +++ b/terminal.go @@ -935,7 +935,7 @@ func (s *stRingBuffer) Add(a string) { // next most recent, and so on. If such an element doesn't exist then ok is // false. func (s *stRingBuffer) NthPreviousEntry(n int) (value string, ok bool) { - if n >= s.size { + if n < 0 || n >= s.size { return "", false } index := s.head - n