mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-22 07:32:32 +01:00
fix: prevent server crash on unsupported Shift-JIS characters (#116)
UTF8ToSJIS panicked when encountering characters outside the Shift-JIS range (emoji, Lenny faces, cuneiform, etc.), crashing the server when such characters were sent via the Discord relay channel. Replace the panic with graceful filtering that drops unmappable runes and preserves valid content. Also fix ToNGWord index-out-of-range panic on empty encoder output. Closes #116
This commit is contained in:
@@ -15,7 +15,15 @@ func UTF8ToSJIS(x string) []byte {
|
||||
e := japanese.ShiftJIS.NewEncoder()
|
||||
xt, _, err := transform.String(e, x)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
// Filter out runes that can't be encoded to Shift-JIS instead of
|
||||
// crashing the server (see PR #116).
|
||||
var filtered []rune
|
||||
for _, r := range x {
|
||||
if _, _, err := transform.String(japanese.ShiftJIS.NewEncoder(), string(r)); err == nil {
|
||||
filtered = append(filtered, r)
|
||||
}
|
||||
}
|
||||
xt, _, _ = transform.String(japanese.ShiftJIS.NewEncoder(), string(filtered))
|
||||
}
|
||||
return []byte(xt)
|
||||
}
|
||||
@@ -36,9 +44,10 @@ func ToNGWord(x string) []uint16 {
|
||||
t := UTF8ToSJIS(string(r))
|
||||
if len(t) > 1 {
|
||||
w = append(w, uint16(t[1])<<8|uint16(t[0]))
|
||||
} else {
|
||||
} else if len(t) == 1 {
|
||||
w = append(w, uint16(t[0]))
|
||||
}
|
||||
// Skip runes that produced no SJIS output (unsupported characters)
|
||||
} else {
|
||||
w = append(w, uint16(r))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user