Skip to content

Commit

Permalink
fix(linux): be more permissive on invalid UTF-8 file content (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
naguam authored Mar 29, 2024
1 parent b30b8e8 commit 3a229ec
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/platform/linux/sysfs/fs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::error;
use std::fs::read_to_string;
use std::io;
use std::path::Path;
use std::str::FromStr;
Expand Down Expand Up @@ -93,8 +92,9 @@ pub fn scope<T: AsRef<Path>>(path: T) -> Result<Scope> {
/// Ok(None) - file is missing
/// Err(_) - unable to access file for some reasons (except `NotFound` and `ENODEV`)
pub fn get_string<T: AsRef<Path>>(path: T) -> Result<Option<String>> {
match read_to_string(path) {
Ok(mut content) => {
match std::fs::read(path) {
Ok(buffer) => {
let mut content: String = String::from_utf8_lossy(buffer.as_slice()).into();
if content.starts_with('\0') {
Err(io::Error::from(io::ErrorKind::InvalidData).into())
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/linux/sysfs/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl<'p> DataBuilder<'p> {
// (real one this time), it is better just to ignore this value.
// See: https://github.com/svartalf/rust-battery/issues/23
match value {
Some(cycles) if cycles == 0 => None,
Some(0) => None,
Some(cycles) => Some(cycles),
None => None,
}
Expand Down

0 comments on commit 3a229ec

Please sign in to comment.