From 8255c8227681a34269144db63973248a1f0719d5 Mon Sep 17 00:00:00 2001 From: Gabriele Cimato Date: Fri, 14 Jun 2024 13:47:16 -0500 Subject: [PATCH] fix: account for orderedMap to be nil --- builtin_set.go | 50 +++++++++++++++++++++++---------------------- builtin_set_test.go | 7 +++++++ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/builtin_set.go b/builtin_set.go index 77deb3f4..b8697380 100644 --- a/builtin_set.go +++ b/builtin_set.go @@ -394,35 +394,37 @@ func (so *setObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, err error) return memUsage, err } - if ctx.ObjectPropsLenExceedsThreshold(so.m.size) { - inc, err := so.estimateMemUsage(ctx) - memUsage += inc - if err != nil { - return memUsage, err - } - } else { - for _, entry := range so.m.hashTable { - if entry == nil { - continue + if so.m != nil { + if ctx.ObjectPropsLenExceedsThreshold(so.m.size) { + inc, err := so.estimateMemUsage(ctx) + memUsage += inc + if err != nil { + return memUsage, err } + } else { + for _, entry := range so.m.hashTable { + if entry == nil { + continue + } - if entry.key != nil { - inc, err := entry.key.MemUsage(ctx) - memUsage += inc - if err != nil { - return memUsage, err + if entry.key != nil { + inc, err := entry.key.MemUsage(ctx) + memUsage += inc + if err != nil { + return memUsage, err + } } - } - if entry.value != nil { - inc, err := entry.value.MemUsage(ctx) - memUsage += inc - if err != nil { - return memUsage, err + if entry.value != nil { + inc, err := entry.value.MemUsage(ctx) + memUsage += inc + if err != nil { + return memUsage, err + } + } + if exceeded := ctx.MemUsageLimitExceeded(memUsage); exceeded { + return memUsage, nil } - } - if exceeded := ctx.MemUsageLimitExceeded(memUsage); exceeded { - return memUsage, nil } } } diff --git a/builtin_set_test.go b/builtin_set_test.go index b6cd5987..de9c6789 100644 --- a/builtin_set_test.go +++ b/builtin_set_test.go @@ -274,6 +274,13 @@ func TestSetObjectMemUsage(t *testing.T) { (5+SizeString)*20, errExpected: nil, }, + { + name: "mem is SizeEmptyStruct given a nil orderedMap object", + mu: NewMemUsageContext(vm, 88, 5000, 50, 50, 0.1, TestNativeMemUsageChecker{}), + so: &setObject{}, + expectedMem: SizeEmptyStruct, + errExpected: nil, + }, } for _, tc := range tests {