Improved color validation - now with fixes and a table driven test

This commit is contained in:
ErikEk
2018-11-04 03:00:19 +01:00
parent b600985063
commit f36c58acd7
2 changed files with 39 additions and 25 deletions

View File

@@ -10,6 +10,7 @@ import (
"math/big"
"net"
"path/filepath"
"regexp"
"strconv"
"sync"
"sync/atomic"
@@ -70,6 +71,10 @@ var (
// ErrServerShuttingDown indicates that the server is in the process of
// gracefully exiting.
ErrServerShuttingDown = errors.New("server is shutting down")
// validColorRegexp is a regexp that lets you check if a particular
// color string matches the standard hex color format #RRGGBB.
validColorRegexp = regexp.MustCompile("^#[A-Fa-f0-9]{6}$")
)
// server is the main server of the Lightning Network Daemon. The server houses
@@ -2821,8 +2826,10 @@ func (s *server) Peers() []*peer {
// form "#RRGGBB", parses the hex color values, and returns a color.RGBA
// struct of the same color.
func parseHexColor(colorStr string) (color.RGBA, error) {
if len(colorStr) != 7 || colorStr[0] != '#' {
return color.RGBA{}, errors.New("Color must be in format #RRGGBB")
// Check if the hex color string is a valid color representation.
if !validColorRegexp.MatchString(colorStr) {
return color.RGBA{}, errors.New("Color must be specified " +
"using a hexadecimal value in the form #RRGGBB")
}
// Decode the hex color string to bytes.