fix(recovery): cut ArgsSummary on a rune boundary

This commit is contained in:
kevin authored and GitHub committed 2026-11-01 22:04:09 +09:00
1 parent ee47753988
commit 498b834b71
2 files changed
+35 -1

No files matched your search

@@ -0,0 +1,29 @@
package recovery
import (
"encoding/json"
"strings"
"testing"
"unicode/utf8"
)
func TestArgsSummaryCutsOnRuneBoundary(t *testing.T) {
// 20 bytes into this command falls inside a three-byte character.
cmd := "echo 'a" + strings.Repeat("测试", 10) + "'"
args, _ := json.Marshal(map[string]string{"command": cmd})
got := ArgsSummary(args, 20)
if !utf8.ValidString(got) {
t.Fatalf("ArgsSummary split a character: %q", got)
}
kept := strings.TrimSuffix(got, "…")
if kept == got || !strings.HasPrefix(cmd, kept) || len(kept) > 20 || len(kept) < 18 {
t.Fatalf("ArgsSummary = %q, want a prefix of the command just under 20 bytes ending in …", got)
}
}
func TestArgsSummaryKeepsShortCommand(t *testing.T) {
args, _ := json.Marshal(map[string]string{"command": " go test ./... "})
if got := ArgsSummary(args, 20); got != "go test ./..." {
t.Fatalf("ArgsSummary = %q", got)
}
}
+6 -1
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"strings"
"unicode/utf8"
)
// CallFingerprint builds a one-shot authorization fingerprint from the
@@ -60,7 +61,11 @@ func ArgsSummary(args json.RawMessage, max int) string {
}
s = strings.Join(strings.Fields(s), " ")
if len(s) > max {
return s[:max] + "…"
cut := max
for cut > 0 && !utf8.RuneStart(s[cut]) {
cut--
}
return s[:cut] + "…"
}
return s
}