Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Mar 14, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+17 −0
Diff settings

Always

Just for now

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

Fixes #1
  • Loading branch information...
mdwhatcott committed Mar 14, 2019
commit aab6f34bf52c50cd83ca21f31ce396d33b5f5591
Copy path View file
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")
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.