Merge rust-bitcoin/rust-bitcoin#7001: units: fix serialization of optional amounts
What changed, and why it matters
This commit fixes a bug in how optional Bitcoin amounts are serialized when using certain compact binary formats (like bincode). The code was writing the raw inner value for a Some(amount) instead of properly marking it as 'Some'. This could cause deserialization to fail or produce wrong values when round-tripping optional amounts. The fix changes the serializer to use the standard 'serialize_some' method and adds tests to confirm round-tripping now works.
Upgrade to a version of rust-bitcoin containing this commit if your application serializes optional Amount or SignedAmount values with bincode or other non-self-describing serde formats. Review persisted binary data produced before the fix, as it may lack the Some marker and could deserialize incorrectly with strict deserializers.
Security signals we found
Data integrity failure: optional amount may deserialize incorrectly or fail to round-trip in binary serde formats
Incorrect Option serialization omitting Some variant tag
Fix verified by new bincode round-trip tests
Evidence from the diff
In units/src/amount/serde.rs, the optional-amount serializers under as_btc::opt and as_str::opt were calling the inner type’s Serialize implementation directly (f64::serialize/str::serialize) for Some values, omitting the Option discriminant. With self-describing formats like JSON this is masked because the deserializer can infer presence, but with non-self-describing formats such as bincode the missing Some tag causes failed or incorrect deserialization. The patch replaces those calls with Serializer::serialize_some and updates the bincode test fixture bytes to include the two-byte Some marker. Tests are added to verify bincode round-trips.
Changed components
units/src/amount/serde.rsas_btc::opt serializeras_str::opt serializerbincode-based consumers of optional Amount/SignedAmountInspect captured patch +12 / −4
### units/src/amount/serde.rs
@@ -362,7 +362,7 @@ pub mod as_btc {
use core::fmt;
use core::marker::PhantomData;
- use serde::{de, Deserializer, Serialize, Serializer};
+ use serde::{de, Deserializer, Serializer};
use crate::amount::{Denomination, SignedAmount};
@@ -373,7 +373,7 @@ pub mod as_btc {
A: Into<SignedAmount> + Copy,
{
match a.map(Into::into).map(|amt| amt.to_float_in(Denomination::Bitcoin)) {
- Some(a) => f64::serialize(&a, s),
+ Some(a) => s.serialize_some(&a),
None => s.serialize_none(),
}
}
@@ -534,7 +534,7 @@ pub mod as_str {
use core::fmt;
use core::marker::PhantomData;
- use serde::{de, Deserializer, Serialize, Serializer};
+ use serde::{de, Deserializer, Serializer};
use crate::amount::{Denomination, SignedAmount};
@@ -545,7 +545,7 @@ pub mod as_str {
A: Into<SignedAmount> + Copy,
{
match a.map(Into::into).map(|amt| amt.to_string_in(Denomination::Bitcoin)) {
- Some(a) => str::serialize(&a, s),
+ Some(a) => s.serialize_some(&a),
None => s.serialize_none(),
}
}
### units/tests/data/serde_bincode
[binary or diff unavailable]
### units/tests/serde.rs
@@ -362,6 +362,10 @@ fn serde_amount_as_btc_opt() {
let v = serde_json::to_string(s).unwrap();
let w: T = serde_json::from_str(&v).unwrap();
assert_eq!(w, *s);
+
+ let bytes = bincode::serialize(s).unwrap();
+ let round_trip: T = bincode::deserialize(&bytes).unwrap();
+ assert_eq!(round_trip, *s);
}
let t: T = serde_json::from_str("{\"amt\": 2.5, \"samt\": -2.5}").unwrap();
@@ -440,6 +444,10 @@ fn serde_amount_as_str_opt() {
let v = serde_json::to_string(s).unwrap();
let w: T = serde_json::from_str(&v).unwrap();
assert_eq!(w, *s);
+
+ let bytes = bincode::serialize(s).unwrap();
+ let round_trip: T = bincode::deserialize(&bytes).unwrap();
+ assert_eq!(round_trip, *s);
}
let t: T =Why this scored 48/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.