Skip to content

Commit

Permalink
fix(unsafe): use safe version of method that verifies data. No perf i…
Browse files Browse the repository at this point in the history
…mpact noticeable
  • Loading branch information
zkat committed Sep 29, 2023
1 parent 7c56628 commit 1a6b54a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
5 changes: 5 additions & 0 deletions crates/nassun/src/error.rs
Expand Up @@ -165,6 +165,11 @@ pub enum NassunError {
#[diagnostic(code(nassun::cache::serialize), url(docsrs))]
SerializeCacheError(String),

/// An error happened while deserializing cache metadata.
#[error("Failed to deserialize cache metadata: {0}")]
#[diagnostic(code(nassun::cache::deserialize), url(docsrs))]
DeserializeCacheError(String),

/// A miscellaneous, usually internal error. This is used mainly to wrap
/// either manual InternalErrors, or those using external errors that
/// don't implement std::error::Error.
Expand Down
30 changes: 14 additions & 16 deletions crates/nassun/src/package.rs
Expand Up @@ -246,14 +246,13 @@ impl Package {
let name = self.name().to_owned();
async_std::task::spawn_blocking(move || {
let mut created = std::collections::HashSet::new();
let index = unsafe {
rkyv::util::archived_root::<TarballIndex>(
entry
.raw_metadata
.as_ref()
.ok_or_else(|| NassunError::CacheMissingIndexError(name))?,
)
};
let index = rkyv::check_archived_root::<TarballIndex>(
entry
.raw_metadata
.as_ref()
.ok_or_else(|| NassunError::CacheMissingIndexError(name))?,
)
.map_err(|e| NassunError::DeserializeCacheError(e.to_string()))?;
prefer_copy = index.should_copy || prefer_copy;
for (path, (sri, mode)) in index.files.iter() {
let sri: Integrity = sri.parse()?;
Expand Down Expand Up @@ -299,14 +298,13 @@ impl fmt::Debug for Package {

#[cfg(not(target_arch = "wasm32"))]
fn clean_from_cache(cache: &Path, sri: &Integrity, entry: cacache::Metadata) -> Result<()> {
let index = unsafe {
rkyv::util::archived_root::<TarballIndex>(
entry
.raw_metadata
.as_ref()
.ok_or_else(|| NassunError::CacheMissingIndexError("".into()))?,
)
};
let index = rkyv::check_archived_root::<TarballIndex>(
entry
.raw_metadata
.as_ref()
.ok_or_else(|| NassunError::CacheMissingIndexError("".into()))?,
)
.map_err(|e| NassunError::DeserializeCacheError(e.to_string()))?;
for (sri, _) in index.files.values() {
let sri: Integrity = sri.as_str().parse()?;
match cacache::remove_hash_sync(cache, &sri) {
Expand Down

0 comments on commit 1a6b54a

Please sign in to comment.