Skip to content

Commit b9e12da

Browse files
Enable arithmetic_side_effects Clippy lint
1 parent ffe23e3 commit b9e12da

File tree

28 files changed

+239
-118
lines changed

28 files changed

+239
-118
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ 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 Some(len) = (self.0.len() as u64).checked_mul(2) else {
66+
return Err(minicbor::encode::Error::message(
67+
"Attributes length overflow",
68+
));
69+
};
70+
encode_array_len(e, "Attributes", len)?;
6671
for attribute in &self.0 {
6772
attribute.encode(e, ctx)?;
6873
}

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ 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 Some(len) = (self.0.len() as u64).checked_mul(2) else {
63+
return Err(minicbor::encode::Error::message(
64+
"General Names length overflow",
65+
));
66+
};
67+
encode_array_len(e, "General Names", len)?;
6368
for gn in &self.0 {
6469
gn.encode(e, ctx)?;
6570
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,24 @@ 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 Some(len) = (attrs.len() as u64).checked_mul(2) else {
110+
return Err(minicbor::encode::Error::message(
111+
"Attribute length overflow",
112+
));
113+
};
114+
encode_array_len(e, "Attributes", len)?;
110115
for attribute in attrs {
111116
attribute.encode(e, ctx)?;
112117
}
113118
}
114119
} else {
115120
// If is okay if the attributes is empty
116-
encode_array_len(e, "Attributes", attrs.len() as u64 * 2)?;
121+
let Some(len) = (attrs.len() as u64).checked_mul(2) else {
122+
return Err(minicbor::encode::Error::message(
123+
"Attribute length overflow",
124+
));
125+
};
126+
encode_array_len(e, "Attributes", len)?;
117127
for attribute in attrs {
118128
attribute.encode(e, ctx)?;
119129
}

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/metadata/cip36/key_registration.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ fn check_is_key_exist(
150150
if found_keys.contains(key) {
151151
err_report.duplicate_field(
152152
format!("{key:?}").as_str(),
153-
format!("Redundant key found in item {} in RBAC map", index + 1).as_str(),
153+
format!(
154+
"Redundant key found in item {} in RBAC map",
155+
index.saturating_add(1)
156+
)
157+
.as_str(),
154158
format!("CIP36 Key Registration {key:?}").as_str(),
155159
);
156160
return true;

0 commit comments

Comments
 (0)