profiles: error if fan curve parse is less than 8

Closes #225
This commit is contained in:
Luke D. Jones
2022-08-20 22:05:22 +12:00
parent 3fe5896596
commit 92009ef96c
2 changed files with 12 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ pub enum ProfileError {
/// (pwm/temp, prev, next)
ParseFanCurvePrevHigher(&'static str, u8, u8),
ParseFanCurvePercentOver100(u8),
NotEnoughPoints,
// Zbus(zbus::Error),
}
@@ -24,6 +25,7 @@ impl fmt::Display for ProfileError {
ProfileError::Read(path, error) => write!(f, "Read {}: {}", path, error),
ProfileError::Write(path, error) => write!(f, "Write {}: {}", path, error),
ProfileError::NotSupported => write!(f, "Not supported"),
ProfileError::NotEnoughPoints => write!(f, "Less than 8 curve points supplied"),
ProfileError::NotFound(deets) => write!(f, "Not found: {}", deets),
ProfileError::Io(detail) => write!(f, "std::io error: {}", detail),
ProfileError::ParseProfileName => write!(f, "Invalid profile name"),

View File

@@ -75,6 +75,10 @@ impl std::str::FromStr for CurveData {
let mut pwm_prev = 0;
let mut percentages = false;
if input.split(',').count() < 8 {
return Err(ProfileError::NotEnoughPoints);
}
for (index, value) in input.split(',').enumerate() {
for (select, num) in value.splitn(2, |c| c == 'c' || c == ':').enumerate() {
if num.contains('%') {
@@ -265,7 +269,12 @@ mod tests {
assert_eq!(
string.as_str(),
"CPU: 30c:1%,49c:1%,59c:3%,69c:3%,79c:30%,89c:49%,99c:56%,109c:58%"
)
);
let curve =
CurveData::from_str("30c:1%,49c:2%,59c:3%,69c:4%,79c:31%,89c:49%,99c:56%");
assert!(curve.is_err());
}
#[test]