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 the bincode format. Previously, an optional amount like `Some(1 BTC)` was written as just the raw number/string, without the marker that says "this value is present." That meant when reading it back, the code could misinterpret the data or fail. The fix uses the standard `serialize_some` method so the `Some` wrapper is preserved. The test data file was also updated to include the two missing bytes that represent `Some`.
Review whether any persisted bincode data produced by these helpers before the fix needs migration or re-serialization, since old payloads may deserialize incorrectly or fail. Confirm the updated test fixture covers both signed and unsigned optional amount cases. Consider auditing other optional serde helpers in the crate for the same pattern.
Security signals we found
Data integrity / serialization round-trip failure
Incorrect deserialization of optional values
Potential silent misinterpretation of `None` vs `Some(0)` depending on format
Evidence from the diff
In units/src/amount/serde.rs, the as_btc::opt and as_str::opt serialization helpers for Option<SignedAmount> were calling the inner type’s Serialize::serialize directly (e.g., f64::serialize(&a, s) and str::serialize(&a, s)). For self-describing formats like JSON this can be acceptable, but for compact binary formats like bincode it omits the Some discriminant, breaking round-trip deserialization. The patch changes both to use s.serialize_some(&a), which correctly emits the optional variant. The Serialize trait import is no longer needed and was removed. The binary test fixture serde_bincode was updated to include the two missing bytes for unsigned_opt_as_btc and signed_opt_as_btc.
Changed components
units/src/amount/serde.rsunits/tests/data/serde_bincodeOptional amount serialization helpers (`as_btc::opt`, `as_str::opt`)Inspect captured patch +4 / −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]Why this scored 50/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.