Skip to content

Commit

Permalink
Fix zed.MapperLookupCache.Lookup OOM on bad ID (#4864)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nwt authored Nov 9, 2023
1 parent 78d2ab6 commit c41a758
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c41a758

Please sign in to comment.