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 <aaron.lehmann@docker.com>
This commit is contained in:
Aaron Lehmann 2015-07-23 14:19:58 -07:00
parent 8cd8bc1365
commit 16b74247fb
4 changed files with 16 additions and 16 deletions

View file

@ -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

View file

@ -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
}
}

View file

@ -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",

View file

@ -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)},
}
}