Add attribute [[noreturn]] (C++11) to functions that will not return #10843

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:noreturn changing 2 files +6 −6
  1. practicalswift commented at 1:07 pm on July 16, 2017: contributor

    Add attribute [[noreturn]] (C++11) to functions that will not return.

    Rationale:

    • Reduce the number of false positives/false negatives from static analyzers with regards to things such as unused or unreachable code
    • Potentially enable additional compiler optimizations
  2. practicalswift force-pushed on Jul 16, 2017
  3. practicalswift force-pushed on Jul 16, 2017
  4. practicalswift force-pushed on Jul 16, 2017
  5. in src/random.cpp:45 in 157c2d5f6c outdated
    38@@ -39,7 +39,7 @@
    39 #include <openssl/err.h>
    40 #include <openssl/rand.h>
    41 
    42-static void RandFailure()
    43+[[noreturn]] static void RandFailure()
    44 {
    45     LogPrintf("Failed to read randomness, aborting\n");
    46     abort();
    


    TheBlueMatt commented at 6:39 pm on July 16, 2017:
    Maybe change this to std::abort() so that its clearer that we’re really [[noreturn]]ing?

    practicalswift commented at 6:54 pm on July 16, 2017:
    Good point! Fixed! :-)
  6. practicalswift force-pushed on Jul 16, 2017
  7. sipa commented at 6:57 pm on July 16, 2017: member
    utACK 2e737cf996336fbf74ceb56d3f105fd7fcc13a3b
  8. gmaxwell approved
  9. gmaxwell commented at 4:01 am on July 17, 2017: contributor
    utACK
  10. paveljanik commented at 8:56 am on July 17, 2017: contributor
    ACK 2e737cf
  11. laanwj commented at 1:03 pm on July 17, 2017: member
    The PR description mention ‘functions’ where this changes only one function. Is that intentional, or did you miss something while pushing?
  12. gmaxwell commented at 1:22 pm on July 17, 2017: contributor
    There were more before…
  13. practicalswift commented at 1:27 pm on July 17, 2017: contributor

    @laanwj Yes, originally I aimed to add the [[noreturn]] attribute also to:

    • BitcoinApplication::handleRunawayException(const QString &message) (in qt/bitcoin.cpp)
    • Shutdown()/StartShutdown() (in test/test_bitcoin_main.cpp)

    But I ran into some compilation issues that I didn’t have time to follow up on, so I excluded them from this PR.

  14. practicalswift force-pushed on Jul 17, 2017
  15. Add attribute [[noreturn]] (C++11) to functions that will not return
    Rationale:
    * Reduce the number of false positives from static analyzers
    * Potentially enable additional compiler optimizations
    b82c55af78
  16. in src/qt/bitcoin.cpp:525 in b40017f227 outdated
    518@@ -519,7 +519,7 @@ void BitcoinApplication::shutdownResult()
    519     quit(); // Exit main loop after shutdown finished
    520 }
    521 
    522-void BitcoinApplication::handleRunawayException(const QString &message)
    523+[[noreturn]] void BitcoinApplication::handleRunawayException(const QString &message)
    524 {
    525     QMessageBox::critical(0, "Runaway exception", BitcoinGUI::tr("A fatal error occurred. Bitcoin can no longer continue safely and will quit.") + QString("\n\n") + message);
    526     ::exit(EXIT_FAILURE);
    


    TheBlueMatt commented at 3:11 pm on July 17, 2017:
    std::exit?
  17. practicalswift force-pushed on Jul 17, 2017
  18. practicalswift commented at 4:55 pm on July 17, 2017: contributor

    I’ve now added the [[noreturn]] attribute to Shutdown()/StartShutdown() (in test/test_bitcoin_main.cpp).

    However, after some digging it appears that [[noreturn]] might not be correct for BitcoinApplication::handleRunawayException(const QString &message). If I understand it correctly it calls QCoreApplication::exit(…) which does return to the caller (it is event processing that stops).

    This PR should now be ready for review.

  19. laanwj commented at 6:12 pm on July 17, 2017: member

    If I understand it correctly it calls QCoreApplication::exit(…) which does return to the caller (it is event processing that stops).

    Agreed - if you’re not 100% sure that a function doesn’t return, it’s much better to err on the safe side and not specify it. From experience, functions with noreturn returning can result in some awful things, which might even be security critical (but at the least hard to debug), as it can fall through to apparently random code.

  20. TheBlueMatt commented at 7:19 pm on July 17, 2017: member
    @practicalswift I’m confused, then, BitcoinApplication::handleRunawayException ends by calling ::exit (the global exit, not QCoreApplication::exit, no?)?
  21. practicalswift force-pushed on Jul 17, 2017
  22. practicalswift force-pushed on Jul 17, 2017
  23. practicalswift force-pushed on Jul 17, 2017
  24. practicalswift commented at 11:48 pm on July 17, 2017: contributor

    @TheBlueMatt You’re absolutely right! I was wrong about QCoreApplication::exit(…) being called. Sorry :-)

    After applying 536504c8bef776902fa071dd76a37ef7037c1945 make check passes for all build configurations except HOST=x86_64-apple-darwin11, see https://travis-ci.org/bitcoin/bitcoin/builds/254651379. I don’t understand why that is.

  25. theuni commented at 0:57 am on July 18, 2017: member
    The attribute needs to be on the declaration rather than the definition.
  26. practicalswift force-pushed on Jul 18, 2017
  27. practicalswift commented at 3:58 am on July 18, 2017: contributor

    @theuni Yes, I thought so but when applying d52e345a6e7526f516425b7547dbca534a884172 I got the parsing error qt/bitcoin.cpp:[…]: Parse error at ";" for all Qt-enabled builds, see https://travis-ci.org/bitcoin/bitcoin/jobs/254702626#L2290.

    0$ make V=1
    1[…]
    2QT_SELECT=qt5 /usr/lib/x86_64-linux-gnu/qt5/bin/moc -I. -I../src/config -I/usr/include/x86_64-linux-gnu/qt5/QtNetwork -I/usr/include/x86_64-linux-gnu/qt5 -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets -I/usr/include/x86_64-linux-gnu/qt5 -I/usr/include/x86_64-linux-gnu/qt5/QtGui -I/usr/include/x86_64-linux-gnu/qt5 -I/usr/include/x86_64-linux-gnu/qt5/QtCore -I/usr/include/x86_64-linux-gnu/qt5 -DHAVE_CONFIG_H -I. qt/bitcoin.cpp | \
    3  /bin/sed -e '/^\*\*.*Created:/d' -e '/^\*\*.*by:/d' > qt/bitcoin.moc
    4qt/bitcoin.cpp:233: Parse error at ";"
    5[…]
    6$ wc -c src/qt/bitcoin.moc
    70 src/qt/bitcoin.moc
    8$ /usr/lib/x86_64-linux-gnu/qt5/bin/moc --version
    9moc 5.5.1
    

    Qt:s moc being confused by the [[noreturn]] attribute? :-)

  28. practicalswift force-pushed on Jul 18, 2017
  29. practicalswift commented at 3:13 pm on July 18, 2017: contributor

    I’ve now excluded the commit touching handleRunawayException(…) (src/qt/bitcoin.cpp). Qt’s moc is doing too much stuff behind the scenes for me to be comfortable doing that change :-)

    This PR should now be ready for review.

  30. TheBlueMatt commented at 8:47 pm on July 19, 2017: member
    utACK b82c55af78738258b56bd8fe7b5f8d5ccf85f832
  31. kallewoof approved
  32. laanwj added the label Refactoring on Aug 22, 2017
  33. laanwj merged this on Aug 22, 2017
  34. laanwj closed this on Aug 22, 2017

  35. laanwj referenced this in commit 2ab7c6300f on Aug 22, 2017
  36. PastaPastaPasta referenced this in commit 099cde54fd on Sep 19, 2019
  37. PastaPastaPasta referenced this in commit 905335b2bc on Sep 23, 2019
  38. PastaPastaPasta referenced this in commit e5768d0aa5 on Sep 24, 2019
  39. PastaPastaPasta referenced this in commit 0456536436 on Nov 19, 2019
  40. PastaPastaPasta referenced this in commit b354482baa on Nov 21, 2019
  41. PastaPastaPasta referenced this in commit 9fb8c96426 on Dec 9, 2019
  42. codablock referenced this in commit fbe7be5ab9 on Dec 31, 2019
  43. PastaPastaPasta referenced this in commit 2c6b4454f4 on Jan 1, 2020
  44. PastaPastaPasta referenced this in commit e057a2160e on Jan 1, 2020
  45. PastaPastaPasta referenced this in commit cfb403ec45 on Jan 1, 2020
  46. PastaPastaPasta referenced this in commit 9d983bf0fc on Jan 2, 2020
  47. PastaPastaPasta referenced this in commit 8e0a02f629 on Jan 2, 2020
  48. PastaPastaPasta referenced this in commit 1862912676 on Jan 2, 2020
  49. PastaPastaPasta referenced this in commit 6abfbd9997 on Jan 2, 2020
  50. PastaPastaPasta referenced this in commit 5c003edbb9 on Jan 2, 2020
  51. PastaPastaPasta referenced this in commit 6f3620898b on Jan 2, 2020
  52. PastaPastaPasta referenced this in commit 87b1473a73 on Jan 2, 2020
  53. PastaPastaPasta referenced this in commit c15a442663 on Jan 3, 2020
  54. PastaPastaPasta referenced this in commit 98ddfb98d3 on Jan 10, 2020
  55. ckti referenced this in commit e121984438 on Mar 28, 2021
  56. practicalswift deleted the branch on Apr 10, 2021
  57. fanquake referenced this in commit 88331aa8a7 on Apr 13, 2021
  58. sidhujag referenced this in commit 83f9e04d38 on Apr 13, 2021
  59. PastaPastaPasta referenced this in commit 045abe2b67 on Jun 27, 2021
  60. PastaPastaPasta referenced this in commit 3c9b783b9d on Jun 28, 2021
  61. PastaPastaPasta referenced this in commit deeb2c6369 on Jun 29, 2021
  62. PastaPastaPasta referenced this in commit 53b425915e on Jul 1, 2021
  63. PastaPastaPasta referenced this in commit f1508881f9 on Jul 1, 2021
  64. PastaPastaPasta referenced this in commit eb145dc79b on Jul 15, 2021
  65. PastaPastaPasta referenced this in commit f94e5391ae on Jul 15, 2021
  66. PastaPastaPasta referenced this in commit 499fb6c18b on Jul 16, 2021
  67. gades referenced this in commit 509d8cd682 on Jan 29, 2022
  68. DrahtBot locked this on Aug 16, 2022

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-10-06 16:12 UTC

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