Skip to content

Commit

Permalink
Ensure int64 is 8 byte aligned for sync/atomic to work on 32 bit arci…
Browse files Browse the repository at this point in the history
…techtures
  • Loading branch information
johnerikhalse committed Sep 28, 2021
1 parent fe6e964 commit ce92343
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions internal/countingreader/countingreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

// Reader counts the bytes read through it.
type Reader struct {
ioReader io.Reader
bytesRead int64
maxBytes int64
ioReader io.Reader
}

// NewReader makes a new Reader that counts the bytes
Expand All @@ -52,15 +52,14 @@ func NewLimited(r io.Reader, maxBytes int64) *Reader {
func (r *Reader) Read(p []byte) (n int, err error) {
if r.maxBytes >= 0 {
remaining := r.maxBytes - r.N()
if remaining <= 0 {
return 0, io.EOF
}
if int64(len(p)) > remaining {
p = p[:remaining]
}
n, err = r.ioReader.Read(p)
atomic.AddInt64(&r.bytesRead, int64(n))

if r.N() >= r.maxBytes {
err = io.EOF
}
} else {
n, err = r.ioReader.Read(p)
atomic.AddInt64(&r.bytesRead, int64(n))
Expand Down

0 comments on commit ce92343

Please sign in to comment.