fix: log SJIS decoding errors instead of silently discarding them

Add SJISToUTF8Lossy() that wraps SJISToUTF8() and logs decode errors at
slog.Debug level. Replace all 31 call sites across 17 files that previously
discarded the error with `_, _ =`. This makes garbled text from malformed
SJIS client data debuggable without adding noise at default log levels.
This commit is contained in:
Houmgaor
2026-02-22 17:01:22 +01:00
parent 59fd722d37
commit f640cfee27
20 changed files with 63 additions and 43 deletions

View File

@@ -461,6 +461,25 @@ func BenchmarkCSVElems(b *testing.B) {
}
}
func TestSJISToUTF8Lossy(t *testing.T) {
// Valid SJIS (ASCII subset) decodes correctly.
got := SJISToUTF8Lossy([]byte("Hello"))
if got != "Hello" {
t.Errorf("SJISToUTF8Lossy(valid) = %q, want %q", got, "Hello")
}
// Truncated multi-byte SJIS sequence (lead byte 0x82 without trail byte)
// does not panic and returns some result (lossy).
got = SJISToUTF8Lossy([]byte{0x82})
_ = got // must not panic
// Nil input returns empty string.
got = SJISToUTF8Lossy(nil)
if got != "" {
t.Errorf("SJISToUTF8Lossy(nil) = %q, want %q", got, "")
}
}
func TestUTF8ToSJIS_UnsupportedCharacters(t *testing.T) {
// Regression test for PR #116: Characters outside the Shift-JIS range
// (e.g. Lenny face, cuneiform) previously caused a panic in UTF8ToSJIS,