{"record":{"id":"265a2ea3588200ea","repo":"valyala/fasthttp","slug":"invalid-writeheader-code-v","errorCode":null,"errorMessage":"invalid WriteHeader code %v","messagePattern":"invalid WriteHeader code (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"fasthttpadaptor/adaptor.go","lineNumber":251,"sourceCode":"\t}\n}\n\nfunc releaseWriter(w *writer) {\n\t_ = w.Close()\n\tif w.bufPool != nil {\n\t\tbufferPool.Put(w.bufPool)\n\t\tw.bufPool = nil\n\t}\n}\n\nfunc (w *writer) Header() http.Header {\n\treturn w.h\n}\n\nfunc (w *writer) WriteHeader(code int) {\n\t// Allow the same codes as net/http.\n\tif code < 100 || code > 999 {\n\t\tpanic(fmt.Sprintf(\"invalid WriteHeader code %v\", code))\n\t}\n\tw.statusCode.CompareAndSwap(0, int64(code))\n}\n\nfunc (w *writer) Write(p []byte) (int, error) {\n\tselect {\n\tcase <-w.streamReady:\n\t\treturn w.pw.Write(p)\n\tdefault:\n\t}\n\n\tw.mu.Lock()\n\tselect {\n\tcase <-w.streamReady:\n\t\tw.mu.Unlock()\n\t\treturn w.pw.Write(p)\n\tdefault:\n\t}","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/fasthttpadaptor/adaptor.go#L233-L269","documentation":"fasthttpadaptor's writer adapts net/http handlers to fasthttp. WriteHeader mirrors net/http's constraint that status codes must be 100-999 and panics otherwise, since fasthttp cannot encode such a code on the wire.","triggerScenarios":"A net/http handler (served via adaptor) calls w.WriteHeader with code < 100 (e.g. 0, 42, 99) or > 999; the panic propagates up through ServeHTTP.","commonSituations":"Custom status constants initialized to zero and never set (WriteHeader(0)); experiments with out-of-range codes; copied handler code using sentinel values like -1.","solutions":["Only pass valid HTTP status codes 100-999 to WriteHeader","Default to http.StatusOK (200) when no explicit status is needed — omitting WriteHeader entirely also yields 200","Clamp/validate the status value before calling WriteHeader"],"exampleFix":"// before\nw.WriteHeader(status) // status may be 0\n// after\nif status < 100 || status > 999 {\n    status = http.StatusOK\n}\nw.WriteHeader(status)","handlingStrategy":"try-catch","validationCode":"func safeWriteHeader(w http.ResponseWriter, code int) {\n    if code >= 100 && code <= 999 {\n        w.WriteHeader(code)\n    }\n}","typeGuard":"func isValidStatusCode(code int) bool {\n    return code >= 100 && code <= 999\n}","tryCatchPattern":"func(w http.ResponseWriter, r *http.Request) {\n    defer func() {\n        if rec := recover(); rec != nil {\n            if s, ok := rec.(string); ok && strings.HasPrefix(s, \"invalid WriteHeader code\") {\n                http.Error(w, \"internal error\", http.StatusInternalServerError)\n                return\n            }\n            panic(rec)\n        }\n    }()\n    nextHandler(w, r)\n}","preventionTips":["Never call WriteHeader with zero-valued or sentinel status variables","Initialize status constants to http.StatusOK, not 0","Centralize status writing behind a helper that validates the range"],"tags":["fasthttp","net-http-adaptor","status-code","panic"],"backgroundTag":"invalid-http-status-code","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}