Skip to content

Commit ab9999e

Browse files
committed
add array lenth validation
1 parent 2af0e7f commit ab9999e

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

rust/vote-tx-v2/src/decoding.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ const GENERALIZED_TX_LEN: u64 = 1;
2222

2323
impl Decode<'_, ()> for GeneralizedTx {
2424
fn decode(d: &mut Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {
25-
d.array()?;
25+
let Some(GENERALIZED_TX_LEN) = d.array()? else {
26+
return Err(minicbor::decode::Error::message(format!(
27+
"must be a defined sized array with {GENERALIZED_TX_LEN} entries"
28+
)));
29+
};
30+
2631
let tx_body = TxBody::decode(d, &mut ())?;
2732
Ok(Self { tx_body })
2833
}
@@ -40,7 +45,12 @@ impl Encode<()> for GeneralizedTx {
4045

4146
impl Decode<'_, ()> for TxBody {
4247
fn decode(d: &mut Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {
43-
d.array()?;
48+
let Some(TX_BODY_LEN) = d.array()? else {
49+
return Err(minicbor::decode::Error::message(format!(
50+
"must be a defined sized array with {GENERALIZED_TX_LEN} entries"
51+
)));
52+
};
53+
4454
let vote_type = Uuid::decode(d, &mut ())?;
4555
let votes = Vec::<Vote>::decode(d, &mut ())?;
4656
let voter_data = VoterData::decode(d, &mut ())?;
@@ -116,7 +126,12 @@ impl Encode<()> for Uuid {
116126

117127
impl Decode<'_, ()> for Vote {
118128
fn decode(d: &mut Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {
119-
d.array()?;
129+
let Some(VOTE_LEN) = d.array()? else {
130+
return Err(minicbor::decode::Error::message(format!(
131+
"must be a defined sized array with {VOTE_LEN} entries"
132+
)));
133+
};
134+
120135
let choices = Vec::<Choice>::decode(d, &mut ())?;
121136
if choices.is_empty() {
122137
return Err(minicbor::decode::Error::message(

0 commit comments

Comments
 (0)