Skip to content

Commit ed1053c

Browse files
authored
Fix unwraps in connection module when probing a loopback port (#954)
* Fix unwraps in connection module when probing a loopback port * Changelog * Fix rustfmt check fail
1 parent 84cd43c commit ed1053c

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Fixed
1515

16+
- Fix a crash with Connection module when connecting to a loopback serial port (#954)
17+
1618
### Removed
1719

1820
## [4.1.0] - 2025-09-18

espflash/src/connection/mod.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -397,31 +397,24 @@ impl Connection {
397397

398398
let value = match response.len() {
399399
10 | 12 => CommandResponseValue::ValueU32(u32::from_le_bytes(
400-
response[4..][..4].try_into().unwrap(),
400+
response[4..][..4].try_into()?,
401+
)),
402+
// MD5 is in ASCII
403+
44 => CommandResponseValue::ValueU128(u128::from_str_radix(
404+
std::str::from_utf8(&response[8..][..32])?,
405+
16,
406+
)?),
407+
// MD5 is BE bytes
408+
26 => CommandResponseValue::ValueU128(u128::from_be_bytes(
409+
response[8..][..16].try_into()?,
401410
)),
402-
44 => {
403-
// MD5 is in ASCII
404-
CommandResponseValue::ValueU128(
405-
u128::from_str_radix(
406-
std::str::from_utf8(&response[8..][..32]).unwrap(),
407-
16,
408-
)
409-
.unwrap(),
410-
)
411-
}
412-
26 => {
413-
// MD5 is BE bytes
414-
CommandResponseValue::ValueU128(u128::from_be_bytes(
415-
response[8..][..16].try_into().unwrap(),
416-
))
417-
}
418411
_ => CommandResponseValue::Vector(response.clone()),
419412
};
420413

421414
let header = CommandResponse {
422415
resp: response[0],
423416
return_op: response[1],
424-
return_length: u16::from_le_bytes(response[2..][..2].try_into().unwrap()),
417+
return_length: u16::from_le_bytes(response[2..][..2].try_into()?),
425418
value,
426419
error: response[response.len() - status_len + 1],
427420
status: response[response.len() - status_len],

espflash/src/error.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
#[cfg(feature = "serialport")]
44
use std::fmt::{Display, Formatter};
5-
use std::{array::TryFromSliceError, io};
5+
use std::{array::TryFromSliceError, io, num::ParseIntError, str::Utf8Error};
66

77
use miette::Diagnostic;
88
#[cfg(feature = "serialport")]
@@ -294,6 +294,14 @@ pub enum Error {
294294
#[error(transparent)]
295295
TryFromSlice(CoreError),
296296

297+
/// Error while trying to parse int from text
298+
#[error(transparent)]
299+
ParseIntError(CoreError),
300+
301+
/// Error while trying to parse UTF-8 from bytes
302+
#[error(transparent)]
303+
Utf8Error(CoreError),
304+
297305
/// Failed to open file
298306
#[error("Failed to open file: {0}")]
299307
FileOpenError(String, #[source] io::Error),
@@ -380,6 +388,18 @@ impl From<TryFromSliceError> for Error {
380388
}
381389
}
382390

391+
impl From<ParseIntError> for Error {
392+
fn from(err: ParseIntError) -> Self {
393+
Self::ParseIntError(Box::new(err))
394+
}
395+
}
396+
397+
impl From<Utf8Error> for Error {
398+
fn from(err: Utf8Error) -> Self {
399+
Self::Utf8Error(Box::new(err))
400+
}
401+
}
402+
383403
impl From<object::Error> for Error {
384404
fn from(err: object::Error) -> Self {
385405
Self::InvalidElf(err.into())

espflash/src/flasher/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,8 +1051,8 @@ impl Flasher {
10511051
{
10521052
let metadata = image_format.metadata();
10531053
if metadata.contains_key("app_size") && metadata.contains_key("part_size") {
1054-
let app_size = metadata["app_size"].parse::<u32>().unwrap();
1055-
let part_size = metadata["part_size"].parse::<u32>().unwrap();
1054+
let app_size = metadata["app_size"].parse::<u32>()?;
1055+
let part_size = metadata["part_size"].parse::<u32>()?;
10561056

10571057
crate::cli::display_image_size(app_size, Some(part_size));
10581058
}

0 commit comments

Comments
 (0)