From ded594c0f9645415955de225de10a9c0edbb1e8a Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 28 May 2025 00:29:13 +1000 Subject: [PATCH] Enforce bad integer division math --- math.go | 2 +- torrent.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/math.go b/math.go index cd733e82..daf62b42 100644 --- 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 diff --git a/torrent.go b/torrent.go index d68c3f49..a1dc80d5 100644 --- a/torrent.go +++ b/torrent.go @@ -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 { -- 2.51.0