Skip to content

Commit

Permalink
fix: guard operating on nil resource fields
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <[email protected]>
  • Loading branch information
wolf31o2 committed Apr 27, 2024
1 parent 13c08ad commit d94dbe1
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions mempool/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func newConsumer() *MempoolConsumer {
}

func (m *MempoolConsumer) NextTx(blocking bool) *MempoolTransaction {
if m == nil {
return nil
}
var ret *MempoolTransaction
if blocking {
// Wait until a transaction is available
Expand Down Expand Up @@ -59,28 +62,39 @@ func (m *MempoolConsumer) NextTx(blocking bool) *MempoolTransaction {
}

func (m *MempoolConsumer) GetTxFromCache(hash string) *MempoolTransaction {
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
return m.cache[hash]
if m != nil {
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
return m.cache[hash]
}
}

Check failure on line 70 in mempool/consumer.go

View workflow job for this annotation

GitHub Actions / go-test (1.21.x, ubuntu-latest)

missing return

Check failure on line 70 in mempool/consumer.go

View workflow job for this annotation

GitHub Actions / go-test (1.22.x, ubuntu-latest)

missing return

func (m *MempoolConsumer) ClearCache() {
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
m.cache = make(map[string]*MempoolTransaction)
if m != nil {
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
m.cache = make(map[string]*MempoolTransaction)
}
}

func (m *MempoolConsumer) RemoveTxFromCache(hash string) {
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
delete(m.cache, hash)
if m != nil {
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
delete(m.cache, hash)
}
}

func (m *MempoolConsumer) stop() {
close(m.txChan)
if m != nil {
close(m.txChan)
}
}

func (m *MempoolConsumer) pushTx(tx *MempoolTransaction, wait bool) bool {
if m == nil {
return false
}
if wait {
// Block on write to channel
m.txChan <- tx
Expand All @@ -94,4 +108,5 @@ func (m *MempoolConsumer) pushTx(tx *MempoolTransaction, wait bool) bool {
return false
}
}
return false
}

0 comments on commit d94dbe1

Please sign in to comment.