{"record":{"id":"43a4b59765f65b5c","repo":"go-delve/delve","slug":"can-t-write-halfway-through-a-file","errorCode":null,"errorMessage":"can't write halfway through a file","messagePattern":"can't write halfway through a file","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/elfwriter/writer.go","lineNumber":51,"sourceCode":"\tseekShstrndx      int64\n}\n\ntype Note struct {\n\tType elf.NType\n\tName string\n\tData []byte\n}\n\n// New creates a new Writer.\nfunc New(w WriteCloserSeeker, fhdr *elf.FileHeader) *Writer {\n\tconst (\n\t\tehsize    = 64\n\t\tphentsize = 56\n\t\tshentsize = 64\n\t)\n\n\tif seek, _ := w.Seek(0, io.SeekCurrent); seek != 0 {\n\t\tpanic(\"can't write halfway through a file\")\n\t}\n\n\tr := &Writer{w: w}\n\n\tif fhdr.Class != elf.ELFCLASS64 {\n\t\tpanic(\"unsupported\")\n\t}\n\n\tif fhdr.Data != elf.ELFDATA2LSB {\n\t\tpanic(\"unsupported\")\n\t}\n\n\t// e_ident\n\tr.Write([]byte{0x7f, 'E', 'L', 'F', byte(fhdr.Class), byte(fhdr.Data), byte(fhdr.Version), byte(fhdr.OSABI), fhdr.ABIVersion, 0, 0, 0, 0, 0, 0, 0})\n\n\tr.u16(uint16(fhdr.Type))    // e_type\n\tr.u16(uint16(fhdr.Machine)) // e_machine\n\tr.u32(uint32(fhdr.Version)) // e_version","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/elfwriter/writer.go#L33-L69","documentation":"elfwriter.New panics with \"can't write halfway through a file\" when the underlying io.WriteSeeker's current position is not 0. The writer emits a complete ELF image from scratch (ELF header at offset 0) and only supports writing to a fresh, empty destination. This is an API-contract violation by the caller, so the library fails fast.","triggerScenarios":"Calling elfwriter.New with a WriteSeeker that has already been written to or whose cursor was Seek'ed to a non-zero offset — e.g. appending to an existing file or reusing a buffer after a previous New call.","commonSituations":"Reusing an os.File opened with O_APPEND or already containing data; calling New twice on the same bytes.Buffer that retains prior writes; building a core-dump copy after partially writing a header.","solutions":["Pass a fresh/empty WriteSeeker (new file truncated to 0, new bytes.Buffer).","If reusing an existing file, call Seek(0, io.SeekStart) and Truncate(0) before New.","If reusing a buffer, reset it (buf.Reset()) first.","Wrap New in recover() if input origin cannot be trusted."],"exampleFix":"// before\nf, _ := os.Create(\"out.elf\")\nf.WriteString(\"junk\")\nw := elfwriter.New(f) // panics\n// after\nf, _ := os.Create(\"out.elf\") // O_TRUNC, position 0\nw := elfwriter.New(f)","handlingStrategy":"validation","validationCode":"// Go: ensure the destination is fresh and at offset 0\nif seek, _ := w.Seek(0, io.SeekCurrent); seek != 0 {\n\treturn errors.New(\"writer must start at offset 0; use a fresh/truncated writer\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a new file (O_TRUNC) or freshly reset bytes.Buffer to elfwriter.New","Never reuse a WriteSeeker across multiple New calls","Seek(0)+Truncate(0) if reusing an existing file","Document that elfwriter writes complete images from scratch"],"tags":["elf","panic","api-misuse","writer"],"backgroundTag":"invalid-api-contract","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}