Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use UnsafeStringToBytes #31358

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions modules/markup/markdown/prefixed_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/markup/common"
"code.gitea.io/gitea/modules/util"

"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/util"
)

type prefixedIDs struct {
Expand All @@ -36,7 +36,7 @@ func (p *prefixedIDs) GenerateWithDefault(value, dft []byte) []byte {
if !bytes.HasPrefix(result, []byte("user-content-")) {
result = append([]byte("user-content-"), result...)
}
if p.values.Add(util.BytesToReadOnlyString(result)) {
if p.values.Add(util.UnsafeBytesToString(result)) {
return result
}
for i := 1; ; i++ {
Expand All @@ -49,7 +49,7 @@ func (p *prefixedIDs) GenerateWithDefault(value, dft []byte) []byte {

// Put puts a given element id to the used ids table.
func (p *prefixedIDs) Put(value []byte) {
p.values.Add(util.BytesToReadOnlyString(value))
p.values.Add(util.UnsafeBytesToString(value))
}

func newPrefixedIDs() *prefixedIDs {
Expand Down
6 changes: 3 additions & 3 deletions modules/markup/markdown/transform_heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"fmt"

"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/util"

"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)

func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Heading, reader text.Reader, tocList *[]markup.Header) {
Expand All @@ -21,11 +21,11 @@ func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Headin
}
txt := v.Text(reader.Source())
header := markup.Header{
Text: util.BytesToReadOnlyString(txt),
Text: util.UnsafeBytesToString(txt),
Level: v.Level,
}
if id, found := v.AttributeString("id"); found {
header.ID = util.BytesToReadOnlyString(id.([]byte))
header.ID = util.UnsafeBytesToString(id.([]byte))
}
*tocList = append(*tocList, header)
g.applyElementDir(v)
Expand Down
5 changes: 2 additions & 3 deletions modules/references/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup/mdstripper"
"code.gitea.io/gitea/modules/setting"

"github.com/yuin/goldmark/util"
"code.gitea.io/gitea/modules/util"
)

var (
Expand Down Expand Up @@ -341,7 +340,7 @@ func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool
return false, nil
}
}
r := getCrossReference(util.StringToReadOnlyBytes(content), match[2], match[3], false, prOnly)
r := getCrossReference(util.UnsafeStringToBytes(content), match[2], match[3], false, prOnly)
if r == nil {
return false, nil
}
Expand Down
7 changes: 3 additions & 4 deletions modules/system/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (

"code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/modules/json"

"github.com/yuin/goldmark/util"
"code.gitea.io/gitea/modules/util"
)

// DBStore can be used to store app state items in local filesystem
Expand All @@ -24,7 +23,7 @@ func (f *DBStore) Get(ctx context.Context, item StateItem) error {
if content == "" {
return nil
}
return json.Unmarshal(util.StringToReadOnlyBytes(content), item)
return json.Unmarshal(util.UnsafeStringToBytes(content), item)
}

// Set saves the state item
Expand All @@ -33,5 +32,5 @@ func (f *DBStore) Set(ctx context.Context, item StateItem) error {
if err != nil {
return err
}
return system.SaveAppStateContent(ctx, item.Name(), util.BytesToReadOnlyString(b))
return system.SaveAppStateContent(ctx, item.Name(), util.UnsafeBytesToString(b))
}
6 changes: 2 additions & 4 deletions modules/util/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package util
import (
"bytes"
"unicode"

"github.com/yuin/goldmark/util"
)

type sanitizedError struct {
Expand All @@ -33,7 +31,7 @@ var schemeSep = []byte("://")

// SanitizeCredentialURLs remove all credentials in URLs (starting with "scheme://") for the input string: "https://user:[email protected]" => "https://[email protected]"
func SanitizeCredentialURLs(s string) string {
bs := util.StringToReadOnlyBytes(s)
bs := UnsafeStringToBytes(s)
schemeSepPos := bytes.Index(bs, schemeSep)
if schemeSepPos == -1 || bytes.IndexByte(bs[schemeSepPos:], '@') == -1 {
return s // fast return if there is no URL scheme or no userinfo
Expand Down Expand Up @@ -70,5 +68,5 @@ func SanitizeCredentialURLs(s string) string {
schemeSepPos = bytes.Index(bs, schemeSep)
}
out = append(out, bs...)
return util.BytesToReadOnlyString(out)
return UnsafeBytesToString(out)
}
2 changes: 1 addition & 1 deletion modules/util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ func ToSnakeCase(input string) string {
}

// UnsafeBytesToString uses Go's unsafe package to convert a byte slice to a string.
// TODO: replace all "goldmark/util.BytesToReadOnlyString" with this official approach
func UnsafeBytesToString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}

// UnsafeStringToBytes uses Go's unsafe package to convert a string to a byte slice.
func UnsafeStringToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}