From c41a75852b787ff07103de3ebc0a2f2a6eeaaebe Mon Sep 17 00:00:00 2001 From: Noah Treuhaft Date: Thu, 9 Nov 2023 14:14:19 -0500 Subject: [PATCH] Fix zed.MapperLookupCache.Lookup OOM on bad ID (#4864) Passing a large type ID to Lookup can cause it to allocate an arbirary amount of memory. Fix by not growing the cache if the ID is unknown to the underlying zed.Mapper. Closes #4858. --- mapper.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mapper.go b/mapper.go index eeaa6e3b87..5348ab3da5 100644 --- a/mapper.go +++ b/mapper.go @@ -77,12 +77,19 @@ func (m *MapperLookupCache) Reset(mapper *Mapper) { } func (m *MapperLookupCache) Lookup(id int) Type { - if id >= len(m.cache) { - m.cache = append(m.cache, make([]Type, id+1-len(m.cache))...) - } else if typ := m.cache[id]; typ != nil { - return typ + if id < len(m.cache) { + if typ := m.cache[id]; typ != nil { + return typ + } } typ := m.mapper.Lookup(id) + if typ == nil { + // To prevent OOM, don't grow cache if id is unknown. + return nil + } + if id >= len(m.cache) { + m.cache = slices.Grow(m.cache[:0], id+1)[:id+1] + } m.cache[id] = typ return typ }