Skip to content

Commit

Permalink
Merge pull request #13 from rschio/master
Browse files Browse the repository at this point in the history
colorx: fix panic when ParseHexColor receives an empty string
  • Loading branch information
icza authored Sep 20, 2024
2 parents 5982a7a + 399513b commit b68404a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion imagex/colorx/colorx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var errInvalidFormat = errors.New("invalid format")
func ParseHexColor(s string) (c color.RGBA, err error) {
c.A = 0xff

if s[0] != '#' {
if len(s) == 0 || s[0] != '#' {
return c, errInvalidFormat
}

Expand Down
8 changes: 8 additions & 0 deletions imagex/colorx/colorx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package colorx

import (
"fmt"
"testing"
)

// ExampleParseHexColor shows how to use the ParseHexColor() function.
Expand Down Expand Up @@ -31,3 +32,10 @@ func ExampleParseHexColor() {
// #abcd = { 0 0 0 255}, invalid format
// #-12 = { 0 17 34 255}, invalid format
}

func TestParseHexColor(t *testing.T) {
_, err := ParseHexColor("")
if err == nil {
t.Errorf("empty string should return error")
}
}

0 comments on commit b68404a

Please sign in to comment.