Skip to content

Commit 207df8d

Browse files
authored
Merge pull request #9951 from yyforyongyu/fix-cached-policy
graph: fix nil pointer deference in `ForEachChannelCacheable`
2 parents cb10536 + 46c5715 commit 207df8d

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

graph/db/kv_store.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ type channelMapKey struct {
241241
chanID [8]byte
242242
}
243243

244+
// String returns a human-readable representation of the key.
245+
func (c channelMapKey) String() string {
246+
return fmt.Sprintf("node=%v, chanID=%x", c.nodeKey, c.chanID)
247+
}
248+
244249
// getChannelMap loads all channel edge policies from the database and stores
245250
// them in a map.
246251
func (c *KVStore) getChannelMap(edges kvdb.RBucket) (
@@ -501,20 +506,47 @@ func (c *KVStore) ForEachChannelCacheable(cb func(*models.CachedEdgeInfo,
501506
return err
502507
}
503508

504-
policy1 := channelMap[channelMapKey{
509+
key1 := channelMapKey{
505510
nodeKey: info.NodeKey1Bytes,
506511
chanID: chanID,
507-
}]
512+
}
513+
policy1 := channelMap[key1]
508514

509-
policy2 := channelMap[channelMapKey{
515+
key2 := channelMapKey{
510516
nodeKey: info.NodeKey2Bytes,
511517
chanID: chanID,
512-
}]
518+
}
519+
policy2 := channelMap[key2]
520+
521+
// We now create the cached edge policies, but
522+
// only when the above policies are found in the
523+
// `channelMap`.
524+
var (
525+
cachedPolicy1 *models.CachedEdgePolicy
526+
cachedPolicy2 *models.CachedEdgePolicy
527+
)
528+
529+
if policy1 != nil {
530+
cachedPolicy1 = models.NewCachedPolicy(
531+
policy1,
532+
)
533+
} else {
534+
log.Warnf("ChannelEdgePolicy not "+
535+
"found using %v", key1)
536+
}
537+
538+
if policy2 != nil {
539+
cachedPolicy2 = models.NewCachedPolicy(
540+
policy2,
541+
)
542+
} else {
543+
log.Warnf("ChannelEdgePolicy not "+
544+
"found using %v", key2)
545+
}
513546

514547
return cb(
515548
models.NewCachedEdge(&info),
516-
models.NewCachedPolicy(policy1),
517-
models.NewCachedPolicy(policy2),
549+
cachedPolicy1, cachedPolicy2,
518550
)
519551
},
520552
)

0 commit comments

Comments
 (0)