From 02f73ca71bd5f4d8b6fb39c6765d79dd38b39365 Mon Sep 17 00:00:00 2001 From: Chris Novakovic Date: Wed, 8 Oct 2025 13:39:24 +0100 Subject: [PATCH] Writer: ensure ModTime is at least Unix epoch Modification times before the Unix epoch cannot meaningfully be represented in ar file headers, which store times as stringified Unix times - ensure the modification time in the header that is passed to `WriteHeader` is at least the epoch. --- writer.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/writer.go b/writer.go index 0406be4..352bd35 100644 --- a/writer.go +++ b/writer.go @@ -6,10 +6,14 @@ import ( "fmt" "io" "strconv" + "time" ) var ( ErrWriteTooLong = errors.New("ar: write too long") + + // Epoch is the Unix epoch, 00:00:00 UTC on 1970-01-01. + Epoch = time.Unix(0, 0) ) // Writer provides sequential writing of an ar archive. @@ -228,6 +232,11 @@ func (aw *Writer) WriteHeader(hdr *Header) error { // This should be unreachable. return errors.New("ar: unsupported variant") } + // Modification times before the Unix epoch cannot meaningfully be represented in ar headers, which + // store times as stringified Unix times - ensure the modification time is at least the epoch. + if hdr.ModTime.Before(Epoch) { + hdr.ModTime = Epoch + } aw.numeric(s.next(12), hdr.ModTime.Unix()) aw.numeric(s.next(6), int64(hdr.Uid)) aw.numeric(s.next(6), int64(hdr.Gid))