Skip to content

Commit

Permalink
fixup! Fix go-gitea#16252 - Equivalent to go-gitea#16268
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Jun 27, 2021
1 parent 339a74b commit 88d9546
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
15 changes: 15 additions & 0 deletions models/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

package models

import (
jsoniter "github.com/json-iterator/go"
)

func keysInt64(m map[int64]struct{}) []int64 {
keys := make([]int64, 0, len(m))
for k := range m {
Expand All @@ -27,3 +31,14 @@ func valuesUser(m map[int64]*User) []*User {
}
return values
}

// JSONUnmarshalIgnoreErroneousBOM - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
// possible that a Blob may gain an unwanted prefix of 0xff 0xfe.
func JSONUnmarshalIgnoreErroneousBOM(bs []byte, v interface{}) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(bs, &v)
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
err = json.Unmarshal(bs[2:], &v)
}
return err
}
12 changes: 0 additions & 12 deletions models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
jsoniter "github.com/json-iterator/go"

"xorm.io/xorm"
"xorm.io/xorm/convert"
Expand Down Expand Up @@ -127,17 +126,6 @@ func Cell2Int64(val xorm.Cell) int64 {
return (*val).(int64)
}

// JsonUnmarshalIgnoreErroneousBOM - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
// possible that a Blob may gain an unwanted prefix of 0xff 0xfe.
func JsonUnmarshalIgnoreErroneousBOM(bs []byte, v interface{}) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(bs, &v)
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
err = json.Unmarshal(bs[2:], &v)
}
return err
}

// BeforeSet is invoked from XORM before setting the value of a field of this object.
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
if colName == "type" {
Expand Down
10 changes: 5 additions & 5 deletions models/repo_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type UnitConfig struct{}

// FromDB fills up a UnitConfig from serialized format.
func (cfg *UnitConfig) FromDB(bs []byte) error {
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return JSONUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

// ToDB exports a UnitConfig to a serialized format.
Expand All @@ -44,7 +44,7 @@ type ExternalWikiConfig struct {

// FromDB fills up a ExternalWikiConfig from serialized format.
func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return JSONUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

// ToDB exports a ExternalWikiConfig to a serialized format.
Expand All @@ -62,7 +62,7 @@ type ExternalTrackerConfig struct {

// FromDB fills up a ExternalTrackerConfig from serialized format.
func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return JSONUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

// ToDB exports a ExternalTrackerConfig to a serialized format.
Expand All @@ -80,7 +80,7 @@ type IssuesConfig struct {

// FromDB fills up a IssuesConfig from serialized format.
func (cfg *IssuesConfig) FromDB(bs []byte) error {
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return JSONUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

// ToDB exports a IssuesConfig to a serialized format.
Expand All @@ -103,7 +103,7 @@ type PullRequestsConfig struct {

// FromDB fills up a PullRequestsConfig from serialized format.
func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
return JsonUnmarshalIgnoreErroneousBOM(bs, &cfg)
return JSONUnmarshalIgnoreErroneousBOM(bs, &cfg)
}

// ToDB exports a PullRequestsConfig to a serialized format.
Expand Down
2 changes: 1 addition & 1 deletion services/auth/source/ldap/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (source *Source) FromDB(bs []byte) error {
Source: source,
}

err := models.JsonUnmarshalIgnoreErroneousBOM(bs, &wrapped)
err := models.JSONUnmarshalIgnoreErroneousBOM(bs, &wrapped)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion services/auth/source/oauth2/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Source struct {

// FromDB fills up an OAuth2Config from serialized format.
func (source *Source) FromDB(bs []byte) error {
return models.JsonUnmarshalIgnoreErroneousBOM(bs, source)
return models.JSONUnmarshalIgnoreErroneousBOM(bs, source)
}

// ToDB exports an SMTPConfig to a serialized format.
Expand Down
2 changes: 1 addition & 1 deletion services/auth/source/pam/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Source struct {

// FromDB fills up a PAMConfig from serialized format.
func (source *Source) FromDB(bs []byte) error {
return models.JsonUnmarshalIgnoreErroneousBOM(bs, &source)
return models.JSONUnmarshalIgnoreErroneousBOM(bs, &source)
}

// ToDB exports a PAMConfig to a serialized format.
Expand Down
2 changes: 1 addition & 1 deletion services/auth/source/smtp/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Source struct {

// FromDB fills up an SMTPConfig from serialized format.
func (source *Source) FromDB(bs []byte) error {
return models.JsonUnmarshalIgnoreErroneousBOM(bs, source)
return models.JSONUnmarshalIgnoreErroneousBOM(bs, source)
}

// ToDB exports an SMTPConfig to a serialized format.
Expand Down
2 changes: 1 addition & 1 deletion services/auth/source/sspi/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Source struct {

// FromDB fills up an SSPIConfig from serialized format.
func (cfg *Source) FromDB(bs []byte) error {
return models.JsonUnmarshalIgnoreErroneousBOM(bs, cfg)
return models.JSONUnmarshalIgnoreErroneousBOM(bs, cfg)
}

// ToDB exports an SSPIConfig to a serialized format.
Expand Down

0 comments on commit 88d9546

Please sign in to comment.