Skip to content
Permalink
Browse files

Ensure id is 5 hex characters (preventing out-of-range panic)

Fixes #1
  • Loading branch information...
mdwhatcott committed Mar 14, 2019
1 parent 79cf6b8 commit aab6f34bf52c50cd83ca21f31ce396d33b5f5591
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 aab6f34

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