From 16b74247fbc72e96cdfba0a4a4bfe176d288a53a Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Thu, 23 Jul 2015 14:19:58 -0700 Subject: [PATCH] Fix uses of "int" where "int64" should be used instead Some structures use int for sizes and UNIX timestamps. On some platforms, int is 32 bits, so this can lead to the year 2038 issues and overflows when dealing with large containers or layers. Consistently use int64 to store sizes and UNIX timestamps in api/types/types.go. Update related to code accordingly (i.e. strconv.FormatInt instead of strconv.Itoa). Use int64 in progressreader package to avoid integer overflow when dealing with large quantities. Update related code accordingly. Signed-off-by: Aaron Lehmann --- jsonmessage/jsonmessage.go | 6 +++--- progressreader/progressreader.go | 12 ++++++------ progressreader/progressreader_test.go | 4 ++-- tarsum/versioning.go | 10 +++++----- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/jsonmessage/jsonmessage.go b/jsonmessage/jsonmessage.go index 7db1626..0c93ee6 100644 --- a/jsonmessage/jsonmessage.go +++ b/jsonmessage/jsonmessage.go @@ -23,8 +23,8 @@ func (e *JSONError) Error() string { type JSONProgress struct { terminalFd uintptr - Current int `json:"current,omitempty"` - Total int `json:"total,omitempty"` + Current int64 `json:"current,omitempty"` + Total int64 `json:"total,omitempty"` Start int64 `json:"start,omitempty"` } @@ -64,7 +64,7 @@ func (p *JSONProgress) String() string { numbersBox = fmt.Sprintf("%8v/%v", current, total) if p.Current > 0 && p.Start > 0 && percentage < 50 { - fromStart := time.Now().UTC().Sub(time.Unix(int64(p.Start), 0)) + fromStart := time.Now().UTC().Sub(time.Unix(p.Start, 0)) perEntry := fromStart / time.Duration(p.Current) left := time.Duration(p.Total-p.Current) * perEntry left = (left / time.Second) * time.Second diff --git a/progressreader/progressreader.go b/progressreader/progressreader.go index d55fadd..f48442b 100644 --- a/progressreader/progressreader.go +++ b/progressreader/progressreader.go @@ -14,9 +14,9 @@ type Config struct { In io.ReadCloser // Stream to read from Out io.Writer // Where to send progress bar to Formatter *streamformatter.StreamFormatter - Size int - Current int - LastUpdate int + Size int64 + Current int64 + LastUpdate int64 NewLines bool ID string Action string @@ -29,11 +29,11 @@ func New(newReader Config) *Config { func (config *Config) Read(p []byte) (n int, err error) { read, err := config.In.Read(p) - config.Current += read - updateEvery := 1024 * 512 //512kB + config.Current += int64(read) + updateEvery := int64(1024 * 512) //512kB if config.Size > 0 { // Update progress for every 1% read if 1% < 512kB - if increment := int(0.01 * float64(config.Size)); increment < updateEvery { + if increment := int64(0.01 * float64(config.Size)); increment < updateEvery { updateEvery = increment } } diff --git a/progressreader/progressreader_test.go b/progressreader/progressreader_test.go index fdf40cb..21d9b0f 100644 --- a/progressreader/progressreader_test.go +++ b/progressreader/progressreader_test.go @@ -20,7 +20,7 @@ func TestOutputOnPrematureClose(t *testing.T) { In: reader, Out: writer, Formatter: streamformatter.NewStreamFormatter(), - Size: len(content), + Size: int64(len(content)), NewLines: true, ID: "Test", Action: "Read", @@ -60,7 +60,7 @@ func TestCompleteSilently(t *testing.T) { In: reader, Out: writer, Formatter: streamformatter.NewStreamFormatter(), - Size: len(content), + Size: int64(len(content)), NewLines: true, ID: "Test", Action: "Read", diff --git a/tarsum/versioning.go b/tarsum/versioning.go index 8988b9f..2882286 100644 --- a/tarsum/versioning.go +++ b/tarsum/versioning.go @@ -95,17 +95,17 @@ func (f tarHeaderSelectFunc) selectHeaders(h *tar.Header) (orderedHeaders [][2]s func v0TarHeaderSelect(h *tar.Header) (orderedHeaders [][2]string) { return [][2]string{ {"name", h.Name}, - {"mode", strconv.Itoa(int(h.Mode))}, + {"mode", strconv.FormatInt(h.Mode, 10)}, {"uid", strconv.Itoa(h.Uid)}, {"gid", strconv.Itoa(h.Gid)}, - {"size", strconv.Itoa(int(h.Size))}, - {"mtime", strconv.Itoa(int(h.ModTime.UTC().Unix()))}, + {"size", strconv.FormatInt(h.Size, 10)}, + {"mtime", strconv.FormatInt(h.ModTime.UTC().Unix(), 10)}, {"typeflag", string([]byte{h.Typeflag})}, {"linkname", h.Linkname}, {"uname", h.Uname}, {"gname", h.Gname}, - {"devmajor", strconv.Itoa(int(h.Devmajor))}, - {"devminor", strconv.Itoa(int(h.Devminor))}, + {"devmajor", strconv.FormatInt(h.Devmajor, 10)}, + {"devminor", strconv.FormatInt(h.Devminor, 10)}, } }