Skip to content

Commit 7994bb7

Browse files
Handle corner cases of lengths in Generator.book
In particular, if minlength is given in the template and maxlength is taken from the limit file, maxlength may be less than minlength. In that case we now make maxlength equal to minlength. Also, the argument of IntN must be maxlength-minlength+1 so that maxlength is actually a value that might be generated. This will also handle the case of maxlength == minlength correctly and without a panic.
1 parent 95b5f03 commit 7994bb7

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

pkg/fakedoc/generator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,11 @@ func (gen *Generator) book(minlength, maxlength int, path string, limits *LimitN
607607
maxlength = int(float64(maxlength) * gen.SizeFactor)
608608
}
609609
}
610+
if maxlength < minlength {
611+
maxlength = minlength
612+
}
610613

611-
length := minlength + gen.Rand.IntN(maxlength-minlength)
614+
length := minlength + gen.Rand.IntN(maxlength-minlength+1)
612615
content, ok := gen.FileCache[path]
613616
if !ok {
614617
file, err := os.Open(path)

0 commit comments

Comments
 (0)