From 6dfb0a8a97718d9e3f1daf375faf647470f1d0d9 Mon Sep 17 00:00:00 2001 From: Jonathan Rosenberg <96974219+Jonathan-Rosenberg@users.noreply.github.com> Date: Thu, 1 Jun 2023 17:56:55 +0300 Subject: [PATCH 1/2] add ownership check. if the value isn't owned, copy it --- pkg/graveler/sstable/range_manager.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/graveler/sstable/range_manager.go b/pkg/graveler/sstable/range_manager.go index 6c77ae48849..f94705e17ad 100644 --- a/pkg/graveler/sstable/range_manager.go +++ b/pkg/graveler/sstable/range_manager.go @@ -92,7 +92,13 @@ func (m *RangeManager) GetValueGE(ctx context.Context, ns committed.Namespace, i } return nil, ErrKeyNotFound } - vBytes, _, err := value.Value(nil) + vBytes, owned, err := value.Value(nil) + if !owned { + b := vBytes + vBytes = make([]byte, len(b)) + copy(vBytes, b) + } + if err != nil { return nil, fmt.Errorf("extract value from sstable id %s (key %s): %w", id, key, err) } @@ -132,7 +138,13 @@ func (m *RangeManager) GetValue(ctx context.Context, ns committed.Namespace, id // lookup path in range but key not found return nil, ErrKeyNotFound } - vBytes, _, err := value.Value(nil) + vBytes, owned, err := value.Value(nil) + if !owned { + b := vBytes + vBytes = make([]byte, len(b)) + copy(vBytes, b) + } + if err != nil { return nil, fmt.Errorf("extract value from sstable id %s (key %s): %w", id, key, err) } From 17e20a2d94baa6ee9982b446feed3f335d689c8b Mon Sep 17 00:00:00 2001 From: Jonathan Rosenberg <96974219+Jonathan-Rosenberg@users.noreply.github.com> Date: Sun, 4 Jun 2023 12:58:02 +0300 Subject: [PATCH 2/2] stop copying value's bytes since the byte buffer is owned by the calling function --- pkg/graveler/committed/value.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/graveler/committed/value.go b/pkg/graveler/committed/value.go index 1944af05069..5f0822d6f53 100644 --- a/pkg/graveler/committed/value.go +++ b/pkg/graveler/committed/value.go @@ -74,8 +74,7 @@ func getBytes(b *[]byte) ([]byte, error) { if l < 0 { return nil, fmt.Errorf("impossible negative length %d: %w", l, ErrBadValueBytes) } - ret := make([]byte, l) - copy(ret, (*b)[:l]) + ret := (*b)[:l] *b = (*b)[l:] return ret, nil }