Skip to content

Commit

Permalink
Fix integer overflow issues at high speeds (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
dopstar authored Sep 10, 2024
1 parent e5c131f commit 67adaa2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
8 changes: 4 additions & 4 deletions defs/bytes_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type BytesCounter struct {
start time.Time
pos int
total int
total uint64
payload []byte
reader io.ReadSeeker
mebi bool
Expand All @@ -33,7 +33,7 @@ func NewCounter() *BytesCounter {
func (c *BytesCounter) Write(p []byte) (int, error) {
n := len(p)
c.lock.Lock()
c.total += n
c.total += uint64(n)
c.lock.Unlock()

return n, nil
Expand All @@ -43,7 +43,7 @@ func (c *BytesCounter) Write(p []byte) (int, error) {
func (c *BytesCounter) Read(p []byte) (int, error) {
n, err := c.reader.Read(p)
c.lock.Lock()
c.total += n
c.total += uint64(n)
c.pos += n
if c.pos == c.uploadSize {
c.resetReader()
Expand Down Expand Up @@ -116,7 +116,7 @@ func (c *BytesCounter) Start() {
}

// Total returns the total bytes read/written
func (c *BytesCounter) Total() int {
func (c *BytesCounter) Total() uint64 {
return c.total
}

Expand Down
4 changes: 2 additions & 2 deletions defs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (s *Server) PingAndJitter(count int) (float64, float64, error) {
}

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

// Upload performs the actual upload test
func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int, uploadSize int, duration time.Duration) (float64, int, error) {
func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int, uploadSize int, duration time.Duration) (float64, uint64, error) {
t := time.Now()
defer func() {
s.TLog.Logf("Upload took %s", time.Now().Sub(t).String())
Expand Down
4 changes: 2 additions & 2 deletions report/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type JSONReport struct {
Timestamp time.Time `json:"timestamp"`
Server Server `json:"server"`
Client Client `json:"client"`
BytesSent int `json:"bytes_sent"`
BytesReceived int `json:"bytes_received"`
BytesSent uint64 `json:"bytes_sent"`
BytesReceived uint64 `json:"bytes_received"`
Ping float64 `json:"ping"`
Jitter float64 `json:"jitter"`
Upload float64 `json:"upload"`
Expand Down
13 changes: 6 additions & 7 deletions speedtest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import (

"github.com/briandowns/spinner"
"github.com/gocarina/gocsv"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"github.com/librespeed/speedtest-cli/defs"
"github.com/librespeed/speedtest-cli/report"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

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

// get download value
var downloadValue float64
var bytesRead int
var bytesRead uint64
if c.Bool(defs.OptionNoDownload) {
log.Info("Download test is disabled")
} else {
Expand All @@ -95,12 +94,12 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
return err
}
downloadValue = download
bytesRead = br
bytesRead = uint64(br)
}

// get upload value
var uploadValue float64
var bytesWritten int
var bytesWritten uint64
if c.Bool(defs.OptionNoUpload) {
log.Info("Upload test is disabled")
} else {
Expand All @@ -110,7 +109,7 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
return err
}
uploadValue = upload
bytesWritten = bw
bytesWritten = uint64(bw)
}

// print result if --simple is given
Expand Down

0 comments on commit 67adaa2

Please sign in to comment.