Remove unnecessary null pointer checks #7432

issue elfring opened this issue on January 28, 2016
  1. elfring commented at 7:13 AM on January 28, 2016: none
  2. jonasschnelli added the label Refactoring on Jan 28, 2016
  3. jonasschnelli commented at 7:45 AM on January 28, 2016: contributor

    At least a if NULL check does not hurt. But I agree. Feel free do open a PR.

  4. elfring commented at 7:50 AM on January 28, 2016: none

    Would you also like to update another software library?

  5. laanwj commented at 9:27 AM on January 28, 2016: member

    In Bitcoin Core we believe in belt-and-suspenders checks. A small change to the code can turn a 'unnecessary' NULL pointer check into the only thing that prevents a horrible security issue. Checking, even though not strictly necessary, may add some robustness to the API.

    Also it may be hard to prove that a check is unnecessary, evaluating the code paths is a lot of review work while there are tons of more serious issues.

    So let's just leave this as it is. If you can quantify that it saves time in a critical path (say an inner loop that is executed millions of times), feel free to remove an unnecessary check, but don't do so indiscriminately.

  6. elfring commented at 9:42 AM on January 28, 2016: none

    Checking, even though not strictly necessary, may add some robustness to the API.

    Do you imagine any special case for the eventual handling of C++ compilers with a standard-incompliant implementation of the delete operator?

  7. laanwj commented at 10:15 AM on January 28, 2016: member

    Sure, if you encounter the code

    if (x)
        delete x;
    

    Then, according to the C++ standard, you can replace it with

    delete x;
    

    But don't do so if there is more code in the if ().

  8. laanwj closed this on Jan 28, 2016

  9. elfring commented at 10:19 AM on January 28, 2016: none

    Why did you close this issue already?

  10. laanwj commented at 10:26 AM on January 28, 2016: member

    I don't see this as a serious issue. A few extra checks won't cause bugs, nor (likely) affect performance.

  11. elfring commented at 10:30 AM on January 28, 2016: none

    I suggested just another small software optimisation here.

  12. jonasschnelli commented at 10:35 AM on January 28, 2016: contributor

    Please no bikeshedding on avoidable pointer checks. No need to create an issue for that (create a PR instead if you want to improve things).

  13. MarcoFalke commented at 6:47 PM on January 28, 2016: member

    Isn't the compiler taking care of such?

  14. MarcoFalke locked this on Sep 8, 2021

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: 2026-04-22 06:15 UTC

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