Handle invalid versions better

This commit is contained in:
Alex Yatskov 2015-06-23 18:27:16 +09:00
parent d83c654219
commit f5d16cc423
2 changed files with 8 additions and 3 deletions

View File

@ -23,6 +23,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path" "path"
@ -41,7 +42,7 @@ type database struct {
vers verList vers verList
} }
func newDatabase(dir string, index int, writable bool) (*database, error) { func newDatabase(dir string, index uint, writable bool) (*database, error) {
db := &database{base: dir} db := &database{base: dir}
if err := db.load(dir, index, writable); err != nil { if err := db.load(dir, index, writable); err != nil {
return nil, err return nil, err
@ -50,7 +51,7 @@ func newDatabase(dir string, index int, writable bool) (*database, error) {
return db, nil return db, nil
} }
func (db *database) load(dir string, index int, writable bool) error { func (db *database) load(dir string, index uint, writable bool) error {
var err error var err error
db.base, err = filepath.Abs(dir) db.base, err = filepath.Abs(dir)
@ -69,6 +70,10 @@ func (db *database) load(dir string, index int, writable bool) error {
return err return err
} }
if index > uint(len(db.vers)) {
return errors.New("invalid version index")
}
if index > 0 { if index > 0 {
db.vers = db.vers[:index] db.vers = db.vers[:index]
} }

2
vfs.go
View File

@ -40,7 +40,7 @@ func usage() {
} }
func main() { func main() {
version := flag.Int("version", 0, "version index (specify 0 for latest)") version := flag.Uint("version", 0, "version index (0 for head)")
readonly := flag.Bool("readonly", false, "mount filesystem as readonly") readonly := flag.Bool("readonly", false, "mount filesystem as readonly")
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()