Cleanup some code

This commit is contained in:
topjohnwu 2023-05-28 23:50:52 -07:00
parent e668dbf6f7
commit f2846694e1
5 changed files with 43 additions and 25 deletions

View File

@ -12,7 +12,7 @@
using namespace std;
int fd_pathat(int dirfd, const char *name, char *path, size_t size) {
if (fd_path(dirfd, byte_slice(path, size)) < 0)
if (fd_path(dirfd, u8_mut_slice(path, size)) < 0)
return -1;
auto len = strlen(path);
path[len] = '/';

View File

@ -15,7 +15,7 @@ static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) {
buf[0] = '\0';
// Fortify logs when a fatal error occurs. Do not run through fortify again
int len = std::min(__call_bypassing_fortify(vsnprintf)(buf, sz, fmt, ap), sz - 1);
log_with_rs(level, rust::Slice(reinterpret_cast<const uint8_t *>(buf), len));
log_with_rs(level, u8_slice(buf, sz));
return len;
}

View File

@ -120,10 +120,15 @@ struct StringCmp {
};
template<typename T>
rust::Slice<uint8_t> byte_slice(T *buf, size_t sz) {
rust::Slice<uint8_t> u8_mut_slice(T *buf, size_t sz) {
return rust::Slice(reinterpret_cast<uint8_t *>(buf), sz);
}
template<typename T>
rust::Slice<const uint8_t> u8_slice(T *buf, size_t sz) {
return rust::Slice(reinterpret_cast<const uint8_t *>(buf), sz);
}
uint64_t parse_uint64_hex(std::string_view s);
int parse_int(std::string_view s);

View File

@ -105,11 +105,9 @@ bool sepolicy::exists(const char *type) {
}
void sepolicy::load_rule_file(const char *file) {
rust::load_rule_file(*this, rust::Slice(
reinterpret_cast<const uint8_t *>(file), strlen(file)));
rust::load_rule_file(*this, u8_slice(file, strlen(file)));
}
void sepolicy::load_rules(const std::string &rules) {
rust::load_rules(*this, rust::Slice(
reinterpret_cast<const uint8_t *>(rules.data()), rules.length()));
rust::load_rules(*this, u8_slice(rules.data(), rules.length()));
}

View File

@ -25,29 +25,44 @@ mod ffi {
}
}
fn load_rules_from_reader<T: BufRead>(mut sepol: Pin<&mut sepolicy>, reader: &mut T) {
reader.foreach_lines(|line| {
let bytes = line.trim().as_bytes();
unsafe {
sepol
.as_mut()
.parse_statement(bytes.as_ptr().cast(), bytes.len() as i32);
trait SepolicyExt {
fn load_rules(self: Pin<&mut Self>, rules: &[u8]);
fn load_rule_file(self: Pin<&mut Self>, filename: &[u8]);
fn load_rules_from_reader<T: BufRead>(self: Pin<&mut Self>, reader: &mut T);
}
impl SepolicyExt for sepolicy {
fn load_rules(self: Pin<&mut sepolicy>, rules: &[u8]) {
let mut cursor = Cursor::new(rules);
self.load_rules_from_reader(&mut cursor);
}
fn load_rule_file(self: Pin<&mut sepolicy>, filename: &[u8]) {
fn inner(sepol: Pin<&mut sepolicy>, filename: &[u8]) -> anyhow::Result<()> {
let filename = str::from_utf8(filename)?;
let mut reader = BufReader::new(File::open(filename)?);
sepol.load_rules_from_reader(&mut reader);
Ok(())
}
true
});
inner(self, filename).log().ok();
}
fn load_rules_from_reader<T: BufRead>(mut self: Pin<&mut sepolicy>, reader: &mut T) {
reader.foreach_lines(|line| {
let bytes = line.trim().as_bytes();
unsafe {
self.as_mut()
.parse_statement(bytes.as_ptr().cast(), bytes.len() as i32);
}
true
});
}
}
pub fn load_rule_file(sepol: Pin<&mut sepolicy>, filename: &[u8]) {
fn inner(sepol: Pin<&mut sepolicy>, filename: &[u8]) -> anyhow::Result<()> {
let filename = str::from_utf8(filename)?;
let mut reader = BufReader::new(File::open(filename)?);
load_rules_from_reader(sepol, &mut reader);
Ok(())
}
inner(sepol, filename).log().ok();
sepol.load_rule_file(filename);
}
pub fn load_rules(sepol: Pin<&mut sepolicy>, rules: &[u8]) {
let mut cursor = Cursor::new(rules);
load_rules_from_reader(sepol, &mut cursor);
sepol.load_rules(rules);
}