From 32fca65f3a4d7328342ad8ab84c60752d66e1c79 Mon Sep 17 00:00:00 2001 From: merakor Date: Wed, 3 Jun 2020 19:24:46 +0000 Subject: kiss-stat: fix segfault This fixes an issue when the file doesn't have a registered owner in the passwd database, but we still tried to print it. We now check if the entry exists before we try printing it. FossilOrigin-Name: f34d077df5b31e7fc810e6b32e8fd526962e1268544661c535477eb8ff40db60 --- bin/kiss-stat.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/bin/kiss-stat.c b/bin/kiss-stat.c index 5d62855..8692904 100644 --- a/bin/kiss-stat.c +++ b/bin/kiss-stat.c @@ -14,19 +14,24 @@ int main (int argc, char *argv[]) { struct stat sb; - // Exit if no or multiple arguments are given + // Exit if no or multiple arguments are given. if (argc != 2 || strcmp(argv[1], "--help") == 0) { fprintf(stderr, "Usage: %s \n", argv[0]); return(1); } - // Exit if file stat cannot be obtained + // Exit if file stat cannot be obtained. if (lstat(argv[1], &sb) == -1) { perror(argv[0]); return(1); } - // Print the user name of file owner + // Exit if name of the owner cannot be retrieved. + if (!getpwuid(sb.st_uid)) { + return(1); + } + + // Print the user name of file owner. struct passwd *pw = getpwuid(sb.st_uid); printf("%s\n", pw->pw_name); return(0); -- cgit v1.2.3