archlinux / alpm.rs

Rust bindings for libalpm
GNU General Public License v3.0
112 stars 21 forks source link

filename method of Package should return Option<&str> #18

Closed PhotonQuantum closed 3 years ago

PhotonQuantum commented 3 years ago

Local packages don't have a "filename" field. Therefore, calling filename() results in a panic.

Minimal reproducible example:

use alpm::Alpm;

fn main() {
    let alpm = Alpm::new("/", "/var/lib/pacman").unwrap();
    let pkg = alpm.localdb().pkgs().find_satisfier("systemd").unwrap();
    dbg!(pkg.name());
    dbg!(pkg.filename());
}

Output:

[src/main.rs:6] pkg.name() = "systemd"
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /home/lightquantum/.cargo/registry/src/github.com-1ecc6299db9ec823/alpm-2.0.0/src/utils.rs:7:10
stack backtrace:
   0: rust_begin_unwind
             at /rustc/657bc01888e6297257655585f9c475a0801db6d2/library/std/src/panicking.rs:515:5
   1: core::panicking::panic_fmt
             at /rustc/657bc01888e6297257655585f9c475a0801db6d2/library/core/src/panicking.rs:92:14
   2: core::panicking::panic
             at /rustc/657bc01888e6297257655585f9c475a0801db6d2/library/core/src/panicking.rs:50:5
   3: core::option::Option<T>::unwrap
             at /rustc/657bc01888e6297257655585f9c475a0801db6d2/library/core/src/option.rs:386:21
   4: alpm::utils::from_cstr
             at /home/lightquantum/.cargo/registry/src/github.com-1ecc6299db9ec823/alpm-2.0.0/src/utils.rs:5:5
   5: alpm::package::Pkg::filename
             at /home/lightquantum/.cargo/registry/src/github.com-1ecc6299db9ec823/alpm-2.0.0/src/package.rs:94:18
   6: alpm_rust_min_reprod::main
             at ./src/main.rs:7:10
   7: core::ops::function::FnOnce::call_once
             at /rustc/657bc01888e6297257655585f9c475a0801db6d2/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
fosskers commented 3 years ago

Good find.

Morganamilo commented 3 years ago

There's probably going to be a couple more of these. Paru is effected and I don't believe it calls that function.

fosskers commented 3 years ago

Aura doesn't as of yet either.

Morganamilo commented 3 years ago

"Fixed" with https://github.com/archlinux/alpm.rs/commit/6681b33cf1137ee56acc625e67141a3d89daae5b

Will hold off on a proper fix until I find all the functions that may return null and as I don't want to push a v3 just yet.

Morganamilo commented 3 years ago

This https://github.com/archlinux/alpm.rs/commit/c413be436c6848805a1e34f233c3ff4d50e6ea68#diff-6d974fec9cd167345df12f3e136a7be85c0f1bcfe60805940d14e881dda30b6c is my fix for now. Basically the same concept as my last comment but easier to strip out when it comes time for v3.

Also I believe the functions mentioned in #19 are the only functions that should be an option so I think this is all solved.