Skip to content
Permalink
Browse files

Merge pull request #2 from mdwhatcott/master

Ensure id is 5 hex characters (preventing out-of-range panic)
  • Loading branch information...
mattes committed Mar 14, 2019
2 parents 79cf6b8 + aab6f34 commit 26c3a522dfb93f23e523dd63e98133faa62868ec
Showing with 17 additions and 0 deletions.
  1. +17 −0 bot.go
17 bot.go
@@ -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 {
@@ -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
@@ -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")

0 comments on commit 26c3a52

Please sign in to comment.
You can’t perform that action at this time.