Skip to content

Commit

Permalink
Merge pull request #2 from mdwhatcott/master
Browse files Browse the repository at this point in the history
Ensure id is 5 hex characters (preventing out-of-range panic)
  • Loading branch information
mattes committed Mar 14, 2019
2 parents 79cf6b8 + aab6f34 commit 26c3a52
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions bot.go
Expand Up @@ -13,6 +13,7 @@ func init() {

var (
ErrIDLength = fmt.Errorf("id length must be 5")
ErrIDHex = fmt.Errorf("id must be 5 hexadecimal characters [0-9a-f]")
)

func Random() string {
Expand Down Expand Up @@ -41,6 +42,11 @@ func Generate(id string) (string, error) {
return "", ErrIDLength
}

id = strings.Map(hexOnly, id)
if len(id) != 5 {
return "", ErrIDHex
}

out := ""

// generate body
Expand All @@ -60,6 +66,17 @@ func Generate(id string) (string, error) {
return out, nil
}

func hexOnly(r rune) rune {
switch r {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return r
case 'a', 'b', 'c', 'd', 'e', 'f':
return r
default:
return -1
}
}

// split splits template into top, center and bottom parts
func split(template string) (top, center, bottom string) {
s := strings.Split(template, "\n")
Expand Down

0 comments on commit 26c3a52

Please sign in to comment.