Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,13 @@ func (cmd *pageItemCommand) Run(args ...string) error {
}

if !options.valueOnly {
err := cmd.PrintLeafItemKey(cmd.Stdout, buf, uint16(itemID), options.format)
err := cmd.PrintLeafItemKey(cmd.Stdout, buf, itemID, options.format)
if err != nil {
return err
}
}
if !options.keyOnly {
err := cmd.PrintLeafItemValue(cmd.Stdout, buf, uint16(itemID), options.format)
err := cmd.PrintLeafItemValue(cmd.Stdout, buf, itemID, options.format)
if err != nil {
return err
}
Expand All @@ -518,15 +518,15 @@ func (cmd *pageItemCommand) Run(args ...string) error {
}

// leafPageElement retrieves a leaf page element.
func (cmd *pageItemCommand) leafPageElement(pageBytes []byte, index uint16) (*guts_cli.LeafPageElement, error) {
func (cmd *pageItemCommand) leafPageElement(pageBytes []byte, index uint64) (*guts_cli.LeafPageElement, error) {
p := (*guts_cli.Page)(unsafe.Pointer(&pageBytes[0]))
if index >= p.Count() {
if index >= uint64(p.Count()) {
return nil, fmt.Errorf("leafPageElement: expected item index less than %d, but got %d.", p.Count(), index)
}
if p.Type() != "leaf" {
return nil, fmt.Errorf("leafPageElement: expected page type of 'leaf', but got '%s'", p.Type())
}
return p.LeafPageElement(index), nil
return p.LeafPageElement(uint16(index)), nil
}

const FORMAT_MODES = "auto|ascii-encoded|hex|bytes|redacted"
Expand Down Expand Up @@ -573,7 +573,7 @@ func writelnBytes(w io.Writer, b []byte, format string) error {
}

// PrintLeafItemKey writes the bytes of a leaf element's key.
func (cmd *pageItemCommand) PrintLeafItemKey(w io.Writer, pageBytes []byte, index uint16, format string) error {
func (cmd *pageItemCommand) PrintLeafItemKey(w io.Writer, pageBytes []byte, index uint64, format string) error {
e, err := cmd.leafPageElement(pageBytes, index)
if err != nil {
return err
Expand All @@ -582,7 +582,7 @@ func (cmd *pageItemCommand) PrintLeafItemKey(w io.Writer, pageBytes []byte, inde
}

// PrintLeafItemKey writes the bytes of a leaf element's value.
func (cmd *pageItemCommand) PrintLeafItemValue(w io.Writer, pageBytes []byte, index uint16, format string) error {
func (cmd *pageItemCommand) PrintLeafItemValue(w io.Writer, pageBytes []byte, index uint64, format string) error {
e, err := cmd.leafPageElement(pageBytes, index)
if err != nil {
return err
Expand Down
54 changes: 54 additions & 0 deletions cmd/bbolt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"go.etcd.io/bbolt/internal/btesting"
"go.etcd.io/bbolt/internal/guts_cli"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -286,6 +287,59 @@ func TestPagesCommand_Run(t *testing.T) {
require.NoError(t, err)
}

func TestPageItemCommand_ItemIDOutOfRange(t *testing.T) {
db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})
srcPath := db.Path()

err := db.Update(func(tx *bolt.Tx) error {
b, bErr := tx.CreateBucketIfNotExists([]byte("data"))
if bErr != nil {
return bErr
}
for i := 0; i < 100; i++ {
if bErr = b.Put([]byte(fmt.Sprintf("key_%d", i)), []byte(fmt.Sprintf("value_%d", i))); bErr != nil {
return bErr
}
}
return nil
})
require.NoError(t, err)
db.Close()

defer requireDBNoChange(t, dbData(t, srcPath), srcPath)

_, hwm, err := guts_cli.ReadPageAndHWMSize(srcPath)
require.NoError(t, err)
leafPageId := 0
for i := 2; i < int(hwm); i++ {
p, _, err := guts_cli.ReadPage(srcPath, uint64(i))
require.NoError(t, err)
if p.Type() == "leaf" && p.Count() > 1 {
leafPageId = int(p.Id())
}
}
require.NotEqual(t, 0, leafPageId)

testCases := []struct {
name string
itemId string
}{
// Congruent to the valid index 0 modulo 2^16.
{name: "wraps onto a valid index", itemId: "65536"},
// Congruent to the out of range index 100 modulo 2^16, so the
// id reported back to the user has to be the one that was typed.
{name: "wraps onto an out of range index", itemId: "65636"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
m := NewMain()
err := m.Run("page-item", srcPath, fmt.Sprintf("%d", leafPageId), tc.itemId)
require.ErrorContains(t, err, fmt.Sprintf("expected item index less than 100, but got %s", tc.itemId))
})
}
}

// Main represents a test wrapper for main.Main that records output.
type Main struct {
*main.Main
Expand Down
Loading