Repository navigation
kernel: don't use assert to handle invalid user input - #33943
stickies-v wants to merge 2 commits into
Conversation
Crashing on invalid user input is problematic for a library, e.g. this is impossible for FFI users to recover from, forcing them to re-implement the error handling logic client-side. Improve this by returning a -1 error code instead of crashing. Updates the C++ wrapper to throw a std::runtime_error for an invalid request, and adds test coverage.
Crashing on invalid user input is problematic for a library, e.g. this is impossible for FFI users to recover from, forcing them to re-implement the error handling logic client-side. Improve this by returning a nullptr instead of crashing. Updates the C++ wrapper to throw a std::out_of_bounds error for an invalid request, and adds test coverage.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33943. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. LLM Linter (✨ experimental)Possible places where named args may be used (e.g.
2025-11-25 |
| constexpr auto INVALID_REQUEST{-1}; | ||
| constexpr auto INVALID_SCRIPT{0}; | ||
| constexpr auto VALID_SCRIPT{1}; | ||
|
|
There was a problem hiding this comment.
I find this a bit inconsistent with this enum :
typedef uint8_t btck_ValidationMode;
#define btck_ValidationMode_VALID ((btck_ValidationMode)(0))
#define btck_ValidationMode_INVALID ((btck_ValidationMode)(1))
#define btck_ValidationMode_INTERNAL_ERROR ((btck_ValidationMode)(2))Where validationmode runs from 0 to 2 and 2 is the internal error.
Also isn't it better to expose this as an ENUM so that it can be re-used by the handlers ? (not sure because of the single use)
Or remove the int and use ScriptValidationFlag as return to be more in line with #33856 https://github.com/bitcoin/bitcoin/pull/33856/files#diff-8e83b7ea1d1a8ee1f496f2fda01436ba62b7ad926d1c61386a049d6ef392f71eR1052
EDIT: Kinda changed my view on this issue, "removed" option 3
There was a problem hiding this comment.
I think you're right that there is no strict consistency wrt return codes in the API, and that's also why I defined the status codes inside the function rather than in the header. A function-specific enum like btck_ScriptPubkeyVerifyResult could be an alternative, though. I didn't do it to minimize overhead, but am open to doing that if preferred.
|
Concept ACK on being more strict with using I think it makes more sense for a public API to sanitize user input and communicate issues with the input, allowing for recovery and leave usages of Regarding the approach: I like what is suggested in #33943 (comment) (exposing the enum and having it as the return value). I'm still thinking about whether to differentiate invalid requests with different error codes or just have one code for all as you did. Also, I remember at some point we had error codes instead of assertions for all cases of invalid user input in |
|
I'm Concept ACK on improving the behaviour around bad input, but not sure about adding more error handling surface to the API with the current approach. In my opinion the cases addressed are all programming errors, and should be treated as such. The argument that this duplicates the error handling code for the wrappers is not convincing to me and the PR itself as it currently stands provides the counter argument for it - an out of bounds case is still added and handled by the respective language wrapper. This seems sane to me, since every language will have its unique convention and way for handling these cases - from returning a variant to throwing an exception. Along these lines, I also don't understand why the change here should make cross-bindings tests easier. Might a better approach be adding a custom argument checker? The secp library has some that do both assertions and allow for a user-defined override. Personally, I think we'd be fine with just assertions, but a macro that can be defined to wrap a user-defined function through a function pointer seems useful for both debugging and providing a way to unify behaviour across tests. EDIT: |
|
Concept ACK on fixing input validation. Agree with @sedited here, not sure if we want to add more error handling to the API. I found the secp256k1 approach using |
| if (!is_valid_flag_combination(script_verify_flags::from_int(flags))) { | ||
| if (status) *status = btck_ScriptVerifyStatus_ERROR_INVALID_FLAGS_COMBINATION; | ||
| return 0; | ||
| return INVALID_SCRIPT; |
There was a problem hiding this comment.
You're also validating flags here. Wouldn't INVALID_REQUEST be more appropriate?
There was a problem hiding this comment.
I'm keeping the behaviour as-is as the default stance, but yes, I'm also not 100% sure on the boundary between INVALID_REQUEST and INVALID_SCRIPT here. I mention this at the end of the PR description, too. I'd like to understand that rationale better - perhaps @sedited can chime in?
| assert(output_index < tx.vout.size()); | ||
| if (output_index >= tx.vout.size()) { | ||
| LogError("output_index is out of bounds."); | ||
| return nullptr; |
There was a problem hiding this comment.
Noob question: How come you're not throwing directly in this function (throw std::out_of_range("output_index is out of bounds")?
There was a problem hiding this comment.
The API is in C, and C doesn't have exceptions, so we have to make sure no exceptions cross the C++/C boundary.
stickies-v
left a comment
There was a problem hiding this comment.
but not sure about adding more error handling surface to the API with the current approach
One design goal of this PR was to minimize increasing the error handling surface while achieving the goal of making these functions more testable, but it seems like the current solution is not striking the right balance. I'm converting this PR to draft while I'm exploring other options, including implementing (global or local) error/illegal callbacks similar to secp256k1 (but that would be a pretty big overhaul). Thank you for the quick reviews and feedback everyone.
the PR itself as it currently stands provides the counter argument for it - an out of bounds case is still added and handled by the respective language wrapper
I see your point, but I think there is a difference between a language wrapper converting a documented return value into an exception and the language wrapper having to duplicate the implementation details.
I also don't understand why the change here should make cross-bindings tests easier.
The C API itself should also be testable by the cross-bindings test suite, and on master it would just crash with an assertion error. Additionally, the cross-bindings test suite defines expected return values or exceptions (e.g. based on btck_ status codes), but assert is neither. Of course, the test suite could define its own exceptions/codes that are not part of the interface, but I think the tests should only cover the public interface. A language binding that covers the entire interface should be able to pass the tests without implementing additional logic. See also my comment here.
| assert(output_index < tx.vout.size()); | ||
| if (output_index >= tx.vout.size()) { | ||
| LogError("output_index is out of bounds."); | ||
| return nullptr; |
There was a problem hiding this comment.
The API is in C, and C doesn't have exceptions, so we have to make sure no exceptions cross the C++/C boundary.
| constexpr auto INVALID_REQUEST{-1}; | ||
| constexpr auto INVALID_SCRIPT{0}; | ||
| constexpr auto VALID_SCRIPT{1}; | ||
|
|
There was a problem hiding this comment.
I think you're right that there is no strict consistency wrt return codes in the API, and that's also why I defined the status codes inside the function rather than in the header. A function-specific enum like btck_ScriptPubkeyVerifyResult could be an alternative, though. I didn't do it to minimize overhead, but am open to doing that if preferred.
| if (!is_valid_flag_combination(script_verify_flags::from_int(flags))) { | ||
| if (status) *status = btck_ScriptVerifyStatus_ERROR_INVALID_FLAGS_COMBINATION; | ||
| return 0; | ||
| return INVALID_SCRIPT; |
There was a problem hiding this comment.
I'm keeping the behaviour as-is as the default stance, but yes, I'm also not 100% sure on the boundary between INVALID_REQUEST and INVALID_SCRIPT here. I mention this at the end of the PR description, too. I'd like to understand that rationale better - perhaps @sedited can chime in?
|
🐙 This pull request conflicts with the target branch and needs rebase. |
|
⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here?
|
|
Can this be closed per #35312 (comment) ? |
I kept this PR open to further investigate the solutions suggested here #33943 (comment), and while I still think it's useful to look at having error/bad input callbacks to improve the developer experience, it seems like it's currently a lot of work without too much impact. Closing. |
Crashing with
assertfor invalid user input is problematic for a library, e.g. this is impossible for FFI users to recover from, forcing them to re-implement the error handling logic client-side. See e.g. the go and .net bindings, or the discussion in stringintech/kernel-bindings-tests#5.This PR changes behaviour by:
btck_script_pubkey_verifyto return-1for invalid/malformed requests,0for invalid scripts, and1for valid scripts. Previously, the program would crash for the-1cases.nullptrwhen the request index is out of bounds. Previously, the program would crash in these cases.The wrapper is updated to throw
std::runtime_errororstd::out_of_boundserrors, and the tests are also updated.By not crashing, we can properly test for these edge cases in
kernel-bindings-teststo ensure that wrappers are handling them properly, as opposed to e.g. not differentiating between0and-1, which a naive (incorrect) implementation could do.Note: I'm not sure why
btck_ScriptVerifyStatus_ERROR_INVALID_FLAGS_COMBINATIONandbtck_ScriptVerifyStatus_ERROR_SPENT_OUTPUTS_REQUIREDare currently handled asINVALID_SCRIPT, in my view they should probably beINVALID_REQUEST(which meansbtck_ScriptVerifyStatusbecomes meaningless in its current form), but I've not implemented that yet as I'm not 100% sure. Happy to update that here, or can also be done in a separate pull later.