]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Enforce bad integer division math
authorMatt Joiner <anacrolix@gmail.com>
Tue, 27 May 2025 14:29:13 +0000 (00:29 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 27 May 2025 14:29:13 +0000 (00:29 +1000)
math.go
torrent.go

diff --git a/math.go b/math.go
index cd733e826249c601063df2a4fa6528ee72df2047..daf62b4244fb87db8eab0552f44263ed6413e870 100644 (file)
--- a/math.go
+++ b/math.go
@@ -4,7 +4,7 @@ import (
        "golang.org/x/exp/constraints"
 )
 
-func intCeilDiv[T constraints.Integer](a, b T) T {
+func intCeilDiv[T constraints.Unsigned](a, b T) T {
        // This still sux for negative numbers due to truncating division. But I don't know that we need
        // or ceil division makes sense for negative numbers.
        return (a + b - 1) / b
index d68c3f49cd8f37f233edd844212926ef69be1c7f..a1dc80d51dd1f59a7dbe882fb4da2236e7b3b0aa 100644 (file)
@@ -538,8 +538,7 @@ func (t *Torrent) setInfo(info *metainfo.Info) error {
        t.info = info
        t.getInfoCtxCancel(errors.New("got info"))
        t.nameMu.Unlock()
-       t._chunksPerRegularPiece = chunkIndexType(
-               (pp.Integer(t.usualPieceSize()) + t.chunkSize - 1) / t.chunkSize)
+       t._chunksPerRegularPiece = chunkIndexType(intCeilDiv(pp.Integer(t.usualPieceSize()), t.chunkSize))
        t.deferUpdateComplete()
        t.displayName = "" // Save a few bytes lol.
        t.initFiles()
@@ -3053,7 +3052,7 @@ func (t *Torrent) addWebSeed(url string, opts ...AddWebSeedsOpt) bool {
        g.MakeMapWithCap(&ws.activeRequests, ws.client.MaxRequests)
        // TODO: Implement an algorithm that assigns this based on sharing chunks across peers. For now
        // we just allow 2 MiB worth of requests. See newHotPeerImpl.nominalMaxRequests.
-       ws.peer.PeerMaxRequests = intCeilDiv(2<<20, ws.peer.t.chunkSize.Int())
+       ws.peer.PeerMaxRequests = maxRequests(intCeilDiv(8<<20, ws.peer.t.chunkSize.Uint32()))
        ws.peer.initUpdateRequestsTimer()
        ws.locker = t.cl.locker()
        for _, f := range t.callbacks().NewPeer {