Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Use strlcpy instead of strncpy
Browse files Browse the repository at this point in the history
Also make sure the read cmdline is terminated with a null byte.

Change-Id: I6b4aa197ce9bc072a912b7163e8616a03b39c3fe
  • Loading branch information
kruton committed Dec 1, 2011
1 parent 6940ec4 commit b953fc2
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions toolbox/lsof.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ void lsof_dumpinfo(pid_t pid)
if (!stat(info.path, &pidstat)) {
pw = getpwuid(pidstat.st_uid);
if (pw) {
strncpy(info.user, pw->pw_name, USER_DISPLAY_MAX - 1);
info.user[USER_DISPLAY_MAX - 1] = '\0';
strlcpy(info.user, pw->pw_name, sizeof(info.user));
} else {
snprintf(info.user, USER_DISPLAY_MAX, "%d", (int)pidstat.st_uid);
}
Expand All @@ -194,18 +193,20 @@ void lsof_dumpinfo(pid_t pid)
fprintf(stderr, "Couldn't read %s\n", info.path);
return;
}

char cmdline[PATH_MAX];
if (read(fd, cmdline, sizeof(cmdline)) < 0) {
int numRead = read(fd, cmdline, sizeof(cmdline) - 1);
close(fd);

if (numRead < 0) {
fprintf(stderr, "Error reading cmdline: %s: %s\n", info.path, strerror(errno));
close(fd);
return;
}
close(fd);
info.path[info.parent_length] = '\0';

cmdline[numRead] = '\0';

// We only want the basename of the cmdline
strncpy(info.cmdline, basename(cmdline), sizeof(info.cmdline));
info.cmdline[sizeof(info.cmdline)-1] = '\0';
strlcpy(info.cmdline, basename(cmdline), sizeof(info.cmdline));

// Read each of these symlinks
print_type("cwd", &info);
Expand Down

0 comments on commit b953fc2

Please sign in to comment.