From d19a9e303928b3be096db583097b219f8494bc28 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Thu, 8 May 2025 17:55:05 +0200 Subject: [PATCH] libstore/profiles: do not dereference current generation if it doesn't exist If the profile inode is invalid, e.g. invalid symlink, the current generation cannot be discovered. Nonetheless, this should not be a reason for an assert failure, instead of crashing, just raise an error. Fixes fj#801. Change-Id: I63937672173bc3bf37196de98307800adc5757e1 Signed-off-by: Raito Bezarius Co-authored-by: Qyriad --- lix/libstore/profiles.cc | 5 ++++- tests/functional/common/vars-and-functions.sh.in | 5 +++++ tests/functional/user-envs.sh | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lix/libstore/profiles.cc b/lix/libstore/profiles.cc index 0482fc1bb..aa511fd86 100644 --- a/lix/libstore/profiles.cc +++ b/lix/libstore/profiles.cc @@ -153,8 +153,11 @@ void deleteGenerations( auto [gens, curGen] = findGenerations(profile); + if (!curGen) + throw Error("cannot find current generation of profile '%1%', is it a valid symlink?", profile); + if (gensToDelete.count(*curGen)) - throw Error("cannot delete current version of profile %1%'", profile); + throw Error("cannot delete current version of profile '%1%'", profile); for (auto & i : gens) { if (!gensToDelete.count(i.number)) continue; diff --git a/tests/functional/common/vars-and-functions.sh.in b/tests/functional/common/vars-and-functions.sh.in index 99a859f49..647926efa 100644 --- a/tests/functional/common/vars-and-functions.sh.in +++ b/tests/functional/common/vars-and-functions.sh.in @@ -68,6 +68,11 @@ clearProfiles() { rm -rf "$profiles" } +linkProfilesTo() { + target="$1" + ln -s "$HOME"/.local/state/nix/profiles "$target" +} + clearStore() { echo "clearing store..." chmod -R +w "$NIX_STORE_DIR" diff --git a/tests/functional/user-envs.sh b/tests/functional/user-envs.sh index bb48a8430..14c87ea69 100644 --- a/tests/functional/user-envs.sh +++ b/tests/functional/user-envs.sh @@ -4,6 +4,13 @@ if [ -z "${storeCleared-}" ]; then clearStore fi +# Test if invalid profiles symlinks are supported. +# https://git.lix.systems/lix-project/lix/issues/801 +VOID=$(mktemp -d) +rm -rf "$VOID" +linkProfilesTo $VOID +expectStderr 1 nix-env --delete-generations 0 | grepQuiet -E "error: cannot find current generation of profile '.*local/state/nix/profiles/profile', is it a valid symlink?" + clearProfiles # Query installed: should be empty.