codeberg-forgejo/modules/charset
Jason Song 77c89572e9
Fix isAllowed of escapeStreamer (#22814) (#22837)
Backport #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.
2023-02-10 11:36:58 +08:00
..
ambiguous Move go-licenses to generate and separate generate into a frontend and backend component (#21061) 2022-09-05 14:04:18 +08:00
invisible Move go-licenses to generate and separate generate into a frontend and backend component (#21061) 2022-09-05 14:04:18 +08:00
ambiguous.go Ensure that Chinese punctuation is not ambiguous when locale is Chinese (#22019) (#22030) 2022-12-05 17:20:38 +08:00
ambiguous_gen.go Switch Unicode Escaping to a VSCode-like system (#19990) 2022-08-13 19:32:34 +01:00
ambiguous_gen_test.go Switch Unicode Escaping to a VSCode-like system (#19990) 2022-08-13 19:32:34 +01:00
breakwriter.go Switch Unicode Escaping to a VSCode-like system (#19990) 2022-08-13 19:32:34 +01:00
breakwriter_test.go Switch Unicode Escaping to a VSCode-like system (#19990) 2022-08-13 19:32:34 +01:00
charset.go Fix various typos (#20338) 2022-07-12 23:32:37 +02:00
charset_test.go Add more linters to improve code readability (#19989) 2022-06-20 12:02:49 +02:00
escape.go Fix line spacing for plaintext previews (#22699) (#22701) 2023-02-01 22:06:58 +00:00
escape_status.go Switch Unicode Escaping to a VSCode-like system (#19990) 2022-08-13 19:32:34 +01:00
escape_stream.go Fix isAllowed of escapeStreamer (#22814) (#22837) 2023-02-10 11:36:58 +08:00
escape_test.go Share HTML template renderers and create a watcher framework (#20218) 2022-08-28 10:43:25 +01:00
htmlstream.go Switch Unicode Escaping to a VSCode-like system (#19990) 2022-08-13 19:32:34 +01:00
invisible_gen.go Switch Unicode Escaping to a VSCode-like system (#19990) 2022-08-13 19:32:34 +01:00