You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
forgejo/modules/charset
Jason Song e253888a0e
Fix isAllowed of escapeStreamer (#22814)
The use of `sort.Search` is wrong: The slice should be sorted, and
`return >= 0` doen't mean it exists, see the
[manual](https://pkg.go.dev/sort#Search).

Could be fixed like this if we really need it:

```diff
diff --git a/modules/charset/escape_stream.go b/modules/charset/escape_stream.go
index 823b63513..fcf1ffbc1 100644
--- a/modules/charset/escape_stream.go
+++ b/modules/charset/escape_stream.go
@@ -20,6 +20,9 @@ import (
 var defaultWordRegexp = regexp.MustCompile(`(-?\d*\.\d\w*)|([^\` + "`" + `\~\!\@\#\$\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s\x00-\x1f]+)`)

 func NewEscapeStreamer(locale translation.Locale, next HTMLStreamer, allowed ...rune) HTMLStreamer {
+       sort.Slice(allowed, func(i, j int) bool {
+               return allowed[i] < allowed[j]
+       })
        return &escapeStreamer{
                escaped:                 &EscapeStatus{},
                PassthroughHTMLStreamer: *NewPassthroughStreamer(next),
@@ -284,14 +287,8 @@ func (e *escapeStreamer) runeTypes(runes ...rune) (types []runeType, confusables
 }

 func (e *escapeStreamer) isAllowed(r rune) bool {
-       if len(e.allowed) == 0 {
-               return false
-       }
-       if len(e.allowed) == 1 {
-               return e.allowed[0] == r
-       }
-
-       return sort.Search(len(e.allowed), func(i int) bool {
+       i := sort.Search(len(e.allowed), func(i int) bool {
                return e.allowed[i] >= r
-       }) >= 0
+       })
+       return i < len(e.allowed) && e.allowed[i] == r
 }
```

But I don't think so, a map is better to do it.
1 year ago
..
ambiguous Implement FSFE REUSE for golang files (#21840) 1 year ago
invisible Implement FSFE REUSE for golang files (#21840) 1 year ago
ambiguous.go Ensure that Chinese punctuation is not ambiguous when locale is Chinese (#22019) 1 year ago
ambiguous_gen.go Implement FSFE REUSE for golang files (#21840) 1 year ago
ambiguous_gen_test.go Implement FSFE REUSE for golang files (#21840) 1 year ago
breakwriter.go Implement FSFE REUSE for golang files (#21840) 1 year ago
breakwriter_test.go Implement FSFE REUSE for golang files (#21840) 1 year ago
charset.go Implement FSFE REUSE for golang files (#21840) 1 year ago
charset_test.go Implement FSFE REUSE for golang files (#21840) 1 year ago
escape.go Fix line spacing for plaintext previews (#22699) 1 year ago
escape_status.go Implement FSFE REUSE for golang files (#21840) 1 year ago
escape_stream.go Fix isAllowed of escapeStreamer (#22814) 1 year ago
escape_test.go Implement FSFE REUSE for golang files (#21840) 1 year ago
htmlstream.go Implement FSFE REUSE for golang files (#21840) 1 year ago
invisible_gen.go Implement FSFE REUSE for golang files (#21840) 1 year ago