From: Matt Joiner Date: Tue, 27 May 2025 14:29:13 +0000 (+1000) Subject: Enforce bad integer division math X-Git-Tag: v1.59.0~105 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=ded594c0f9645415955de225de10a9c0edbb1e8a;p=btrtrc.git Enforce bad integer division math --- 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 {