Skip to content

Commit 6bbc97e

Browse files
Writer: ensure ModTime is at least Unix epoch (#15)
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.
1 parent 7bbf814 commit 6bbc97e

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

writer.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ import (
66
"fmt"
77
"io"
88
"strconv"
9+
"time"
910
)
1011

1112
var (
1213
ErrWriteTooLong = errors.New("ar: write too long")
14+
15+
// Epoch is the Unix epoch, 00:00:00 UTC on 1970-01-01.
16+
Epoch = time.Unix(0, 0)
1317
)
1418

1519
// Writer provides sequential writing of an ar archive.
@@ -228,6 +232,11 @@ func (aw *Writer) WriteHeader(hdr *Header) error {
228232
// This should be unreachable.
229233
return errors.New("ar: unsupported variant")
230234
}
235+
// Modification times before the Unix epoch cannot meaningfully be represented in ar headers, which
236+
// store times as stringified Unix times - ensure the modification time is at least the epoch.
237+
if hdr.ModTime.Before(Epoch) {
238+
hdr.ModTime = Epoch
239+
}
231240
aw.numeric(s.next(12), hdr.ModTime.Unix())
232241
aw.numeric(s.next(6), int64(hdr.Uid))
233242
aw.numeric(s.next(6), int64(hdr.Gid))

0 commit comments

Comments
 (0)