Skip to content

Commit

Permalink
make cycles fix in recurse func more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlehane committed Oct 13, 2019
1 parent 9ab3773 commit 722de43
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,26 @@ func fixName(f *File) {

func (r *Reader) traverse() error {
r.File = make([]*File, 0, len(r.direntries))
var recurse func(int, []string)
var err error
var (
recurse func(int, []string)
err error
counter int
)
recurse = func(i int, path []string) {
// prevent cycles, number of recurse calls can't exceed number of directory entries
counter++
if counter > len(r.direntries) {
err = Error{ErrTraverse, "traversal counter overflow", int64(i)}
return
}
if i < 0 || i >= len(r.direntries) {
err = Error{ErrTraverse, "illegal traversal index", int64(i)}
return
}
file := r.direntries[i]
if file.leftSibID != noStream && int(file.leftSibID) != i {
if file.leftSibID != noStream {
recurse(int(file.leftSibID), path)
}
if len(r.File) >= cap(r.File) {
err = Error{ErrTraverse, "traversal counter overflow", int64(i)}
return
}
r.File = append(r.File, file)
file.Path = path
if file.childID != noStream {
Expand Down

0 comments on commit 722de43

Please sign in to comment.