Skip to content

Commit d3c1f81

Browse files
gballetfirmianavan
authored andcommitted
whisper: fix issue in topic list copy (ethereum#16381)
- Fixes ethereum#16271. What was appeneded was a pointer to an object that changes during the iteration. - The topic is allocated as a 4-byte array, fill partial topics with 0s. Partial topics are currently disabled, but would crash as they rely on the presence of byte number 3.
1 parent 405515a commit d3c1f81

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

whisper/whisperv6/api.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,10 @@ func (api *PublicWhisperAPI) NewMessageFilter(req Criteria) (string, error) {
558558
}
559559

560560
if len(req.Topics) > 0 {
561-
topics = make([][]byte, 0, len(req.Topics))
562-
for _, topic := range req.Topics {
563-
topics = append(topics, topic[:])
561+
topics = make([][]byte, len(req.Topics))
562+
for i, topic := range req.Topics {
563+
topics[i] = make([]byte, TopicLength)
564+
copy(topics[i], topic[:])
564565
}
565566
}
566567

whisper/whisperv6/api_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2018 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package whisperv6
18+
19+
import (
20+
"bytes"
21+
"crypto/ecdsa"
22+
"testing"
23+
"time"
24+
25+
"github.com/ethereum/go-ethereum/common"
26+
set "gopkg.in/fatih/set.v0"
27+
)
28+
29+
func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
30+
w := &Whisper{
31+
privateKeys: make(map[string]*ecdsa.PrivateKey),
32+
symKeys: make(map[string][]byte),
33+
envelopes: make(map[common.Hash]*Envelope),
34+
expirations: make(map[uint32]*set.SetNonTS),
35+
peers: make(map[*Peer]struct{}),
36+
messageQueue: make(chan *Envelope, messageQueueLimit),
37+
p2pMsgQueue: make(chan *Envelope, messageQueueLimit),
38+
quit: make(chan struct{}),
39+
syncAllowance: DefaultSyncAllowance,
40+
}
41+
w.filters = NewFilters(w)
42+
43+
keyID, err := w.GenerateSymKey()
44+
if err != nil {
45+
t.Fatalf("Error generating symmetric key: %v", err)
46+
}
47+
api := PublicWhisperAPI{
48+
w: w,
49+
lastUsed: make(map[string]time.Time),
50+
}
51+
52+
t1 := [4]byte{0xde, 0xea, 0xbe, 0xef}
53+
t2 := [4]byte{0xca, 0xfe, 0xde, 0xca}
54+
55+
crit := Criteria{
56+
SymKeyID: keyID,
57+
Topics: []TopicType{TopicType(t1), TopicType(t2)},
58+
}
59+
60+
_, err = api.NewMessageFilter(crit)
61+
if err != nil {
62+
t.Fatalf("Error creating the filter: %v", err)
63+
}
64+
65+
found := false
66+
candidates := w.filters.getWatchersByTopic(TopicType(t1))
67+
for _, f := range candidates {
68+
if len(f.Topics) == 2 {
69+
if bytes.Equal(f.Topics[0], t1[:]) && bytes.Equal(f.Topics[1], t2[:]) {
70+
found = true
71+
}
72+
}
73+
}
74+
75+
if !found {
76+
t.Fatalf("Could not find filter with both topics")
77+
}
78+
}

0 commit comments

Comments
 (0)