{"record":{"id":"9bc9e6497871583f","repo":"shadow1ng/fscan","slug":"result-cannot-be-nil-9bc9e6","errorCode":null,"errorMessage":"result cannot be nil","messagePattern":"result cannot be nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/output/writers.go","lineNumber":127,"sourceCode":"\t\trealtimePath: realtimePath,\n\t}, nil\n}\n\n// WriteHeader 写入头部\nfunc (w *TXTWriter) WriteHeader() error {\n\treturn nil\n}\n\n// Write 收集扫描结果到分类缓冲，同时实时备份\nfunc (w *TXTWriter) Write(result *ScanResult) error {\n\tw.mu.Lock()\n\tdefer w.mu.Unlock()\n\n\tif w.closed {\n\t\treturn fmt.Errorf(\"writer is closed\")\n\t}\n\tif result == nil {\n\t\treturn fmt.Errorf(\"result cannot be nil\")\n\t}\n\n\t// 1. 加入内存分类缓冲（用于最终有序输出）\n\tw.buffer.Add(result)\n\n\t// 2. 实时写入备份文件（防崩溃丢数据）\n\tif w.realtimeFile != nil {\n\t\tline := w.formatLine(result)\n\t\tif _, err := w.realtimeFile.WriteString(line + \"\\n\"); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to write realtime backup: %w\", err)\n\t\t}\n\t\tif err := w.realtimeFile.Sync(); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to sync realtime backup: %w\", err)\n\t\t}\n\t}\n\n\treturn nil\n}","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/shadow1ng/fscan/blob/95cc12e753bf43de7004e5aef42a9ffba3934303/common/output/writers.go#L109-L145","documentation":"Argument guard in TXTWriter.Write: the caller passed a nil *ScanResult. Nothing can be formatted or added to the result buffer/realtime backup, so the write is rejected; it is an API-contract violation, not a filesystem or state problem.","triggerScenarios":"Passing nil directly to Write, forwarding (nil, nil) from an upstream scan function, or iterating a results slice containing nil entries.","commonSituations":"Aggregation code appending nil placeholders, failed probe stages returning no result and no error, tests like TestTXTWriter_NilResult.","solutions":["Nil-check results before calling Write","Fix upstream functions to return an error whenever the result is nil","Filter nil entries from result slices before writing"],"exampleFix":"// before\nfor _, r := range results { w.Write(r) } // r may be nil\n// after\nfor _, r := range results {\n    if r != nil { w.Write(r) }\n}","handlingStrategy":"type-guard","validationCode":"if res == nil {\n    return errors.New(\"cannot write nil result\")\n}\nreturn w.Write(res)","typeGuard":"func writable(r *ScanResult) bool { return r != nil }","tryCatchPattern":"if err := w.Write(res); err != nil {\n    if strings.Contains(err.Error(), \"result cannot be nil\") {\n        log.Debug(\"nil result skipped\")\n        return nil\n    }\n    return err\n}","preventionTips":["Guard all Write call sites with nil checks","Ensure upstream producers never return nil results without an error","Sanitize result slices (remove nils) before the write phase"],"tags":["output","nil-check","validation"],"backgroundTag":"null-argument","analyzedSha":"95cc12e753bf43de7004e5aef42a9ffba3934303","analyzedAt":"2026-09-06T17:07:30.094Z","contentChangedAt":"2026-09-06T17:07:30.094Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}