From 8374d6adc5c51ae188af61302cc5d4f87cb02adf Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Mon, 12 Jan 2015 08:06:42 -0700 Subject: [PATCH] Add check for max mmap size. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The max mmap size was previous unchecked which resulted in a panic once the maximum size was reached. This commit adds a check for the max size when re-mapping and returns an error if the new map will exceed the size. Thanks to Tamás Gulácsi for testing out the change on i386. --- db.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/db.go b/db.go index 6c45736..ad27176 100644 --- a/db.go +++ b/db.go @@ -162,16 +162,6 @@ func (db *DB) mmap(minsz int) error { db.mmaplock.Lock() defer db.mmaplock.Unlock() - // Dereference all mmap references before unmapping. - if db.rwtx != nil { - db.rwtx.root.dereference() - } - - // Unmap existing data before continuing. - if err := db.munmap(); err != nil { - return err - } - info, err := db.file.Stat() if err != nil { return fmt.Errorf("mmap stat error: %s", err) @@ -186,6 +176,21 @@ func (db *DB) mmap(minsz int) error { } size = db.mmapSize(size) + // Verify the map size is not above the maximum allowed. + if size > maxMapSize { + return fmt.Errorf("mmap too large") + } + + // Dereference all mmap references before unmapping. + if db.rwtx != nil { + db.rwtx.root.dereference() + } + + // Unmap existing data before continuing. + if err := db.munmap(); err != nil { + return err + } + // Memory-map the data file as a byte slice. if err := mmap(db, size); err != nil { return err