-
Notifications
You must be signed in to change notification settings - Fork 38.7k
fuzz: replace hardcoded numbers for bech32 limits #30596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,8 +29,9 @@ FUZZ_TARGET(bech32) | |
| std::vector<unsigned char> input; | ||
| ConvertBits<8, 5, true>([&](unsigned char c) { input.push_back(c); }, buffer.begin(), buffer.end()); | ||
|
|
||
| if (input.size() + 3 + 6 <= 90) { | ||
| // If it's possible to encode input in Bech32(m) without exceeding the 90-character limit: | ||
| // Input data part + 3 characters for the HRP and separator (bc1) + the checksum characters | ||
| if (input.size() + 3 + bech32::CHECKSUM_SIZE <= bech32::CharLimit::BECH32) { | ||
glozow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // If it's possible to encode input in Bech32(m) without exceeding the bech32-character limit: | ||
| for (auto encoding : {bech32::Encoding::BECH32, bech32::Encoding::BECH32M}) { | ||
| const std::string encoded = bech32::Encode(encoding, "bc", input); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. according to the bip the hrp
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree that fuzzing for more than just the hard-coded "bc" is ideal, but probably better as a separate follow-up.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you working on this or should I do it?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Up for grabs! Feel free to ping me for review.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in #30623 (comment) |
||
| assert(!encoded.empty()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: would it make sense to move these to the header as well - and make them static constexpr as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, I don't think static makes a difference here since we aren't talking about a class member? Regarding
CHARSETandCHARSET_REV, what's the advantage of having them in the header file if we aren't using them outside of bech32.cpp?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on other usages in the code,
static constexprin headers is good practice to prevent potential linking issues and ensure each translation unit has its own independent copy. Let me know if I'm wrong here.Separating constants and definitions into the header, leaving implementations in the cpp, i.e. separation of concerns.
If you think this is outside the scope, I can accept that, thanks for considering my suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josibake The keyword
staticin C++ has two completely different meanings sadly 😠Inside a class, a
staticvariable/function makes it a class member rather than an instance member (shared by all instances).Outside a class,
staticmeans that a variable/function is only accessible within its compilation unit (whose effect is identical to placing it in an anonymousnamespace).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sipa , @paplorinc thanks for the explanation! In this case, since we are in a named namespace, it seems the static keyword would be preferable? Or doesn't matter? Will do some more reading on this cause I'm not sure what the best practice is here.
This is another one I'm not sure about: in my mind,
CHARSETis internal to bech32 hence why it makes sense to keep it in bech32.cpp. But I suppose you could also argue the char set is a definition (from BIP173), so it should be in the header. Either way, I think it's out of scope for this PR, but thought it was an interesting point.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I add this to #30623 (comment) as well?