misc: add missing out parameter initialization for error cases
What changed, and why it matters
This commit fixes a pattern where library functions could return an error code without first setting their output parameter to a known value. In C, callers often check only the error code, but if they later read the output variable by mistake it could contain leftover or uninitialized data. The patch makes the library consistently zero out output parameters before returning any error, reducing the chance of subtle bugs or information leaks in downstream wallets and tools.
Treat as a low-to-moderate reliability/security hardening fix. Downstream projects using libwally-core should upgrade and review any code that reads output parameters after a non-WALLY_OK return, especially for Elements-only APIs compiled without BUILD_ELEMENTS.
Security signals we found
Uninitialized or attacker-influenced out-parameter values on error paths
Defensive initialization of output pointers before early returns
Elements/Liquid feature stubs returning WALLY_ERROR without clearing outputs
Potential information disclosure via stale stack/heap values in calling code
Evidence from the diff
The diff adds if (written) *written = 0; (and similar for value_out) before early error returns in src/elements.c, src/map.c, src/psbt.c, and src/transaction.c. Many of these functions previously initialized the output only inside #else branches or after further validation, leaving the caller-provided pointer untouched when BUILD_ELEMENTS is disabled or when an early validation failure occurred. The change is defensive: it ensures every error path writes a deterministic value to out-parameters that are documented as length/amount outputs.
Changed components
src/elements.csrc/map.csrc/psbt.csrc/transaction.cInspect captured patch +63 / −4
### src/elements.c
@@ -224,6 +224,8 @@ int wally_asset_value_commitment(uint64_t value,
int wally_asset_rangeproof_get_maximum_len(uint64_t value, int min_bits, size_t *written)
{
#ifndef BUILD_ELEMENTS
+ if (written)
+ *written = 0;
return WALLY_ERROR;
#else
const secp256k1_context *ctx = secp_ctx();
@@ -259,6 +261,8 @@ int wally_asset_rangeproof_with_nonce(uint64_t value,
size_t *written)
{
#ifndef BUILD_ELEMENTS
+ if (written)
+ *written = 0;
return WALLY_ERROR;
#else
const secp256k1_context *ctx = secp_ctx();
@@ -340,11 +344,16 @@ int wally_asset_rangeproof(uint64_t value,
size_t *written)
{
#ifndef BUILD_ELEMENTS
+ if (written)
+ *written = 0;
return WALLY_ERROR;
#else
unsigned char nonce_hash[SHA256_LEN];
int ret;
+ if (written)
+ *written = 0;
+
ret = wally_ecdh_nonce_hash(pub_key, pub_key_len, priv_key, priv_key_len,
nonce_hash, sizeof(nonce_hash));
if (ret == WALLY_OK)
@@ -371,6 +380,9 @@ int wally_explicit_rangeproof(uint64_t value,
const unsigned char *generator, size_t generator_len,
unsigned char *bytes_out, size_t len, size_t *written)
{
+ if (written)
+ *written = 0;
+
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
@@ -509,11 +521,16 @@ int wally_asset_unblind(const unsigned char *pub_key, size_t pub_key_len,
uint64_t *value_out)
{
#ifndef BUILD_ELEMENTS
+ if (value_out)
+ *value_out = 0;
return WALLY_ERROR;
#else
unsigned char nonce_hash[SHA256_LEN];
int ret;
+ if (value_out)
+ *value_out = 0;
+
ret = wally_ecdh_nonce_hash(pub_key, pub_key_len, priv_key, priv_key_len,
nonce_hash, sizeof(nonce_hash));
if (ret == WALLY_OK)
@@ -535,11 +552,14 @@ int wally_asset_unblind(const unsigned char *pub_key, size_t pub_key_len,
int wally_asset_surjectionproof_size(size_t num_inputs, size_t *written)
{
#ifndef BUILD_ELEMENTS
+ if (written)
+ *written = 0;
return WALLY_ERROR;
#else
size_t num_used = num_inputs > 3 ? 3 : num_inputs;
if (written)
*written = 0;
+
if (!num_inputs || num_inputs > SECP256K1_SURJECTIONPROOF_MAX_N_INPUTS || !written)
return WALLY_EINVAL;
*written = SECP256K1_SURJECTIONPROOF_SERIALIZATION_BYTES(num_inputs, num_used);
@@ -646,9 +666,13 @@ int wally_asset_surjectionproof_len(const unsigned char *output_asset, size_t ou
size_t *written)
{
#ifndef BUILD_ELEMENTS
+ if (written)
+ *written = 0;
return WALLY_ERROR;
#else
unsigned char buff[1];
+ if (written)
+ *written = 0;
return surjproof_impl(output_asset, output_asset_len, output_abf, output_abf_len,
output_generator, output_generator_len, bytes, bytes_len,
asset, asset_len, abf, abf_len, generator, generator_len,
@@ -666,6 +690,9 @@ int wally_asset_surjectionproof(const unsigned char *output_asset, size_t output
unsigned char *bytes_out, size_t len,
size_t *written)
{
+ if (written)
+ *written = 0;
+
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
@@ -1092,11 +1119,12 @@ int wally_asset_blinding_key_to_vbf(
int wally_asset_pak_whitelistproof_size(size_t num_keys, size_t *written)
{
+ if (written)
+ *written = 0;
+
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
- if (written)
- *written = 0;
if (!written || num_keys > SECP256K1_WHITELIST_MAX_N_KEYS)
return WALLY_EINVAL;
*written = 1 + 32 * (1 + num_keys);
@@ -1114,6 +1142,8 @@ int wally_asset_pak_whitelistproof(
unsigned char *bytes_out, size_t len, size_t *written)
{
#ifndef BUILD_ELEMENTS
+ if (written)
+ *written = 0;
return WALLY_ERROR;
#else
const secp256k1_context *ctx = secp_ctx();
@@ -1183,9 +1213,13 @@ int wally_asset_pak_whitelistproof_len(
size_t *written)
{
#ifndef BUILD_ELEMENTS
+ if (written)
+ *written = 0;
return WALLY_ERROR;
#else
unsigned char buff[1]; /* Undersized: we only want the length */
+ if (written)
+ *written = 0;
return wally_asset_pak_whitelistproof(online_keys, online_keys_len,
offline_keys, offline_keys_len,
key_index,
### src/map.c
@@ -99,6 +99,8 @@ static int map_find(const struct wally_map *map_in, size_t index,
int wally_map_find_from(const struct wally_map *map_in, size_t index,
const unsigned char *key, size_t key_len, size_t *written)
{
+ if (written)
+ *written = 0;
return key ? map_find(map_in, index, key, key_len, written) : WALLY_EINVAL;
}
### src/psbt.c
@@ -356,10 +356,12 @@ static int map_field_set(struct wally_map *map_in, uint32_t type,
#define MAP_INNER_FIELD_PSET(typ, name, FT) \
int wally_psbt_ ## typ ## _get_ ## name ## _len(const struct wally_psbt_ ## typ *p, \
size_t * written) { \
+ if (written) *written = 0; \
return WALLY_ERROR; \
} \
int wally_psbt_ ## typ ## _get_ ## name(const struct wally_psbt_ ## typ *p, \
unsigned char *bytes_out, size_t len, size_t * written) { \
+ if (written) *written = 0; \
return WALLY_ERROR; \
} \
int wally_psbt_ ## typ ## _clear_ ## name(struct wally_psbt_ ## typ *p) { \
@@ -1039,6 +1041,8 @@ int wally_psbt_output_get_blinding_status(const struct wally_psbt_output *output
uint32_t flags, size_t *written)
{
#ifndef BUILD_ELEMENTS
+ if (written)
+ *written = 0;
return WALLY_ERROR;
#else
uint64_t state;
@@ -1416,6 +1420,7 @@ int wally_psbt_get_global_tx_alloc(const struct wally_psbt *psbt, struct wally_t
#else
#define PSBT_GET_PSET(name, v) \
int wally_psbt_get_ ## name(const struct wally_psbt *psbt, size_t *written) { \
+ if (written) *written = 0; \
return WALLY_ERROR; \
}
#endif /* BUILD_ELEMENTS */
@@ -1540,10 +1545,10 @@ int wally_psbt_find_input_spending_utxo(const struct wally_psbt *psbt,
#ifndef WALLY_ABI_NO_ELEMENTS
int wally_psbt_get_global_scalars_size(const struct wally_psbt *psbt, size_t *written)
{
+ if (written) *written = 0;
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
- if (written) *written = 0;
if (!psbt_is_valid(psbt) || psbt->version == PSBT_0 || !written)
return WALLY_EINVAL;
*written = psbt->global_scalars.num_items;
@@ -1555,10 +1560,10 @@ int wally_psbt_find_global_scalar(struct wally_psbt *psbt,
const unsigned char *scalar, size_t scalar_len,
size_t *written)
{
+ if (written) *written = 0;
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
- if (written) *written = 0;
if (!psbt_is_valid(psbt) || psbt->version == PSBT_0)
return WALLY_EINVAL;
return wally_map_find(&psbt->global_scalars, scalar, scalar_len, written);
@@ -4541,6 +4546,8 @@ int wally_psbt_get_input_signing_script_len(const struct wally_psbt *psbt,
size_t index, size_t *written)
{
const unsigned char *p;
+ if (written)
+ *written = 0;
return written ? get_signing_script(psbt, index, &p, written) : WALLY_EINVAL;
}
@@ -4679,6 +4686,9 @@ int wally_psbt_get_input_scriptcode_len(const struct wally_psbt *psbt, size_t in
{
unsigned char p2pkh[WALLY_SCRIPTPUBKEY_P2PKH_LEN];
const unsigned char *p;
+
+ if (written)
+ *written = 0;
return get_scriptcode(psbt, index, p2pkh, sizeof(p2pkh),
script, script_len, &p, written);
}
@@ -5962,10 +5972,12 @@ int wally_psbt_is_elements(const struct wally_psbt *psbt, size_t *written)
#define PSBT_FIELD_PSET(typ, name, ver) \
int wally_psbt_get_ ## typ ## _ ## name ## _len(const struct wally_psbt *psbt, \
size_t index, size_t *written) { \
+ if (written) *written = 0; \
return WALLY_ERROR; \
} \
int wally_psbt_get_ ## typ ## _ ## name(const struct wally_psbt *psbt, size_t index, \
unsigned char *bytes_out, size_t len, size_t *written) { \
+ if (written) *written = 0; \
return WALLY_ERROR; \
} \
int wally_psbt_clear_ ## typ ## _ ## name(struct wally_psbt *psbt, size_t index) { \
@@ -6282,6 +6294,7 @@ PSBT_GET_I_PSET(output, blinder_index, uint32_t, PSBT_2)
int wally_psbt_has_output_blinder_index(const struct wally_psbt *psbt, size_t index, size_t *written) {
#ifndef BUILD_ELEMENTS
+ if (written) *written = 0;
return WALLY_ERROR;
#else
struct wally_psbt_output *p = psbt_get_output(psbt, index);
@@ -6316,6 +6329,7 @@ PSBT_FIELD_PSET(output, asset_blinding_surjectionproof, PSBT_2)
int wally_psbt_get_output_blinding_status(const struct wally_psbt *psbt, size_t index, uint32_t flags, size_t *written)
{
#ifndef BUILD_ELEMENTS
+ if (written) *written = 0;
return WALLY_ERROR;
#else
struct wally_psbt_output *p = psbt_get_output(psbt, index);
### src/transaction.c
@@ -1836,6 +1836,10 @@ int wally_tx_get_length(const struct wally_tx *tx, uint32_t flags,
size_t *written)
{
size_t is_elements = 0;
+
+ if (written)
+ *written = 0;
+
#ifdef BUILD_ELEMENTS
if (wally_tx_is_elements(tx, &is_elements) != WALLY_OK)
return WALLY_EINVAL;
@@ -2155,6 +2159,9 @@ int wally_tx_to_bytes(const struct wally_tx *tx, uint32_t flags,
{
size_t is_elements = 0;
+ if (written)
+ *written = 0;
+
#ifdef BUILD_ELEMENTS
if (wally_tx_is_elements(tx, &is_elements) != WALLY_OK)
return WALLY_EINVAL;
@@ -3207,6 +3214,7 @@ static struct wally_tx_output *tx_get_output(const struct wally_tx *tx, size_t i
#else
#define TX_GET_B_ELEMENTS(typ, name) \
int wally_tx_get_ ## typ ## _ ## name(const struct wally_tx *tx, size_t index, unsigned char *bytes_out, size_t len, size_t *written) { \
+ if (written) *written = 0; \
return WALLY_ERROR; \
}
#endif /* BUILD_ELEMENTS */
@@ -3233,6 +3241,7 @@ static struct wally_tx_output *tx_get_output(const struct wally_tx *tx, size_t i
#else
#define TX_GET_I_ELEMENTS(typ, name) \
int wally_tx_get_ ## typ ## _ ## name(const struct wally_tx *tx, size_t index, size_t *written) { \
+ if (written) *written = 0; \
return WALLY_ERROR; \
}
#endif /* BUILD_ELEMENTS */Why this scored 51/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.