commit 4d1dfb082e27e8a66be7d758c2021b68f05079ba
parent dd584890e131648f74496768b7ee7e431ca988f6
Author: Cem Keylan <cem@ckyln.com>
Date: Wed, 3 Jun 2020 22:24:46 +0300
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.
Diffstat:
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git 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 <pathname>\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);