Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow issues at high speeds #73

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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