refactor: Enforce C-str fmt strings in WalletLogPrintf() #28237

pull MarcoFalke wants to merge 4 commits into bitcoin:master from MarcoFalke:2308-wallet-c-str- changing 8 files +37 −19
  1. MarcoFalke commented at 7:16 am on August 8, 2023: member

    All fmt functions only accept a raw C-string as argument.

    There should never be a need to pass a format string that is not a compile-time string literal, so disallow it in WalletLogPrintf() to avoid accidentally introducing it.

    Apart from consistency, this also fixes the clang-tidy plugin bug #26296 (review).

  2. doc: Fix bitcoin-unterminated-logprintf tidy comments
    * Move module description from test to LogPrintfCheck
    * Add test doc
    * Remove unused comment, see https://github.com/bitcoin/bitcoin/pull/26296/files#r1279351539
    fa244f3321
  3. DrahtBot commented at 7:16 am on August 8, 2023: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK theuni

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #27277 (Move log messages: tx enqueue to mempool, allocation to blockstorage by Sjors)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  4. DrahtBot renamed this:
    refactor: Enforce C-str fmt strings in WalletLogPrintf()
    refactor: Enforce C-str fmt strings in WalletLogPrintf()
    on Aug 8, 2023
  5. DrahtBot added the label Refactoring on Aug 8, 2023
  6. MarcoFalke force-pushed on Aug 8, 2023
  7. DrahtBot added the label CI failed on Aug 8, 2023
  8. MarcoFalke force-pushed on Aug 8, 2023
  9. refactor: Enforce C-str fmt strings in WalletLogPrintf() fa6dc57760
  10. MarcoFalke force-pushed on Aug 8, 2023
  11. DrahtBot removed the label CI failed on Aug 8, 2023
  12. in src/wallet/wallet.cpp:2322 in fa6dc57760 outdated
    2318@@ -2319,7 +2319,7 @@ OutputType CWallet::TransactionChangeType(const std::optional<OutputType>& chang
    2319 void CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm)
    2320 {
    2321     LOCK(cs_wallet);
    2322-    WalletLogPrintf("CommitTransaction:\n%s", tx->ToString());
    2323+    WalletLogPrintf("CommitTransaction:\n%s", tx->ToString()); // NOLINT(bitcoin-unterminated-logprintf)
    


    theuni commented at 4:29 pm on August 8, 2023:
    Hmm, this one wasn’t being caught before?

    MarcoFalke commented at 4:57 pm on August 8, 2023:
    Yes, see #26296 (review)

    theuni commented at 5:52 pm on August 8, 2023:

    Hmm.

    I haven’t tried this using the c-i scripts, but running clang-tidy manually against current master, this works fine for me as-is:

    0$ /opt/clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang-tidy-16 --load=/home/cory/dev/bitcoin2/contrib/devtools/bitcoin-tidy/build2/libbitcoin-tidy.so -checks="-*,bitcoin-*" wallet/wallet.cpp -- -DHAVE_BUILD_INFO -I. -I./minisketch/include -I./secp256k1/include -I./univalue/include -I./leveldb/include -isystem /home/cory/dev/bitcoin2/depends/x86_64-pc-linux-gnu/include -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION -DBOOST_NO_CXX98_FUNCTION_BASE -DHAVE_CONFIG_H  -std=c++17
    11 warning generated.
    2/home/cory/dev/bitcoin2/src/wallet/wallet.cpp:2322:21: error: Unterminated format string used with LogPrintf [bitcoin-unterminated-logprintf,-warnings-as-errors]
    3    WalletLogPrintf("CommitTransaction:\n%s", tx->ToString());
    4                    ^
    5                                           \n
    61 warning treated as error
    

    Any idea what the difference is?


    MarcoFalke commented at 11:46 am on August 16, 2023:

    @theuni You’ll have to share exact steps to reproduce, ideally starting from a fresh install of you operating system. Otherwise it will be close to impossible to reproduce. Locally and on CI this isn’t caught (otherwise CI would be red). c.f

    0$ (cd src/ &&  clang-tidy --load=/home/fake/bitcoin/contrib/devtools/bitcoin-tidy/build/libbitcoin-tidy.so -checks="-*,bitcoin-*" wallet/wallet.cpp ) 
    112 warnings generated.
    2Suppressed 12 warnings (12 in non-user code).
    3Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
    

    theuni commented at 1:42 pm on August 16, 2023:

    Huh. For whatever reason, my clang-tidy-16 acts as if IgnoreUnlessSpelledInSource is set. That’s weird.

    It came from here: https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.0/clang+llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz

    I can’t reproduce with clang-tidy-15. clang-query-16 only matches if traversal is set to IgnoreUnlessSpelledInSource.

    $ /opt/clang+llvm-15.0.2-x86_64-unknown-linux-gnu/bin/clang-query wallet/wallet.cpp – -DHAVE_BUILD_INFO -I. -I./minisketch/include -I./secp256k1/include -I./univalue/include -I./leveldb/include -isystem /home/cory/dev/bitcoin2/depends/x86_64-pc-linux-gnu/include -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION -DBOOST_NO_CXX98_FUNCTION_BASE -DHAVE_CONFIG_H -std=c++17 -O2 clang-query> m cxxMemberCallExpr(thisPointerType(qualType(hasDeclaration(cxxRecordDecl(hasName(“CWallet”))))),callee(cxxMethodDecl(hasName(“WalletLogPrintf”))),hasArgument(0, stringLiteral().bind(“logstring”))) 0 matches. clang-query> set traversal IgnoreUnlessSpelledInSource clang-query> m cxxMemberCallExpr(thisPointerType(qualType(hasDeclaration(cxxRecordDecl(hasName(“CWallet”))))),callee(cxxMethodDecl(hasName(“WalletLogPrintf”))),hasArgument(0, stringLiteral().bind(“logstring”)))

    Match #33:

    /home/cory/dev/bitcoin2/src/wallet/wallet.cpp:4149:36: note: “logstring” binds here wallet.WalletLogPrintf(“Making a new watchonly wallet containing the unwatched solvable scripts\n”); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/cory/dev/bitcoin2/src/wallet/wallet.cpp:4149:13: note: “root” binds here wallet.WalletLogPrintf(“Making a new watchonly wallet containing the unwatched solvable scripts\n”); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 33 matches

    I don’t think this is worth spending any more time on, but I still don’t understand why it’s different.

  13. in src/wallet/wallet.h:894 in fa6dc57760 outdated
    889@@ -890,9 +890,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
    890     };
    891 
    892     /** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
    893-    template<typename... Params>
    894-    void WalletLogPrintf(std::string fmt, Params... parameters) const {
    895-        LogPrintf(("%s " + fmt).c_str(), GetDisplayName(), parameters...);
    896+    template <typename... Params>
    897+    void WalletLogPrintf(const char* fmt, Params... parameters) const
    


    theuni commented at 4:33 pm on August 8, 2023:
    I was going to suggest the nonnull attribute here, but I believe our tidy check actually enforces that :)

    MarcoFalke commented at 4:56 pm on August 8, 2023:
    I hope that the tests (and reviewers) will catch a LogPrint(nullptr, 1, 2, 3). But adding nonnull can be done in a follow-up, if needed, for all log-functions.
  14. theuni commented at 4:35 pm on August 8, 2023: member

    Makes sense to me. I almost did the same thing while poking at WalletLogPrintf for the sake of easier static analysis.

    Am I missing some obvious reason though, other than “might as well”?

  15. MarcoFalke commented at 5:00 pm on August 8, 2023: member

    Am I missing some obvious reason though, other than “might as well”?

    Edited OP to list both reasons.

  16. refactor: Enable all clang-tidy plugin bitcoin tests
    This makes it easier to add new ones without having to modify this file
    every time.
    faa11434fe
  17. bitcoin-tidy: Apply bitcoin-unterminated-logprintf to spkm as well fa60fa3b0c
  18. theuni commented at 1:45 pm on August 16, 2023: member
    ACK fa60fa3b0cba4a30726af8e0e9d1e84e14849eda
  19. fanquake merged this on Aug 18, 2023
  20. fanquake closed this on Aug 18, 2023

  21. MarcoFalke deleted the branch on Aug 18, 2023

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-06-29 07:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me