Skip to content

Commit 67adaa2

Browse files
authored
Fix integer overflow issues at high speeds (#73)
1 parent e5c131f commit 67adaa2

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

defs/bytes_counter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
type BytesCounter struct {
1515
start time.Time
1616
pos int
17-
total int
17+
total uint64
1818
payload []byte
1919
reader io.ReadSeeker
2020
mebi bool
@@ -33,7 +33,7 @@ func NewCounter() *BytesCounter {
3333
func (c *BytesCounter) Write(p []byte) (int, error) {
3434
n := len(p)
3535
c.lock.Lock()
36-
c.total += n
36+
c.total += uint64(n)
3737
c.lock.Unlock()
3838

3939
return n, nil
@@ -43,7 +43,7 @@ func (c *BytesCounter) Write(p []byte) (int, error) {
4343
func (c *BytesCounter) Read(p []byte) (int, error) {
4444
n, err := c.reader.Read(p)
4545
c.lock.Lock()
46-
c.total += n
46+
c.total += uint64(n)
4747
c.pos += n
4848
if c.pos == c.uploadSize {
4949
c.resetReader()
@@ -116,7 +116,7 @@ func (c *BytesCounter) Start() {
116116
}
117117

118118
// Total returns the total bytes read/written
119-
func (c *BytesCounter) Total() int {
119+
func (c *BytesCounter) Total() uint64 {
120120
return c.total
121121
}
122122

defs/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (s *Server) PingAndJitter(count int) (float64, float64, error) {
188188
}
189189

190190
// Download performs the actual download test
191-
func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chunks int, duration time.Duration) (float64, int, error) {
191+
func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chunks int, duration time.Duration) (float64, uint64, error) {
192192
t := time.Now()
193193
defer func() {
194194
s.TLog.Logf("Download took %s", time.Now().Sub(t).String())
@@ -280,7 +280,7 @@ Loop:
280280
}
281281

282282
// Upload performs the actual upload test
283-
func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int, uploadSize int, duration time.Duration) (float64, int, error) {
283+
func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int, uploadSize int, duration time.Duration) (float64, uint64, error) {
284284
t := time.Now()
285285
defer func() {
286286
s.TLog.Logf("Upload took %s", time.Now().Sub(t).String())

report/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type JSONReport struct {
1111
Timestamp time.Time `json:"timestamp"`
1212
Server Server `json:"server"`
1313
Client Client `json:"client"`
14-
BytesSent int `json:"bytes_sent"`
15-
BytesReceived int `json:"bytes_received"`
14+
BytesSent uint64 `json:"bytes_sent"`
15+
BytesReceived uint64 `json:"bytes_received"`
1616
Ping float64 `json:"ping"`
1717
Jitter float64 `json:"jitter"`
1818
Upload float64 `json:"upload"`

speedtest/helper.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import (
1515

1616
"github.com/briandowns/spinner"
1717
"github.com/gocarina/gocsv"
18-
log "github.com/sirupsen/logrus"
19-
"github.com/urfave/cli/v2"
20-
2118
"github.com/librespeed/speedtest-cli/defs"
2219
"github.com/librespeed/speedtest-cli/report"
20+
log "github.com/sirupsen/logrus"
21+
"github.com/urfave/cli/v2"
2322
)
2423

2524
const (
@@ -85,7 +84,7 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
8584

8685
// get download value
8786
var downloadValue float64
88-
var bytesRead int
87+
var bytesRead uint64
8988
if c.Bool(defs.OptionNoDownload) {
9089
log.Info("Download test is disabled")
9190
} else {
@@ -95,12 +94,12 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
9594
return err
9695
}
9796
downloadValue = download
98-
bytesRead = br
97+
bytesRead = uint64(br)
9998
}
10099

101100
// get upload value
102101
var uploadValue float64
103-
var bytesWritten int
102+
var bytesWritten uint64
104103
if c.Bool(defs.OptionNoUpload) {
105104
log.Info("Upload test is disabled")
106105
} else {
@@ -110,7 +109,7 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
110109
return err
111110
}
112111
uploadValue = upload
113-
bytesWritten = bw
112+
bytesWritten = uint64(bw)
114113
}
115114

116115
// print result if --simple is given

0 commit comments

Comments
 (0)