From 30ce69b5cb05712c3a4903b0f397912119c4f1f9 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 31 Oct 2021 13:26:57 +0200 Subject: [PATCH] Improve robustness of resolving user/group names in Stat --- source/fs/android/stat_owner.cpp | 26 ++++++++++++++++++-------- source/fs/stat_private.h | 4 ++-- source/fs/unix/stat_owner.cpp | 30 ++++++++++++++++++++---------- source/fs/unix/stat_platform.h | 3 +++ source/fs/windows/stat.cpp | 8 ++++---- source/fs/windows/stat_platform.h | 3 +++ 6 files changed, 50 insertions(+), 24 deletions(-) diff --git a/source/fs/android/stat_owner.cpp b/source/fs/android/stat_owner.cpp index 45fba30..48823e9 100644 --- a/source/fs/android/stat_owner.cpp +++ b/source/fs/android/stat_owner.cpp @@ -9,17 +9,27 @@ namespace FS { void Stat::Private::fill_owner_info(Stat::OwnerInfo &result) { - struct passwd *owner; - if((owner = getpwuid(owner_id))) - result.owner = owner->pw_name; + if(owner_id!=INVALID_UID) + { + struct passwd *owner; + if((owner = getpwuid(owner_id))) + result.owner = owner->pw_name; + else + result.owner = format("%d", owner_id); + } else - result.owner = format("%d", owner_id); + result.owner = "None"; - struct group *group; - if((group = getgrgid(group_id))) - result.group = group->gr_name; + if(group_id!=INVALID_GID) + { + struct group *group; + if((group = getgrgid(group_id))) + result.group = group->gr_name; + else + result.group = format("%d", group_id); + } else - result.group = format("%d", group_id); + result.group = "None"; } } // namespace FS diff --git a/source/fs/stat_private.h b/source/fs/stat_private.h index 7b20638..60438a2 100644 --- a/source/fs/stat_private.h +++ b/source/fs/stat_private.h @@ -8,8 +8,8 @@ namespace FS { struct Stat::Private { - UserID owner_id; - GroupID group_id; + UserID owner_id = INVALID_UID; + GroupID group_id = INVALID_GID; Private(); Private(const Private &); diff --git a/source/fs/unix/stat_owner.cpp b/source/fs/unix/stat_owner.cpp index 1a33d74..9c643d9 100644 --- a/source/fs/unix/stat_owner.cpp +++ b/source/fs/unix/stat_owner.cpp @@ -11,19 +11,29 @@ void Stat::Private::fill_owner_info(Stat::OwnerInfo &result) { char buf[1024]; - struct passwd pw; - struct passwd *owner; - if(!getpwuid_r(owner_id, &pw, buf, sizeof(buf), &owner) && owner) - result.owner = owner->pw_name; + if(owner_id!=INVALID_UID) + { + struct passwd pw; + struct passwd *owner; + if(!getpwuid_r(owner_id, &pw, buf, sizeof(buf), &owner) && owner) + result.owner = owner->pw_name; + else + result.owner = format("%d", owner_id); + } else - result.owner = format("%d", owner_id); + result.owner = "None"; - struct group gr; - struct group *group; - if(!getgrgid_r(group_id, &gr, buf, sizeof(buf), &group) && group) - result.group = group->gr_name; + if(group_id!=INVALID_GID) + { + struct group gr; + struct group *group; + if(!getgrgid_r(group_id, &gr, buf, sizeof(buf), &group) && group) + result.group = group->gr_name; + else + result.group = format("%d", group_id); + } else - result.group = format("%d", group_id); + result.group = "None"; } } // namespace FS diff --git a/source/fs/unix/stat_platform.h b/source/fs/unix/stat_platform.h index cc3f095..d888fed 100644 --- a/source/fs/unix/stat_platform.h +++ b/source/fs/unix/stat_platform.h @@ -9,6 +9,9 @@ namespace FS { typedef uid_t UserID; typedef gid_t GroupID; +const UserID INVALID_UID = -1; +const GroupID INVALID_GID = -1; + } // namespace FS } // namespace Msp diff --git a/source/fs/windows/stat.cpp b/source/fs/windows/stat.cpp index ac9e0c9..36f28a0 100644 --- a/source/fs/windows/stat.cpp +++ b/source/fs/windows/stat.cpp @@ -50,20 +50,20 @@ Stat::Private::Private(const Private &other): Stat::Private::~Private() { - if(owner_id) + if(owner_id!=INVALID_UID) HeapFree(GetProcessHeap(), 0, owner_id); - if(group_id) + if(group_id!=INVALID_GID) HeapFree(GetProcessHeap(), 0, group_id); } void Stat::Private::fill_owner_info(Stat::OwnerInfo &result) { - if(owner_id) + if(owner_id!=INVALID_UID) result.owner = get_account_name(owner_id); else result.owner = "None"; - if(group_id) + if(group_id!=INVALID_GID) result.group = get_account_name(group_id); else result.group = "None"; diff --git a/source/fs/windows/stat_platform.h b/source/fs/windows/stat_platform.h index 4e8cfbc..c193acc 100644 --- a/source/fs/windows/stat_platform.h +++ b/source/fs/windows/stat_platform.h @@ -9,6 +9,9 @@ namespace FS { typedef PSID UserID; typedef PSID GroupID; +const UserID INVALID_UID = 0; +const GroupID INVALID_GID = 0; + } // namespace FS } // namespace Msp -- 2.45.2