From ccffe0011070d9246975fa627d3f0f5f2b518ba0 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Sun, 19 Jul 2020 18:47:01 -0700 Subject: [PATCH] Fix off by one error in GenUniqueItems --- internal/helpers/helpers.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/internal/helpers/helpers.go b/internal/helpers/helpers.go index b9acfb2..b707478 100644 --- a/internal/helpers/helpers.go +++ b/internal/helpers/helpers.go @@ -18,18 +18,24 @@ func GenUniqueItems(total int, fn genFunc, seed int64) []string { items := []string{} // Generate initial items - for i := 0; i <= total; i++ { + for i := 0; i < total; i++ { items = append(items, fn(roller.TableRoll())) } // Walk items and check if they match any other item. If they do then replace // it with a fresh item. This doesn't guarantee unique items, but prevents - // Denial of Service attacks (a bit) - for i, item := range items { - for x := 0; x < len(items); x++ { - if items[x] == item { - items[i] = fn(roller.TableRoll()) - break + // Denial of Service attacks (a bit) by only doing 5 passes. + passes := 5 + for p := 0; p <= passes; p++ { + for i, _ := range items { + for x := 0; x < len(items); x++ { + if x == i { + break + } + if items[x] == items[i] { + items[i] = fn(roller.TableRoll()) + break + } } } }