diff --git a/native/src/base/files.cpp b/native/src/base/files.cpp index f790573a1..9d72caa3c 100644 --- a/native/src/base/files.cpp +++ b/native/src/base/files.cpp @@ -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] = '/'; diff --git a/native/src/base/logging.cpp b/native/src/base/logging.cpp index a1a191dac..a093284e4 100644 --- a/native/src/base/logging.cpp +++ b/native/src/base/logging.cpp @@ -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(buf), len)); + log_with_rs(level, u8_slice(buf, sz)); return len; } diff --git a/native/src/base/misc.hpp b/native/src/base/misc.hpp index b7c241a0f..bf6c13f32 100644 --- a/native/src/base/misc.hpp +++ b/native/src/base/misc.hpp @@ -120,10 +120,15 @@ struct StringCmp { }; template -rust::Slice byte_slice(T *buf, size_t sz) { +rust::Slice u8_mut_slice(T *buf, size_t sz) { return rust::Slice(reinterpret_cast(buf), sz); } +template +rust::Slice u8_slice(T *buf, size_t sz) { + return rust::Slice(reinterpret_cast(buf), sz); +} + uint64_t parse_uint64_hex(std::string_view s); int parse_int(std::string_view s); diff --git a/native/src/sepolicy/api.cpp b/native/src/sepolicy/api.cpp index acaaee053..81de494c3 100644 --- a/native/src/sepolicy/api.cpp +++ b/native/src/sepolicy/api.cpp @@ -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(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(rules.data()), rules.length())); + rust::load_rules(*this, u8_slice(rules.data(), rules.length())); } diff --git a/native/src/sepolicy/lib.rs b/native/src/sepolicy/lib.rs index 74cfaaefe..e63f6d899 100644 --- a/native/src/sepolicy/lib.rs +++ b/native/src/sepolicy/lib.rs @@ -25,29 +25,44 @@ mod ffi { } } -fn load_rules_from_reader(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(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(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); }