mirror of https://github.com/etcd-io/bbolt.git
Merge pull request #358 from ptabor/20221218-defensive-pageread
Validate page being fetched at possition 'p' self identifies as page 'p'pull/365/head
commit
d4831e6217
8
node.go
8
node.go
|
@ -188,12 +188,16 @@ func (n *node) read(p *page) {
|
|||
}
|
||||
|
||||
// write writes the items onto one or more pages.
|
||||
// The page should have p.id (might be 0 for meta or bucket-inline page) and p.overflow set
|
||||
// and the rest should be zeroed.
|
||||
func (n *node) write(p *page) {
|
||||
_assert(p.count == 0 && p.flags == 0, "node cannot be written into a not empty page")
|
||||
|
||||
// Initialize page.
|
||||
if n.isLeaf {
|
||||
p.flags |= leafPageFlag
|
||||
p.flags = leafPageFlag
|
||||
} else {
|
||||
p.flags |= branchPageFlag
|
||||
p.flags = branchPageFlag
|
||||
}
|
||||
|
||||
if len(n.inodes) >= 0xFFFF {
|
||||
|
|
10
page.go
10
page.go
|
@ -53,6 +53,16 @@ func (p *page) meta() *meta {
|
|||
return (*meta)(unsafeAdd(unsafe.Pointer(p), unsafe.Sizeof(*p)))
|
||||
}
|
||||
|
||||
func (p *page) fastCheck(id pgid) {
|
||||
_assert(p.id == id, "Page expected to be: %v, but self identifies as %v", id, p.id)
|
||||
// Only one flag of page-type can be set.
|
||||
_assert(p.flags == branchPageFlag ||
|
||||
p.flags == leafPageFlag ||
|
||||
p.flags == metaPageFlag ||
|
||||
p.flags == freelistPageFlag,
|
||||
"page %v: has unexpected type/flags: %x", p.id, p.flags)
|
||||
}
|
||||
|
||||
// leafPageElement retrieves the leaf node by index
|
||||
func (p *page) leafPageElement(index uint16) *leafPageElement {
|
||||
return (*leafPageElement)(unsafeIndex(unsafe.Pointer(p), unsafe.Sizeof(*p),
|
||||
|
|
5
tx.go
5
tx.go
|
@ -609,12 +609,15 @@ func (tx *Tx) page(id pgid) *page {
|
|||
// Check the dirty pages first.
|
||||
if tx.pages != nil {
|
||||
if p, ok := tx.pages[id]; ok {
|
||||
p.fastCheck(id)
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise return directly from the mmap.
|
||||
return tx.db.page(id)
|
||||
p := tx.db.page(id)
|
||||
p.fastCheck(id)
|
||||
return p
|
||||
}
|
||||
|
||||
// forEachPage iterates over every page within a given page and executes a function.
|
||||
|
|
Loading…
Reference in New Issue