]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Clarify maximum value of "metadata_size" (#609)
authorYenForYang <YenForYang@users.noreply.github.com>
Tue, 14 Sep 2021 12:36:19 +0000 (07:36 -0500)
committerGitHub <noreply@github.com>
Tue, 14 Sep 2021 12:36:19 +0000 (22:36 +1000)
global.go
torrent.go

index e06d93230428a2ac08c5538a0ea09821492386cc..4e48d0c550ea9eb5881ba2e60a1d29824764f68c 100644 (file)
--- a/global.go
+++ b/global.go
@@ -10,6 +10,10 @@ import (
 const (
        pieceHash        = crypto.SHA1
        defaultChunkSize = 0x4000 // 16KiB
+       
+       // Arbitrary maximum of "metadata_size" (see https://www.bittorrent.org/beps/bep_0009.html)
+       // This value is 2x what libtorrent-rasterbar uses, which should be plenty
+       maxMetadataSize uint32 = 8*1024*1024
 )
 
 // These are our extended message IDs. Peers will use these values to
index 6f4730e2eaa606a414f53e7a9ee815d9a564f333..a584874b383cb327933e2cbde47f4fc34c4dd696 100644 (file)
@@ -481,19 +481,19 @@ func (t *Torrent) haveAllMetadataPieces() bool {
 }
 
 // TODO: Propagate errors to disconnect peer.
-func (t *Torrent) setMetadataSize(bytes int) (err error) {
+func (t *Torrent) setMetadataSize(size int) (err error) {
        if t.haveInfo() {
                // We already know the correct metadata size.
                return
        }
-       if bytes <= 0 || bytes > 10000000 { // 10MB, pulled from my ass.
+       if uint32(size) > maxMetadataSize {
                return errors.New("bad size")
        }
-       if t.metadataBytes != nil && len(t.metadataBytes) == int(bytes) {
+       if len(t.metadataBytes) == size {
                return
        }
-       t.metadataBytes = make([]byte, bytes)
-       t.metadataCompletedChunks = make([]bool, (bytes+(1<<14)-1)/(1<<14))
+       t.metadataBytes = make([]byte, size)
+       t.metadataCompletedChunks = make([]bool, (size+(1<<14)-1)/(1<<14))
        t.metadataChanged.Broadcast()
        for c := range t.conns {
                c.requestPendingMetadata()