kernel: document that btck_WriteBytes may receive a null pointer #36418

Manually merged
alexanderwiederin merged 1 commit from kernel-writebytes-nonnull into master 2026-10-07 20:26:00 +00:00 AGit
alexanderwiederin commented 2026-10-02 14:22:29 +00:00 (Migrated from github.com)

The header does not say whether the bytes pointer can be null when size is zero. It can: btck_witness_stack_get_item_at passes std::vector::data() straight through, and that may be null for an empty element.

An alternative is to guarantee non-null rather than document that it isn't. In other words, route the vector-backed writer calls through a helper that substitutes a valid, never-read pointer when the buffer is empty.

The header does not say whether the bytes pointer can be null when size is zero. It can: `btck_witness_stack_get_item_at` passes `std::vector::data()` straight through, and that may be null for an empty element. An alternative is to guarantee non-null rather than document that it isn't. In other words, route the vector-backed writer calls through a helper that substitutes a valid, never-read pointer when the buffer is empty.
DrahtBot commented 2026-10-02 14:22:37 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK stickies-v, sedited

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36418 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36418.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36418#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [stickies-v](https://github.com/bitcoin/bitcoin/pull/36418#issuecomment-6043514414), [sedited](https://github.com/bitcoin/bitcoin/pull/36418#pullrequestreview-5447836026) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

Base: 9dfde64cc3262329051fd05fffe40eecc786a99f
Head: b497a71c6630fe37f25620f9f08f1c52f9d4d558

Findings

Minor

The in-tree callback is unsafe for the documented null case (src/kernel/bitcoinkernel.h:394, head)

When bytes is null and size is zero, the in-tree C++ callback in src/kernel/bitcoinkernel_wrapper.h computes first + len before inserting the range. Even adding zero to a null pointer is undefined behavior. ScriptPubkey::ToBytes() reaches this callback through btck_script_pubkey_to_bytes, and empty scripts are supported. The existing empty_script test exercises this path but does not detect the undefined pointer arithmetic. Return from the callback when len == 0, or otherwise handle the null case without pointer arithmetic. No non-test caller of this convenience method was identified, so production impact is unconfirmed.

Review coverage was incomplete. adversarial_glm: Output failed validation: Finding must identify a positive line

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `9dfde64cc3262329051fd05fffe40eecc786a99f` Head: `b497a71c6630fe37f25620f9f08f1c52f9d4d558` ##### Findings **Minor** **The in-tree callback is unsafe for the documented null case** (src/kernel/bitcoinkernel.h:394, head) When `bytes` is null and `size` is zero, the in-tree C++ callback in `src/kernel/bitcoinkernel_wrapper.h` computes `first + len` before inserting the range. Even adding zero to a null pointer is undefined behavior. `ScriptPubkey::ToBytes()` reaches this callback through `btck_script_pubkey_to_bytes`, and empty scripts are supported. The existing `empty_script` test exercises this path but does not detect the undefined pointer arithmetic. Return from the callback when `len == 0`, or otherwise handle the null case without pointer arithmetic. No non-test caller of this convenience method was identified, so production impact is unconfirmed. Review coverage was incomplete. adversarial_glm: Output failed validation: Finding must identify a positive line [Full review report](<https://review.fish.foo/traces/36418-b497a71c6630fe37f25620f9f08f1c52f9d4d558-537-2.html>)
sedited (Migrated from github.com) approved these changes 2026-10-06 10:52:18 +00:00
sedited (Migrated from github.com) left a comment

ACK 76359bee61

Documenting this seems like the correct call, given that developers can, and should probably, already check if the size is zero.

ACK 76359bee61edb2062493f0ba4ecd47943c359e0a Documenting this seems like the correct call, given that developers can, and should probably, already check if the size is zero.
stickies-v (Migrated from github.com) reviewed 2026-10-07 15:39:35 +00:00
stickies-v (Migrated from github.com) left a comment

Approach ACK, documenting seems like the best choice now.

