Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 8f72275

Browse files
committed
WIP: json-sysctl: fix offset handling
1 parent 9b2d979 commit 8f72275

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

tests/json-sysctl/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![no_std]
22
#![feature(const_str_as_bytes)]
33

4+
use core::cmp::min;
45
use core::convert::TryInto;
56
use core::sync::atomic::{AtomicBool, Ordering};
67

@@ -36,9 +37,11 @@ impl linux_kernel_module::chrdev::FileOperations for JsonChrdev {
3637
c: C.load(Ordering::Relaxed),
3738
};
3839
let mut s =
39-
serde_json_core::to_string::<typenum::U32, _>(&o).map_err(|_| error::Error::ENOMEM)?;
40-
s.push_str("\n").map_err(|_| error::Error::ENOMEM)?;
41-
buf.write(&s.into_bytes()[offset.try_into()?..][..buf.len()])?;
40+
serde_json_core::to_vec::<typenum::U32, _>(&o).map_err(|_| error::Error::ENOMEM)?;
41+
s.push(b'\n').map_err(|_| error::Error::ENOMEM)?;
42+
let start = min(offset.try_into()?, s.len());
43+
let end = min(start + buf.len(), s.len());
44+
buf.write(&s[start..end])?;
4245
Ok(())
4346
}
4447
}

0 commit comments

Comments
 (0)