Skip to content

Commit

Permalink
Fix nil input to starting key of batch lookup
Browse files Browse the repository at this point in the history
Batch lookup requires the caller to provide the starting key
(`startKey`) from which the lookup begins. In order to perform a batch
lookup from the very beginning of the map, it is expected to pass a nil
`startKey`. Due to the type of `startKey` being `interface{}`, this
means that it's possible for the value of `startKey` to have a concrete
type T, but contain a nil value.

For example, let's say the concrete type T is an *int.

```
func dumpAll[T comparable](m *Map, startKey, key *T) (count int, err error) {
    ...
    m.batchLookup(..., startKey, ...)
    ...
}

dumpAll(m, nil, new(int))
```

The first call to `dumpAll()` must pass nil to `startKey`. When it
reaches `(*Map).batchLookup()`, `startKey` becomes a _non-nil_
`interface{}` with an underlying concrete type of `*int`, whose value
_is_ nil.

This is problematic because `startKey != nil` evaluates to true and the
call to `marshalMapSyscallInput()` expects `startKey` to contain a
non-nil value.

This commit fixes this by checking whether the underlying value of the
interface contains a non-nil value.

Signed-off-by: Chris Tarazi <[email protected]>
  • Loading branch information
christarazi committed Oct 31, 2023
1 parent 572c585 commit 9c87531
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,8 @@ func (m *Map) batchLookup(cmd sys.Cmd, startKey, nextKeyOut, keysOut, valuesOut
}

var err error
if startKey != nil {
startKeyValue := reflect.ValueOf(startKey)
if startKey != nil && (startKeyValue != reflect.Value{} && !startKeyValue.IsNil()) {
attr.InBatch, err = marshalMapSyscallInput(startKey, int(m.keySize))
if err != nil {
return 0, err
Expand Down

0 comments on commit 9c87531

Please sign in to comment.