Approach ACK, documenting seems like the best choice now.
stickies-v (Migrated from github.com) commented 2026-10-07 15:36:38 +00:00

The commit message in 76359be says that this is relevant only to bindings, but I think it also matters to C users, for example when they give bytes to memcpy. I suggest this more general alternative:

kernel: document that btck_WriteBytes may receive a null pointer

btck_witness_stack_get_item_at passes std::vector::data() to the writer,
which can be null for an empty witness item. Document this in the contract,
together with the other btck_WriteBytes parameters.
The commit message in 76359be says that this is relevant only to bindings, but I think it also matters to C users, for example when they give `bytes` to `memcpy`. I suggest this more general alternative: ``` kernel: document that btck_WriteBytes may receive a null pointer btck_witness_stack_get_item_at passes std::vector::data() to the writer, which can be null for an empty witness item. Document this in the contract, together with the other btck_WriteBytes parameters. ```
stickies-v (Migrated from github.com) commented 2026-10-07 15:33:44 +00:00

Documenting how callers should deal with this seems out of scope, would just describe behaviour. Let's also use more complete Doxygen syntax while we're at it.

git diff on 76359bee61
diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h
index ba6daf72fd..0ada5576a9 100644
--- a/src/kernel/bitcoinkernel.h
+++ b/src/kernel/bitcoinkernel.h
@@ -391,11 +391,11 @@ typedef void (*btck_ValidationInterfaceBlockDisconnected)(void* user_data, btck_
 /**
  * Function signature for serializing data.
  *
- * The bytes pointer may be null when size is zero. Callers that construct a
- * language-level slice or array view from the arguments must handle that
- * case, as some languages require a non-null pointer even for an empty view.
- *
- * Returns 0 to indicate success.
+ * @param[in] bytes    The data to write. Can be null if size is 0.
+ * @param[in] size     The number of bytes in the data.
+ * @param[in] userdata Holds the user-defined opaque structure that was given
+ *                     with the writer.
+ * @return             0 on success.
  */
 typedef int (*btck_WriteBytes)(const void* bytes, size_t size, void* userdata);
 

Documenting how callers should deal with this seems out of scope, would just describe behaviour. Let's also use more complete Doxygen syntax while we're at it. <details> <summary>git diff on 76359bee61</summary> ```diff diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h index ba6daf72fd..0ada5576a9 100644 --- a/src/kernel/bitcoinkernel.h +++ b/src/kernel/bitcoinkernel.h @@ -391,11 +391,11 @@ typedef void (*btck_ValidationInterfaceBlockDisconnected)(void* user_data, btck_ /** * Function signature for serializing data. * - * The bytes pointer may be null when size is zero. Callers that construct a - * language-level slice or array view from the arguments must handle that - * case, as some languages require a non-null pointer even for an empty view. - * - * Returns 0 to indicate success. + * @param[in] bytes The data to write. Can be null if size is 0. + * @param[in] size The number of bytes in the data. + * @param[in] userdata Holds the user-defined opaque structure that was given + * with the writer. + * @return 0 on success. */ typedef int (*btck_WriteBytes)(const void* bytes, size_t size, void* userdata); ``` </details>
alexanderwiederin (Migrated from github.com) reviewed 2026-10-07 17:31:40 +00:00
alexanderwiederin (Migrated from github.com) commented 2026-10-07 17:31:40 +00:00

Taken - thanks!

Taken - thanks!
alexanderwiederin (Migrated from github.com) reviewed 2026-10-07 17:31:48 +00:00
alexanderwiederin (Migrated from github.com) commented 2026-10-07 17:31:48 +00:00

Taken - thanks!

Taken - thanks!
stickies-v commented 2026-10-07 17:45:13 +00:00 (Migrated from github.com)
ACK b497a71c6630fe37f25620f9f08f1c52f9d4d558
sedited (Migrated from github.com) approved these changes 2026-10-07 20:17:59 +00:00
sedited (Migrated from github.com) left a comment
ACK b497a71c6630fe37f25620f9f08f1c52f9d4d558
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36418
No description provided.