Skip to content

Commit

Permalink
Remove hz-gb-2312 encoding as it can crash
Browse files Browse the repository at this point in the history
Upstream go's x/text/encoding package has a bug that makes the
'hz-gb-2312' encoding crash (golang/go#35118)

This was reported to go-message in
#95

This commit removes the offending encoding so that go-message can't crash if
people import the charset package. It should be reverted once the upstream
package is fixed by the go developers.
  • Loading branch information
brunnre8 authored and emersion committed May 25, 2020
1 parent 0e60ea0 commit 5b97b1b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 5 additions & 3 deletions charset/charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ var charsets = map[string]encoding.Encoding{

"ansi_x3.110-1983": charmap.ISO8859_1, // see RFC 1345 page 62, mostly superset of ISO 8859-1
"gb2312": simplifiedchinese.GBK, // GBK is a superset of HZGB2312
"cp1250": charmap.Windows1250,
"cp1251": charmap.Windows1251,
"cp1252": charmap.Windows1252,
// disabled due to https://github.com/emersion/go-message/issues/95
"hz-gb-2312": nil,
"cp1250": charmap.Windows1250,
"cp1251": charmap.Windows1251,
"cp1252": charmap.Windows1252,
}

func init() {
Expand Down
13 changes: 13 additions & 0 deletions charset/charset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package charset
import (
"bytes"
"io/ioutil"
"strings"
"testing"
)

Expand Down Expand Up @@ -71,3 +72,15 @@ func TestCharsetReader(t *testing.T) {
}
}
}

func TestDisabledCharsetReader(t *testing.T) {
_, err := Reader("hz-gb-2312", strings.NewReader("Some dummy text"))
if err == nil {
t.Errorf("%v encoding is disabled and should give an error", "hz-gb-2312")
return
}
if !strings.HasSuffix(err.Error(), "unsupported charset") {
t.Errorf("expected error to end in 'unsupported charset', got %v",
err.Error())
}
}

0 comments on commit 5b97b1b

Please sign in to comment.