From 10aaa35db54901699b6e377f8a0ea1e89f33686c Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Sun, 31 Oct 2021 19:16:10 +0100 Subject: [PATCH 1/3] kvdb: add loggableKeyName method Safely logs the bucket key name as hex if it includes special characters. Co-authored-by: Oliver Gugger --- kvdb/bolt_compact.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/kvdb/bolt_compact.go b/kvdb/bolt_compact.go index 7da51b3d..e46c55f9 100644 --- a/kvdb/bolt_compact.go +++ b/kvdb/bolt_compact.go @@ -8,6 +8,7 @@ package kvdb import ( + "encoding/hex" "fmt" "os" "path" @@ -228,6 +229,30 @@ func (cmd *compacter) walk(db *bbolt.DB, walkFn walkFunc) error { }) } +// LoggableKeyName returns a printable name of the given key. +func LoggableKeyName(key []byte) string { + strKey := string(key) + if hasSpecialChars(strKey) { + return hex.EncodeToString(key) + } + + return strKey +} + +// hasSpecialChars returns true if any of the characters in the given string +// cannot be printed. +func hasSpecialChars(s string) bool { + for _, b := range s { + if !(b >= 'a' && b <= 'z') && !(b >= 'A' && b <= 'Z') && + !(b >= '0' && b <= '9') && b != '-' && b != '_' { + + return true + } + } + + return false +} + // walkBucket recursively walks through a bucket. func (cmd *compacter) walkBucket(b *bbolt.Bucket, keyPath [][]byte, k, v []byte, seq uint64, fn walkFunc) error { From 6be65856430ba11c2b64eda7dd3b9258e3770f9a Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Thu, 21 Oct 2021 22:54:48 +0200 Subject: [PATCH 2/3] kvdb/bolt_compact: filter out non-ASCII from bucket name before printing Printing the raw bucket name lead to weird characters and even sounds being printed. --- kvdb/bolt_compact.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kvdb/bolt_compact.go b/kvdb/bolt_compact.go index e46c55f9..72d470f9 100644 --- a/kvdb/bolt_compact.go +++ b/kvdb/bolt_compact.go @@ -220,7 +220,8 @@ func (cmd *compacter) walk(db *bbolt.DB, walkFn walkFunc) error { return tx.ForEach(func(name []byte, b *bbolt.Bucket) error { // This will log the top level buckets only to give the // user some sense of progress. - log.Debugf("Compacting top level bucket %s", name) + log.Debugf("Compacting top level bucket '%s'", + LoggableKeyName(name)) return cmd.walkBucket( b, nil, name, nil, b.Sequence(), walkFn, From 7162af7656e326f887c52f64fe412ddc6e8ba187 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Tue, 2 Nov 2021 08:53:54 +0100 Subject: [PATCH 3/3] docs: update release notes --- docs/release-notes/release-notes-0.14.0.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/release-notes/release-notes-0.14.0.md b/docs/release-notes/release-notes-0.14.0.md index 0412470f..4e920ee1 100644 --- a/docs/release-notes/release-notes-0.14.0.md +++ b/docs/release-notes/release-notes-0.14.0.md @@ -587,6 +587,9 @@ messages directly. There is no routing/path finding involved. * [Do not error log when an invoice that has been canceled and GC'd is expired]( https://github.com/lightningnetwork/lnd/pull/5913) +* [Don't print bucket names with special characters when compacting]( + https://github.com/lightningnetwork/lnd/pull/5878) + ## Documentation The [code contribution guidelines have been updated to mention the new