silentpayments: allow NULL arrays in scan_outputs if n_tx_outputs is 0
What changed, and why it matters
This commit fixes a minor API usability issue in the experimental silent payments module. Previously, a function that scans transaction outputs for silent payments would crash if given empty arrays represented as NULL pointers, even though it did not actually need to read any data. The change allows NULL pointers when the number of outputs is zero, matching how similar functions already behave. It is a defensive hardening fix rather than an active security vulnerability.
No urgent action required. Treat as routine defensive fix. If using the silent payments module, ensure the updated API contract is followed and consider updating bindings to allow NULL empty arrays.
Security signals we found
NULL pointer argument check relaxed for empty arrays
SECP256K1_ARG_NONNULL attributes removed for two parameters
New API test verifying NULL-with-zero-length behavior
Matches existing pattern in prevouts_summary_create and sender_create_outputs
Evidence from the diff
secp256k1_silentpayments_recipient_scan_outputs previously enforced ARG_CHECK non-NULL on found_outputs and tx_outputs unconditionally, and declared them SECP256K1_ARG_NONNULL. When n_tx_outputs is 0, neither array is dereferenced, so a caller passing an empty std::vector::data() (which may be NULL) would trigger an abort. The patch moves the NULL checks inside an if (n_tx_outputs > 0) block and removes the SECP256K1_ARG_NONNULL attributes for those parameters. A test case is added verifying that NULL arrays with n_tx_outputs=0 succeed and return 0 found outputs.
Changed components
include/secp256k1_silentpayments.hsrc/modules/silentpayments/main_impl.hsrc/modules/silentpayments/tests_impl.hInspect captured patch +18 / −11
### include/secp256k1_silentpayments.h
@@ -356,14 +356,16 @@ typedef struct secp256k1_silentpayments_found_output {
* Args: ctx: pointer to a context object
* Out: found_outputs: pointer to an array of pointers to found
* output objects. The found outputs array MUST
- * have the same length as the tx_outputs array.
+ * have the same length as the tx_outputs array
+ * (can be NULL if n_tx_outputs is 0).
* n_found_outputs: pointer to an integer indicating the final
* size of the found outputs array. This number
* represents the number of outputs found while
* scanning (0 if none are found). Can't be larger than
* SECP256K1_SILENTPAYMENTS_RECIPIENT_GROUP_LIMIT.
* In: tx_outputs: pointer to the transaction's x-only public key outputs,
- * in their original transaction (vout) order
+ * in their original transaction (vout) order (can be
+ * NULL if n_tx_outputs is 0)
* n_tx_outputs: the size of the tx_outputs array.
* scan_key32: pointer to the recipient's 32 byte scan key. The scan
* key is valid if it passes secp256k1_ec_seckey_verify
@@ -390,7 +392,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_recipien
const secp256k1_pubkey *unlabeled_spend_pubkey,
secp256k1_silentpayments_label_lookup label_lookup,
const void *label_context
-) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7) SECP256K1_ARG_NONNULL(8);
+) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7) SECP256K1_ARG_NONNULL(8);
#ifdef __cplusplus
}
### src/modules/silentpayments/main_impl.h
@@ -611,16 +611,18 @@ int secp256k1_silentpayments_recipient_scan_outputs(
/* Sanity check inputs */
VERIFY_CHECK(ctx != NULL);
- ARG_CHECK(found_outputs != NULL);
ARG_CHECK(n_found_outputs != NULL);
*n_found_outputs = 0;
- ARG_CHECK(tx_outputs != NULL);
- for (i = 0; i < n_tx_outputs; i++) {
- ARG_CHECK(found_outputs[i] != NULL);
- ARG_CHECK(tx_outputs[i] != NULL);
- /* Validate each tx output object early so malformed pubkeys are always rejected. */
- if (!secp256k1_xonly_pubkey_load(ctx, &tx_output_ge, tx_outputs[i])) {
- return 0;
+ if (n_tx_outputs > 0) {
+ ARG_CHECK(found_outputs != NULL);
+ ARG_CHECK(tx_outputs != NULL);
+ for (i = 0; i < n_tx_outputs; i++) {
+ ARG_CHECK(found_outputs[i] != NULL);
+ ARG_CHECK(tx_outputs[i] != NULL);
+ /* Validate each tx output object early so malformed pubkeys are always rejected. */
+ if (!secp256k1_xonly_pubkey_load(ctx, &tx_output_ge, tx_outputs[i])) {
+ return 0;
+ }
}
}
ARG_CHECK(scan_key32 != NULL);
### src/modules/silentpayments/tests_impl.h
@@ -641,6 +641,9 @@ static void test_recipient_api(void) {
n_f = 1;
CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 0, ALICE_SECKEY, &ps, &p, &label_lookup, &labels_cache) == 0);
CHECK(n_f == 0);
+ n_f = 1;
+ CHECK(secp256k1_silentpayments_recipient_scan_outputs(CTX, NULL, &n_f, NULL, 0, ALICE_SECKEY, &ps, &p, &label_lookup, &labels_cache) == 0);
+ CHECK(n_f == 0);
/* Check that malformed secret key, public keys, and prevouts_summary arguments are handled */
CHECK_ILLEGAL(CTX, secp256k1_silentpayments_recipient_scan_outputs(CTX, fp, &n_f, tp, 1, ALICE_SECKEY, &ps, &malformed_p, NULL, NULL));Why this scored 30/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.