{"record":{"id":"c9087e646af0cab6","repo":"golang/go","slug":"errwritetoolong","errorCode":"ErrWriteTooLong","errorMessage":"archive/tar: write too long","messagePattern":"archive/tar: write too long","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/archive/tar/common.go","lineNumber":35,"sourceCode":"\t\"io/fs\"\n\t\"maps\"\n\t\"math\"\n\t\"path\"\n\t\"reflect\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n)\n\n// BUG: Use of the Uid and Gid fields in Header could overflow on 32-bit\n// architectures. If a large value is encountered when decoding, the result\n// stored in Header will be the truncated version.\n\nvar tarinsecurepath = godebug.New(\"tarinsecurepath\")\n\nvar (\n\tErrHeader          = errors.New(\"archive/tar: invalid tar header\")\n\tErrWriteTooLong    = errors.New(\"archive/tar: write too long\")\n\tErrFieldTooLong    = errors.New(\"archive/tar: header field too long\")\n\tErrWriteAfterClose = errors.New(\"archive/tar: write after close\")\n\tErrInsecurePath    = errors.New(\"archive/tar: insecure file path\")\n\terrMissData        = errors.New(\"archive/tar: sparse file references non-existent data\")\n\terrUnrefData       = errors.New(\"archive/tar: sparse file contains unreferenced data\")\n\terrWriteHole       = errors.New(\"archive/tar: write non-NUL byte in sparse hole\")\n\terrSparseTooLong   = errors.New(\"archive/tar: sparse map too long\")\n)\n\ntype headerError []string\n\nfunc (he headerError) Error() string {\n\tconst prefix = \"archive/tar: cannot encode header\"\n\tvar ss []string\n\tfor _, s := range he {\n\t\tif s != \"\" {\n\t\t\tss = append(ss, s)\n\t\t}","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/archive/tar/common.go#L17-L53","documentation":"ErrWriteTooLong is returned by the tar Writer when more bytes are written for an entry than declared in its Header.Size, and on Close when the stream still has trailing non-zero bytes after the last entry. The tar format requires each regular-file entry to be exactly Header.Size bytes; overshooting corrupts subsequent headers, so the Writer rejects the extra bytes.","triggerScenarios":"Calling Write([]byte) with total bytes exceeding the Size set on the prior WriteHeader; using io.Copy from a source whose Length is larger than Header.Size; closing the archive when extra bytes remain past the last entry (ensureEOF check).","commonSituations":"Setting Header.Size to a smaller value than the actual file (stale stat, race with file growing between os.Stat and io.Copy); copying a growing log file; mismatched Size between WriteHeader and the data stream; concatenating tars incorrectly so Close sees extra bytes.","solutions":["Set Header.Size to the exact byte length of the data you will write, taken at the moment of writing (re-stat the file).","Use io.CopyN(tw, src, hdr.Size) to guarantee you never write past the declared size.","If the source length is unknown, set Size = 0 only if you genuinely want an empty file; otherwise buffer first or use a pipe with accurate length.","On Close(), ensure the underlying reader is fully consumed before the writer finalizes the footer."],"exampleFix":"// before\nfi, _ := os.Stat(path)\nhdr, _ := tar.FileInfoHeader(fi, \"\")\nhdr.Size = 100 // wrong, file is larger\ntw.WriteHeader(hdr)\nio.Copy(tw, f) // throws ErrWriteTooLong\n\n// after\nfi, _ := os.Stat(path)\nhdr, _ := tar.FileInfoHeader(fi, \"\")\nhdr.Size = fi.Size() // exact\ntw.WriteHeader(hdr)\nio.Copy(tw, f)","handlingStrategy":"validation","validationCode":"fi, err := os.Stat(path)\nif err != nil { return err }\nhdr, _ := tar.FileInfoHeader(fi, \"\")\nhdr.Size = fi.Size() // exact, taken at write time","typeGuard":null,"tryCatchPattern":"n, err := tw.Write(buf)\nif errors.Is(err, tar.ErrWriteTooLong) {\n  log.Printf(\"truncating entry to declared size: dropped %d bytes\", len(buf)-n)\n  err = nil\n}","preventionTips":["Always set Header.Size from a fresh os.Stat immediately before WriteHeader.","Use io.CopyN(tw, src, hdr.Size) to bound writes to the declared size.","Avoid archiving files that may grow concurrently (logs); snapshot or freeze first."],"tags":["archive-tar","writer","size-mismatch","validation"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}