Skip to content

Commit 2aab21d

Browse files
chore(general): Enable arithmetic_side_effects Clippy lint (#179)
* Enable arithmetic_side_effects Clippy lint
1 parent 94f7445 commit 2aab21d

File tree

29 files changed

+232
-120
lines changed

29 files changed

+232
-120
lines changed

rust/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ members = [
1010
"cbork-cddl-parser",
1111
"cbork-utils",
1212
"catalyst-voting",
13-
"catalyst-types",
13+
"catalyst-types",
1414
"immutable-ledger",
1515
"vote-tx-v1",
1616
"vote-tx-v2",
@@ -60,3 +60,4 @@ string_slice = "deny"
6060
unchecked_duration_subtraction = "deny"
6161
unreachable = "deny"
6262
missing_docs_in_private_items = "deny"
63+
arithmetic_side_effects = "deny"

rust/Earthfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
VERSION 0.8
22

3-
IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:v3.2.27 AS rust-ci
3+
IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:v3.2.28 AS rust-ci
44

55
COPY_SRC:
66
FUNCTION

rust/c509-certificate/src/attributes/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ impl Encode<()> for Attributes {
6262
));
6363
}
6464
// The attribute type should be included in array too
65-
encode_array_len(e, "Attributes", self.0.len() as u64 * 2)?;
65+
let len = (self.0.len() as u64)
66+
.checked_mul(2)
67+
.ok_or_else(|| minicbor::encode::Error::message("Attributes length overflow"))?;
68+
encode_array_len(e, "Attributes", len)?;
6669
for attribute in &self.0 {
6770
attribute.encode(e, ctx)?;
6871
}

rust/c509-certificate/src/extensions/extension/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ impl Encode<()> for Extension {
110110
{
111111
// Determine encoded OID value based on critical flag
112112
let encoded_oid = if self.critical {
113-
-mapped_oid
113+
mapped_oid.checked_neg().ok_or_else(|| {
114+
minicbor::encode::Error::message(format!(
115+
"Invalid OID value (will overflow during negation): {mapped_oid}"
116+
))
117+
})?
114118
} else {
115119
mapped_oid
116120
};

rust/c509-certificate/src/extensions/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ impl Encode<()> for Extensions {
7070
if self.0.len() == 1 && extension.registered_oid().c509_oid().oid() == &KEY_USAGE_OID {
7171
match extension.value() {
7272
ExtensionValue::Int(value) => {
73-
let ku_value = if extension.critical() { -value } else { *value };
73+
let ku_value = if extension.critical() {
74+
value
75+
.checked_neg()
76+
.ok_or_else(|| minicbor::encode::Error::message(format!("Invalid key usage value (will overflow during negation): {value}")))?
77+
} else {
78+
*value
79+
};
7480
encode_helper(e, "Extensions KeyUsage", ctx, &ku_value)?;
7581
return Ok(());
7682
},

rust/c509-certificate/src/general_names/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ impl Encode<()> for GeneralNames {
5959
));
6060
}
6161
// The general name type should be included in array too
62-
encode_array_len(e, "General Names", self.0.len() as u64 * 2)?;
62+
let len = (self.0.len() as u64)
63+
.checked_mul(2)
64+
.ok_or_else(|| minicbor::encode::Error::message("General Names length overflow"))?;
65+
encode_array_len(e, "General Names", len)?;
6366
for gn in &self.0 {
6467
gn.encode(e, ctx)?;
6568
}

rust/c509-certificate/src/name/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,20 @@ impl Encode<()> for NameValue {
106106

107107
encode_cn_value(e, cn_value)?;
108108
} else {
109-
encode_array_len(e, "Attributes", attrs.len() as u64 * 2)?;
109+
let len = (attrs.len() as u64).checked_mul(2).ok_or_else(|| {
110+
minicbor::encode::Error::message("Attribute length overflow")
111+
})?;
112+
encode_array_len(e, "Attributes", len)?;
110113
for attribute in attrs {
111114
attribute.encode(e, ctx)?;
112115
}
113116
}
114117
} else {
115118
// If is okay if the attributes is empty
116-
encode_array_len(e, "Attributes", attrs.len() as u64 * 2)?;
119+
let len = (attrs.len() as u64).checked_mul(2).ok_or_else(|| {
120+
minicbor::encode::Error::message("Attribute length overflow")
121+
})?;
122+
encode_array_len(e, "Attributes", len)?;
117123
for attribute in attrs {
118124
attribute.encode(e, ctx)?;
119125
}

rust/cardano-blockchain-types/src/auxdata/scripts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl TryFrom<u64> for ScriptType {
100100
match value {
101101
0 => Err(anyhow!("Invalid script type: {}", value)),
102102
1 => Ok(Self::Native),
103-
_ => Ok(Self::Plutus(value - 1)),
103+
_ => Ok(Self::Plutus(value.saturating_sub(1))),
104104
}
105105
}
106106
}

rust/cardano-blockchain-types/src/fork.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl fmt::Display for Fork {
7171
0 => write!(f, "IMMUTABLE"),
7272
1 => write!(f, "BACKFILL"),
7373
// For live forks: 2 maps to LIVE:1, 3 maps to LIVE:2 etc.
74-
2..=u64::MAX => write!(f, "LIVE:{}", self.0 - 1),
74+
2..=u64::MAX => write!(f, "LIVE:{}", self.0.saturating_sub(1)),
7575
}
7676
}
7777
}

rust/cardano-blockchain-types/src/multi_era_block_data.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,10 @@ pub(crate) mod tests {
487487
let pallas_block =
488488
pallas::ledger::traverse::MultiEraBlock::decode(test_block.raw.as_slice())?;
489489

490-
let previous_point =
491-
Point::new((pallas_block.slot() - 1).into(), vec![0; 32].try_into()?);
490+
let previous_point = Point::new(
491+
(pallas_block.slot().checked_sub(1).unwrap()).into(),
492+
vec![0; 32].try_into()?,
493+
);
492494

493495
let block = MultiEraBlock::new(
494496
Network::Preprod,
@@ -511,7 +513,7 @@ pub(crate) mod tests {
511513
pallas::ledger::traverse::MultiEraBlock::decode(test_block.raw.as_slice())?;
512514

513515
let previous_point = Point::new(
514-
(pallas_block.slot() - 1).into(),
516+
(pallas_block.slot().checked_sub(1).unwrap()).into(),
515517
pallas_block
516518
.header()
517519
.previous_hash()
@@ -540,7 +542,7 @@ pub(crate) mod tests {
540542
let prev_point = pallas::ledger::traverse::MultiEraBlock::decode(block.as_slice())
541543
.map(|block| {
542544
Point::new(
543-
(block.slot() - 1).into(),
545+
(block.slot().saturating_sub(1)).into(),
544546
block
545547
.header()
546548
.previous_hash()

0 commit comments

Comments
 (0)