Skip to content

Commit b68404a

Browse files
authored
Merge pull request #13 from rschio/master
colorx: fix panic when ParseHexColor receives an empty string
2 parents 5982a7a + 399513b commit b68404a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

imagex/colorx/colorx.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var errInvalidFormat = errors.New("invalid format")
1414
func ParseHexColor(s string) (c color.RGBA, err error) {
1515
c.A = 0xff
1616

17-
if s[0] != '#' {
17+
if len(s) == 0 || s[0] != '#' {
1818
return c, errInvalidFormat
1919
}
2020

imagex/colorx/colorx_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package colorx
22

33
import (
44
"fmt"
5+
"testing"
56
)
67

78
// ExampleParseHexColor shows how to use the ParseHexColor() function.
@@ -31,3 +32,10 @@ func ExampleParseHexColor() {
3132
// #abcd = { 0 0 0 255}, invalid format
3233
// #-12 = { 0 17 34 255}, invalid format
3334
}
35+
36+
func TestParseHexColor(t *testing.T) {
37+
_, err := ParseHexColor("")
38+
if err == nil {
39+
t.Errorf("empty string should return error")
40+
}
41+
}

0 commit comments

Comments
 (0)