♻️ refactor: check and handle previously ignored error returns

This commit is contained in:
2026-07-13 23:01:48 +07:00
parent a293873296
commit fed48989de
6 changed files with 48 additions and 31 deletions
+19 -6
View File
@@ -40,27 +40,40 @@ func TestTSFInvalid(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "bad.tsf")
os.WriteFile(path, []byte("not a tsf file"), 0o644)
if err := os.WriteFile(path, []byte("not a tsf file"), 0o644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
if _, err := loadTSF(path); err == nil {
t.Error("expected error for garbage input")
}
// Valid container but no frames.
writeTSF(path, &FramesContainer{FPS: 30})
if err := writeTSF(path, &FramesContainer{FPS: 30}); err != nil {
t.Fatalf("writeTSF: %v", err)
}
if _, err := loadTSF(path); err == nil {
t.Error("expected error for empty frames")
}
// Valid container but fps <= 0.
writeTSF(path, &FramesContainer{ColorFrames: [][]byte{{1}}, FPS: 0})
if err := writeTSF(path, &FramesContainer{ColorFrames: [][]byte{{1}}, FPS: 0}); err != nil {
t.Fatalf("writeTSF: %v", err)
}
if _, err := loadTSF(path); err == nil {
t.Error("expected error for fps<=0")
}
// Truncated payload.
writeTSF(path, &FramesContainer{ColorFrames: [][]byte{{1, 2, 3, 4}}, FPS: 30})
raw, _ := os.ReadFile(path)
os.WriteFile(path, raw[:len(raw)-2], 0o644)
if err := writeTSF(path, &FramesContainer{ColorFrames: [][]byte{{1, 2, 3, 4}}, FPS: 30}); err != nil {
t.Fatalf("writeTSF: %v", err)
}
raw, err := os.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
if err := os.WriteFile(path, raw[:len(raw)-2], 0o644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
if _, err := loadTSF(path); err == nil {
t.Error("expected error for truncated file")
}
+1 -1
View File
@@ -20,7 +20,7 @@ func writeTSF(output string, data *FramesContainer) error {
if err != nil {
return err
}
defer f.Close()
defer func() { _ = f.Close() }()
w := bufio.NewWriterSize(f, 1<<20)
if _, err := w.WriteString(tsfMagic); err != nil {
+1 -1
View File
@@ -76,7 +76,7 @@ func emit(level logLevel, name string, stream *os.File, args []any) {
}
}
ts := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
fmt.Fprintf(stream, "[%s] %-5s %s\n", ts, strings.ToUpper(name), strings.Join(parts, " "))
_, _ = fmt.Fprintf(stream, "[%s] %-5s %s\n", ts, strings.ToUpper(name), strings.Join(parts, " "))
}
func logDebug(args ...any) { emit(levelDebug, "debug", os.Stdout, args) }
+7 -3
View File
@@ -61,7 +61,9 @@ func generateFrames(config Config, framesDir, videoArg string) {
fail(fmt.Sprintf("Source video %q does not exist or is not a file.", videoArg))
}
os.MkdirAll(framesDir, 0o755)
if err := os.MkdirAll(framesDir, 0o755); err != nil {
fail(fmt.Sprintf("Failed to create frames directory %q: %s", framesDir, err.Error()))
}
base := strings.TrimSuffix(filepath.Base(videoPath), filepath.Ext(videoPath))
output := filepath.Join(framesDir, base+".tsf")
@@ -142,7 +144,7 @@ func loadAllFrames(framesDir string) []*FramesContainer {
}
func main() {
godotenv.Load()
_ = godotenv.Load()
logThreshold = resolveThreshold()
config := loadConfig()
@@ -160,7 +162,9 @@ func main() {
return
}
os.MkdirAll(dataDir, 0o755)
if err := os.MkdirAll(dataDir, 0o755); err != nil {
fail(fmt.Sprintf("Failed to create data directory %q: %s", dataDir, err.Error()))
}
var bannerText, fakeLoginText, goodbyeText *string
if text, ok := loadOptionalTextFile(filepath.Join(dataDir, "banner.txt")); ok {
+19 -19
View File
@@ -183,7 +183,7 @@ func (s *Server) Listen(host string, port int) error {
func (s *Server) Close() {
s.closeOnce.Do(func() {
if s.listener != nil {
s.listener.Close()
_ = s.listener.Close()
}
})
}
@@ -192,7 +192,7 @@ func (s *Server) handleConn(conn net.Conn) {
ip := hostOnly(conn.RemoteAddr().String())
if s.tracker.hasReachedLimits(ip, s.config.MaxConnections, s.config.MaxTotalConnections) {
conn.Close()
_ = conn.Close()
logWarn("Connection rejected (limit reached) from", ip)
return
}
@@ -201,7 +201,7 @@ func (s *Server) handleConn(conn net.Conn) {
defer s.tracker.decrement(ip)
if s.config.HandshakeTimeout > 0 {
conn.SetDeadline(time.Now().Add(s.config.HandshakeTimeout))
_ = conn.SetDeadline(time.Now().Add(s.config.HandshakeTimeout))
}
sshConn, chans, reqs, err := ssh.NewServerConn(conn, s.sshConfig)
@@ -211,12 +211,12 @@ func (s *Server) handleConn(conn net.Conn) {
} else {
logWarn(fmt.Sprintf("Client error from %s:", ip), sanitize(err.Error()))
}
conn.Close()
_ = conn.Close()
return
}
conn.SetDeadline(time.Time{})
_ = conn.SetDeadline(time.Time{})
logDebug("Handshake from", ip)
defer sshConn.Close()
defer func() { _ = sshConn.Close() }()
setIndex := rand.Intn(len(s.sets))
logInfo(fmt.Sprintf(
@@ -228,7 +228,7 @@ func (s *Server) handleConn(conn net.Conn) {
for newChannel := range chans {
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
_ = newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel, requests, err := newChannel.Accept()
@@ -318,7 +318,7 @@ func (s *Server) handleSession(
}
logDebug(fmt.Sprintf("Client %s TERM=%q -> color tier %d", ip, sanitizeN(term, 64), tier))
}
req.Reply(true, nil)
_ = req.Reply(true, nil)
case "window-change":
if len(req.Payload) >= 8 {
cols := int(binary.BigEndian.Uint32(req.Payload))
@@ -326,7 +326,7 @@ func (s *Server) handleSession(
size.set(cols, rows, s.config.MaxDimension)
}
if req.WantReply {
req.Reply(true, nil)
_ = req.Reply(true, nil)
}
case "exec":
command := ""
@@ -337,21 +337,21 @@ func (s *Server) handleSession(
}
}
logInfo(fmt.Sprintf("Client %s attempted exec: %q", ip, sanitizeN(command, 512)))
req.Reply(true, nil)
_ = req.Reply(true, nil)
if !started {
started = true
go s.playVideo(sshConn, channel, size, ip, initialSetIndex, false, tier)
}
case "shell":
logDebug("Opening shell for session", ip)
req.Reply(true, nil)
_ = req.Reply(true, nil)
if !started {
started = true
go s.playVideo(sshConn, channel, size, ip, initialSetIndex, false, tier)
}
default:
if req.WantReply {
req.Reply(false, nil)
_ = req.Reply(false, nil)
}
}
}
@@ -384,8 +384,8 @@ func (s *Server) playVideo(
logDebug(fmt.Sprintf("Terminal size %dx%d for %s", w, h, ip))
if s.fakeLogin != nil {
channel.Write([]byte(clearScreen))
channel.Write([]byte(*s.fakeLogin))
_, _ = channel.Write([]byte(clearScreen))
_, _ = channel.Write([]byte(*s.fakeLogin))
}
done := make(chan struct{})
@@ -465,7 +465,7 @@ func (s *Server) playVideo(
ascii, err := current.renderer.render(currentFrame, w, h, keepAspectRatio, tier)
if err != nil {
logError("Render error for", ip, sanitize(err.Error()))
sshConn.Close()
_ = sshConn.Close()
return
}
@@ -482,14 +482,14 @@ func (s *Server) playVideo(
currentFrame = 0
loopCount++
if config.MaxLoop > 0 && loopCount >= config.MaxLoop {
channel.Write([]byte(clearScreen))
_, _ = channel.Write([]byte(clearScreen))
if s.goodbye != nil {
channel.Write([]byte(*s.goodbye))
_, _ = channel.Write([]byte(*s.goodbye))
}
time.Sleep(1 * time.Second)
logInfo("Playback finished, closing session", ip)
channel.Close()
sshConn.Close()
_ = channel.Close()
_ = sshConn.Close()
return
}
+1 -1
View File
@@ -113,7 +113,7 @@ func extractFrames(path, vf, label string, maxDimension, totalFrames int) ([][]b
break
}
if err != nil {
cmd.Wait()
_ = cmd.Wait()
return nil, fmt.Errorf("ffmpeg stream error: %s", err.Error())
}
}