diff options
author | Jamie Liu <jamieliu@google.com> | 2020-03-04 14:29:11 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-03-04 14:30:27 -0800 |
commit | a690b5762480ae80ef4264402fcfa8d84b57339a (patch) | |
tree | 9eb01d4119404ca48d96e458290e6b9af8d3e9bb /pkg | |
parent | 20a412ba44cd987a256f63419c3878ec6bbc9917 (diff) |
Ensure that safemem.BlockSeqOf(safemem.Block{}) produces an empty BlockSeq.
PiperOrigin-RevId: 298941855
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/safemem/seq_test.go | 21 | ||||
-rw-r--r-- | pkg/safemem/seq_unsafe.go | 3 |
2 files changed, 24 insertions, 0 deletions
diff --git a/pkg/safemem/seq_test.go b/pkg/safemem/seq_test.go index eba4bb535..de34005e9 100644 --- a/pkg/safemem/seq_test.go +++ b/pkg/safemem/seq_test.go @@ -20,6 +20,27 @@ import ( "testing" ) +func TestBlockSeqOfEmptyBlock(t *testing.T) { + bs := BlockSeqOf(Block{}) + if !bs.IsEmpty() { + t.Errorf("BlockSeqOf(Block{}).IsEmpty(): got false, wanted true; BlockSeq is %v", bs) + } +} + +func TestBlockSeqOfNonemptyBlock(t *testing.T) { + b := BlockFromSafeSlice(make([]byte, 1)) + bs := BlockSeqOf(b) + if bs.IsEmpty() { + t.Fatalf("BlockSeqOf(non-empty Block).IsEmpty(): got true, wanted false; BlockSeq is %v", bs) + } + if head := bs.Head(); head != b { + t.Fatalf("BlockSeqOf(non-empty Block).Head(): got %v, wanted %v", head, b) + } + if tail := bs.Tail(); !tail.IsEmpty() { + t.Fatalf("BlockSeqOf(non-empty Block).Tail().IsEmpty(): got false, wanted true: tail is %v", tail) + } +} + type blockSeqTest struct { desc string diff --git a/pkg/safemem/seq_unsafe.go b/pkg/safemem/seq_unsafe.go index dcdfc9600..f5f0574f8 100644 --- a/pkg/safemem/seq_unsafe.go +++ b/pkg/safemem/seq_unsafe.go @@ -56,6 +56,9 @@ type BlockSeq struct { // BlockSeqOf returns a BlockSeq representing the single Block b. func BlockSeqOf(b Block) BlockSeq { + if b.length == 0 { + return BlockSeq{} + } bs := BlockSeq{ data: b.start, length: -1, |