From 67adaa29568b337b305d8cab515a8c01ce00fcf4 Mon Sep 17 00:00:00 2001 From: Mkhanyisi Madlavana Date: Tue, 10 Sep 2024 16:53:33 +0200 Subject: [PATCH] Fix integer overflow issues at high speeds (#73) --- defs/bytes_counter.go | 8 ++++---- defs/server.go | 4 ++-- report/json.go | 4 ++-- speedtest/helper.go | 13 ++++++------- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/defs/bytes_counter.go b/defs/bytes_counter.go index 1553aff..64c155b 100644 --- a/defs/bytes_counter.go +++ b/defs/bytes_counter.go @@ -14,7 +14,7 @@ import ( type BytesCounter struct { start time.Time pos int - total int + total uint64 payload []byte reader io.ReadSeeker mebi bool @@ -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 @@ -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() @@ -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 } diff --git a/defs/server.go b/defs/server.go index 370b826..ed2ffad 100644 --- a/defs/server.go +++ b/defs/server.go @@ -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()) @@ -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()) diff --git a/report/json.go b/report/json.go index fee05b3..d399aed 100644 --- a/report/json.go +++ b/report/json.go @@ -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"` diff --git a/speedtest/helper.go b/speedtest/helper.go index 4d1d5d7..1be6203 100644 --- a/speedtest/helper.go +++ b/speedtest/helper.go @@ -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 ( @@ -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 { @@ -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 { @@ -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