Skip to content

Commit 6bffed2

Browse files
authored
address arithmetic_side_effects lints (#644)
1 parent 1a84305 commit 6bffed2

File tree

7 files changed

+63
-29
lines changed

7 files changed

+63
-29
lines changed

src/disk_usage.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ pub fn check(
4545

4646
fn check_max_usage(estimated: u64, max_disk_usage: NonZeroU64) -> Result<()> {
4747
// convert to MB
48-
let allowed = max_disk_usage.get() * 1024 * 1024;
48+
let allowed = max_disk_usage
49+
.get()
50+
.saturating_mul(1024)
51+
.saturating_mul(1024);
4952

5053
if estimated > allowed {
5154
return Err(Error::DiskUsageEstimateExceeded { estimated, allowed });
@@ -142,9 +145,9 @@ fn disk_usage(path: &Path) -> Result<DiskUsage> {
142145
.try_into()
143146
.map_err(|e| Error::Other("unable to identify block size", format!("{e}")))?;
144147

145-
let total = statfs.f_blocks * f_bsize;
146-
let free = statfs.f_bavail * f_bsize;
147-
let used = total - free;
148+
let total = statfs.f_blocks.saturating_mul(f_bsize);
149+
let free = statfs.f_bavail.saturating_mul(f_bsize);
150+
let used = total.saturating_sub(free);
148151

149152
let result = DiskUsage { total, used };
150153

src/image.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ impl Header {
8383
let end = src
8484
.read_u64::<LittleEndian>()
8585
.map_err(|e| Error::ReadHeader(e, "end offset"))?
86-
+ 1;
86+
.checked_add(1)
87+
.ok_or(Error::TooLarge)?;
8788
let padding = src
8889
.read_u64::<LittleEndian>()
8990
.map_err(|e| Error::ReadHeader(e, "padding"))?;
@@ -108,7 +109,10 @@ impl Header {
108109
};
109110
let mut bytes = [0; 32];
110111
LittleEndian::write_u32_into(&[magic, self.version], &mut bytes[..8]);
111-
LittleEndian::write_u64_into(&[self.range.start, self.range.end - 1, 0], &mut bytes[8..]);
112+
LittleEndian::write_u64_into(
113+
&[self.range.start, self.range.end.saturating_sub(1), 0],
114+
&mut bytes[8..],
115+
);
112116
Ok(bytes)
113117
}
114118

@@ -143,7 +147,7 @@ where
143147
while size >= PAGE_SIZE {
144148
src.read_exact(&mut buf).map_err(Error::Read)?;
145149
dst.write_all(&buf).map_err(Error::Write)?;
146-
size -= PAGE_SIZE;
150+
size = size.saturating_sub(PAGE_SIZE);
147151
}
148152
if size > 0 {
149153
buf.resize(size, 0);
@@ -159,8 +163,14 @@ where
159163
R: Read,
160164
W: Write,
161165
{
162-
let size = usize::try_from(header.range.end - header.range.start)
163-
.map_err(|_| Error::SizeConversion)?;
166+
let size = usize::try_from(
167+
header
168+
.range
169+
.end
170+
.checked_sub(header.range.start)
171+
.ok_or(Error::SizeConversion)?,
172+
)
173+
.map_err(|_| Error::SizeConversion)?;
164174

165175
// read the entire block into memory, but still read page by page
166176
let mut buf = Cursor::new(vec![0; size]);
@@ -202,7 +212,7 @@ where
202212
W: Write,
203213
{
204214
header.write(dst)?;
205-
let size = usize::try_from(header.range.end - header.range.start)
215+
let size = usize::try_from(header.range.end.saturating_sub(header.range.start))
206216
.map_err(|_| Error::SizeConversion)?;
207217

208218
if header.version == 1 {
@@ -232,7 +242,7 @@ where
232242
R: Read,
233243
W: Write,
234244
{
235-
if header.range.end - header.range.start > MAX_BLOCK_SIZE {
245+
if header.range.end.saturating_sub(header.range.start) > MAX_BLOCK_SIZE {
236246
copy_large_block(header, src, dst)
237247
} else {
238248
copy_if_nonzero(header, src, dst)
@@ -252,10 +262,14 @@ where
252262
W: Write,
253263
{
254264
if header.version == 2 {
255-
while header.range.end - header.range.start > MAX_BLOCK_SIZE {
265+
while header.range.end.saturating_sub(header.range.start) > MAX_BLOCK_SIZE {
256266
let range = Range {
257267
start: header.range.start,
258-
end: header.range.start + MAX_BLOCK_SIZE,
268+
end: header
269+
.range
270+
.start
271+
.checked_add(MAX_BLOCK_SIZE)
272+
.ok_or(Error::TooLarge)?,
259273
};
260274
copy_block_impl(
261275
&Header {
@@ -265,7 +279,11 @@ where
265279
src,
266280
dst,
267281
)?;
268-
header.range.start += MAX_BLOCK_SIZE;
282+
header.range.start = header
283+
.range
284+
.start
285+
.checked_add(MAX_BLOCK_SIZE)
286+
.ok_or(Error::TooLarge)?;
269287
}
270288
}
271289
if header.range.end > header.range.start {

src/iomem.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ pub fn split_ranges(ranges: Vec<Range<u64>>, max_size: u64) -> Vec<Range<u64>> {
8989
let mut result = vec![];
9090

9191
for mut range in ranges {
92-
while range.end - range.start > max_size {
92+
while range.end.saturating_sub(range.start) > max_size {
93+
let end = range.start.saturating_add(max_size);
9394
result.push(Range {
9495
start: range.start,
95-
end: range.start + max_size,
96+
end,
9697
});
97-
range.start += max_size;
98+
range.start = end;
9899
}
99100
if !range.is_empty() {
100101
result.push(range);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(crate) fn format_error(e: &impl StdError, f: &mut Formatter) -> FmtResult {
9191
while let Some(inner) = source {
9292
writeln!(f, "{i: >5}: {inner}")?;
9393
source = inner.source();
94-
i += 1;
94+
i = i.saturating_add(1);
9595
}
9696
}
9797

src/snapshot.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,14 @@ impl<'a, 'b> Snapshot<'a, 'b> {
269269
header.range.contains(&range.start),
270270
// TODO: ranges is currently inclusive, but not a
271271
// RangeInclusive. this should be adjusted.
272-
header.range.contains(&(range.end - 1)),
272+
header.range.contains(&(range.end.saturating_sub(1))),
273273
) {
274274
(true, true) => {
275275
let block = Block {
276-
offset: header.offset + range.start - header.range.start,
276+
offset: header
277+
.offset
278+
.saturating_add(range.start)
279+
.saturating_sub(header.range.start),
277280
range: range.clone(),
278281
};
279282

@@ -282,7 +285,10 @@ impl<'a, 'b> Snapshot<'a, 'b> {
282285
}
283286
(true, false) => {
284287
let block = Block {
285-
offset: header.offset + range.start - header.range.start,
288+
offset: header
289+
.offset
290+
.saturating_add(range.start)
291+
.saturating_sub(header.range.start),
286292
range: range.start..header.range.end,
287293
};
288294

@@ -353,13 +359,17 @@ impl<'a, 'b> Snapshot<'a, 'b> {
353359
.first()
354360
.ok_or_else(|| Error::UnableToCreateSnapshot("no initial memory range".to_string()))?
355361
.start;
356-
let start = first_vaddr - first_start;
362+
let start = first_vaddr.saturating_sub(first_start);
357363

358364
let mut physical_ranges = vec![];
359365

360366
for phdr in segments {
361-
let entry_start = phdr.p_vaddr - start;
362-
let entry_end = entry_start + phdr.p_memsz;
367+
let entry_start = phdr.p_vaddr.checked_sub(start).ok_or_else(|| {
368+
Error::UnableToCreateSnapshot("unable to calculate start address".to_string())
369+
})?;
370+
let entry_end = entry_start.checked_add(phdr.p_memsz).ok_or_else(|| {
371+
Error::UnableToCreateSnapshot("unable to calculate end address".to_string())
372+
})?;
363373

364374
physical_ranges.push(Block {
365375
range: entry_start..entry_end,

src/upload/blobstore.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ fn calc_concurrency(
115115
x if (x < REASONABLE_BLOCK_SIZE * BLOB_MAX_BLOCKS) => REASONABLE_BLOCK_SIZE,
116116
// otherwise, just use the smallest block size that will fit
117117
// within MAX BLOCKS to reduce memory pressure
118-
x => (x / BLOB_MAX_BLOCKS) + 1,
118+
x => (x / BLOB_MAX_BLOCKS)
119+
.checked_add(1)
120+
.ok_or(Error::TooLarge)?,
119121
}
120122
}
121123
// minimum required to hit high-throughput block blob performance thresholds
@@ -130,11 +132,11 @@ fn calc_concurrency(
130132
let upload_concurrency = match upload_concurrency {
131133
// manually specifying concurrency of 0 will disable concurrency
132134
0 | 1 => 1,
133-
_ => match (MEMORY_THRESHOLD).saturating_div(block_size) {
134-
0 => 1,
135+
_ => match (MEMORY_THRESHOLD).checked_div(block_size) {
136+
None | Some(0) => 1,
135137
// cap the number of concurrent threads to reduce concurrency issues
136138
// at the server end.
137-
x => cmp::min(MAX_CONCURRENCY, x),
139+
Some(x) => cmp::min(MAX_CONCURRENCY, x),
138140
},
139141
};
140142

src/write_counter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<W> Counter<W> {
2929
impl<W: Write> Write for Counter<W> {
3030
fn write(&mut self, buf: &[u8]) -> Result<usize> {
3131
let count = self.inner.write(buf)?;
32-
self.count += count;
32+
self.count = self.count.saturating_add(count);
3333
Ok(count)
3434
}
3535

0 commit comments

Comments
 (0)