Skip to content

Commit

Permalink
fix: no description causes segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
volitank authored and eatradish committed Mar 18, 2024
1 parent bd0b510 commit 4294ce8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
10 changes: 7 additions & 3 deletions apt-pkg-c/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ inline bool Version::is_downloadable() const noexcept { return ptr->Downloadable
inline bool Version::is_installed() const noexcept { return ptr->ParentPkg().CurrentVer() == *ptr; }

// This is for backend records lookups. You can also get package files from here.
inline DescriptionFile Version::unsafe_description_file() const noexcept {
return DescriptionFile{
std::make_unique<DescFileIterator>(ptr->TranslatedDescription().FileList())};
inline DescriptionFile Version::unsafe_description_file() const {
auto desc_file = ptr->TranslatedDescription();
// Must check if DescFileIterator is null first.
// See https://gitlab.com/volian/rust-apt/-/issues/28
if (desc_file.end()) { throw std::runtime_error("DescFile doesn't exist"); }

return DescriptionFile{std::make_unique<DescFileIterator>(desc_file.FileList())};
}

// You go through here to get the package files.
Expand Down
4 changes: 2 additions & 2 deletions src/raw/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub mod raw {

// This is for backend records lookups.
// You can also get package files from here.
pub fn unsafe_description_file(self: &Version) -> DescriptionFile;
pub fn unsafe_description_file(self: &Version) -> Result<DescriptionFile>;

// You go through here to get the package files.
pub fn unsafe_version_file(self: &Version) -> VersionFile;
Expand Down Expand Up @@ -343,7 +343,7 @@ impl raw::Version {
pub fn version_files(&self) -> Option<RawVersionFile> { self.unsafe_version_file().make_safe() }

pub fn description_files(&self) -> Option<RawDescriptionFile> {
self.unsafe_description_file().make_safe()
self.unsafe_description_file().ok()?.make_safe()
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ mod cache {
dbg!(cand_desc);
}

// This should not segfault, but has in the past.
// See https://gitlab.com/volian/rust-apt/-/issues/28
#[test]
fn no_description() {
let cache = new_cache!(&["tests/files/cache/no-description_0.0.1.deb"]).unwrap();

let pkg = cache.get("no-description").unwrap();
let cand = pkg.candidate().unwrap();
if let Some(desc) = cand.description() {
println!("{desc}");
}
}

#[test]
fn version_uris() {
let cache = new_cache!().unwrap();
Expand Down
7 changes: 7 additions & 0 deletions tests/files/cache/no-description_0.0.1/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Package: no-description
Version: 0.0.1
Section: base
Priority: optional
Architecture: all
Depends: htop, python3-rich
Maintainer: Your Name <[email protected]>

0 comments on commit 4294ce8

Please sign in to comment.