fuzz: use std::optional for sep_pos_opt variable #18901

pull brakmic wants to merge 1 commits into bitcoin:master from brakmic:warning-in-fuzz-tests changing 3 files +8 −8
  1. brakmic commented at 9:23 pm on May 6, 2020: contributor

    This PR changes the original size_t sep_pos to std::optional<size_t> sep_post_opt to remove the warning when compiling fuzz tests.

    0warning: variable 'sep_pos' may be uninitialized when used here [-Wconditional-uninitialized]
    

    Also, it adds --enable-c++17 flag to CI fuzz scripts.

  2. fanquake added the label Tests on May 6, 2020
  3. in src/test/fuzz/asmap_direct.cpp:17 in 7459b27f10 outdated
    13@@ -14,7 +14,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
    14 {
    15     // Encoding: [asmap using 1 bit / byte] 0xFF [addr using 1 bit / byte]
    16     bool have_sep = false;
    17-    size_t sep_pos;
    18+    size_t sep_pos = 0;
    


    promag commented at 11:14 pm on May 6, 2020:
    Could do size_t sep_pos = buffer.size(); and before usage do assert(sep_pos < buffer.size()).

    brakmic commented at 5:44 am on May 7, 2020:
    I took the recommended value from compiler warning message. But it seems that there is a much better solution to that proposed by @MarcoFalke
  4. sipa commented at 11:21 pm on May 6, 2020: member
    I dislike changes like this. The code is correct, and the compiler is just not smart enough to figure it out. Silencing the warning means it’d become harder to detect actual bugs that might be introduced later.
  5. MarcoFalke commented at 11:25 pm on May 6, 2020: member
    I wouldn’t mind setting --enable-c++17 on the fuzz tests and then simply using std::optional
  6. sipa commented at 11:27 pm on May 6, 2020: member
    @MarcoFalke Seems reasonable.
  7. brakmic commented at 5:45 am on May 7, 2020: contributor
    Is it OK to close this PR? @MarcoFalke
  8. MarcoFalke commented at 12:17 pm on May 7, 2020: member

    Oh, I was hoping you keep working on this. All you need to do is to switch the flag in the ci config for the fuzz test. See git grep '\--enable-c++1' ci

    Then #import <optional> and adjust the fix.

  9. brakmic commented at 12:19 pm on May 7, 2020: contributor

    Oh, I was hoping you keep working on this. All you need to do is to switch the flag in the ci config for the fuzz test. See git grep '\--enable-c++1' ci

    Then #import <optional> and adjust the fix.

    No problem, I’ll gladly continue working on it. :)

    My impression was, that you are going to introduce new flags and include std::optional which would render this solution obsolete.

  10. in src/test/fuzz/asmap_direct.cpp:17 in eec61880c9 outdated
    14@@ -14,7 +15,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
    15 {
    16     // Encoding: [asmap using 1 bit / byte] 0xFF [addr using 1 bit / byte]
    17     bool have_sep = false;
    


    MarcoFalke commented at 1:16 pm on May 7, 2020:
    can remove this now
  11. brakmic renamed this:
    fuzz: initialize variable sep_pos to get rid of warning
    fuzz: use std::optional for sep_pos variable
    on May 7, 2020
  12. MarcoFalke commented at 1:27 pm on May 7, 2020: member
    glanced over the code on GitHub for 5 seconds ACK 4f0a6d79c7efc950b263a91084fdef2354567f54
  13. brakmic commented at 1:31 pm on May 7, 2020: contributor

    glanced over the code on GitHub for 5 seconds ACK 4f0a6d7

    Thanks. Pushed again because forgot to include you as co-author.

  14. brakmic commented at 2:43 pm on May 7, 2020: contributor

    @MarcoFalke The two fuzz tests on travis seem to be working fine. No warnings related to sep_pos so far.

    However, there is another error message, from the fuzzer itself. Not related to this PR, actually, but worth looking into, I think:

    0 [#1](/bitcoin-bitcoin/1/) 0x556d615cabf0 in ArgsManager::ParseParameters(int, char const* const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) /home/travis/build/bitcoin/bitcoin/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/src/util/system.cpp:288:21
    
    0SUMMARY: UndefinedBehaviorSanitizer: nullptr-with-nonzero-offset /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/basic_string.h:527:69 in 
    1MS: 0 ; base unit: 0000000000000000000000000000000000000000
    20x24,0x5f,0x5b,0x5d,
    3$_[]
    4artifact_prefix='./'; Test unit written to ./crash-d4f298d1f9cf49b6e356887182b01050ec7ac17f
    5Base64: JF9bXQ==
    

    A false positive maybe?

  15. in src/test/fuzz/asmap_direct.cpp:17 in 38e94d6eea outdated
    14 void test_one_input(const std::vector<uint8_t>& buffer)
    15 {
    16     // Encoding: [asmap using 1 bit / byte] 0xFF [addr using 1 bit / byte]
    17-    bool have_sep = false;
    18-    size_t sep_pos;
    19+    std::optional<size_t> sep_pos = std::nullopt;
    


    sipa commented at 4:12 pm on May 7, 2020:
    Nit: this is the default constructor, so you can drop the = std::nullopt;.
  16. brakmic commented at 6:02 pm on May 7, 2020: contributor

    @MarcoFalke

    I think the UB problem has to do with this line of code in util/system.cpp

    0for (int i = 1; i < argc; i++) {
    1      std::string key(argv[i]);
    

    I’ll create a draft PR and test it. If it passes, I’ll open a proper PR.

    –EDIT: Currently compiling with fuzzers on my macOS. Let’s see if I can catch this error locally.

  17. practicalswift commented at 8:05 pm on May 7, 2020: contributor
    Is there any reason to go with std::optional instead of the expected Optional? :)
  18. in src/test/fuzz/asmap_direct.cpp:29 in 848ab4918b outdated
    30         }
    31     }
    32-    if (!have_sep) return; // Needs exactly 1 separator
    33-    if (buffer.size() - sep_pos - 1 > 128) return; // At most 128 bits in IP address
    34+    if (!sep_pos) return; // Needs exactly 1 separator
    35+    if (buffer.size() - sep_pos.value() - 1 > 128) return; // At most 128 bits in IP address
    


    MarcoFalke commented at 10:16 pm on May 8, 2020:

    for a smaller diff you could rename the optional to sep_pos_opt and insert before this line

    0const size_t sep_pos{sep_pos_opt.value()};
    
  19. MarcoFalke approved
  20. MarcoFalke commented at 10:18 pm on May 8, 2020: member

    ACK 848ab4918b88f9095c49ed7aaa9c12558b1fdda3

    Seems reasonable to switch to C++17 for the fuzzers. Locally I am already running them on C++17, and the project is going to switch in about 4-5 months as well. So I don’t see the need to additionally burden the fuzz tests with the heavy boost headers temporarily and then make an effort to pull them out again.

  21. brakmic renamed this:
    fuzz: use std::optional for sep_pos variable
    fuzz: use std::optional for sep_pos_opt variable
    on May 8, 2020
  22. fuzz: use std::optional for sep_pos variable
    Co-authored-by: MarcoFalke <falke.marco@gmail.com>
    420fa0770f
  23. in src/test/fuzz/asmap_direct.cpp:29 in c3f39279c0 outdated
    30             return;
    31         }
    32     }
    33-    if (!have_sep) return; // Needs exactly 1 separator
    34+    const size_t sep_pos{sep_pos_opt.value()};
    35+    if (!sep_pos) return; // Needs exactly 1 separator
    


    MarcoFalke commented at 11:59 pm on May 8, 2020:

    The two lines are swapped, no?

    0    if (!sep_pos_opt) return; // Needs exactly 1 separator
    

    brakmic commented at 8:01 am on May 9, 2020:

    Yeah, these errors happen when you have no working compiler on your machine. Can’t compile fuzzing variants anymore (Catalina is not an OS but Malware). It’s time for Linux VM.

    Sorry!

    –EDIT: it works now, somehow.

  24. practicalswift commented at 10:05 am on May 9, 2020: contributor
    ACK 420fa0770f37619bfa29898d59dac45b6a477abb
  25. MarcoFalke commented at 11:30 am on May 9, 2020: member
    ACK 420fa07
  26. MarcoFalke merged this on May 9, 2020
  27. MarcoFalke closed this on May 9, 2020

  28. MarcoFalke referenced this in commit f71a3a8829 on May 11, 2020
  29. Fabcien referenced this in commit 04e0fd2b3e on Jan 26, 2021
  30. DrahtBot locked this on Feb 15, 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-12-27 09:12 UTC

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