aboutsummaryrefslogtreecommitdiffstats
path: root/toolbox
diff options
context:
space:
mode:
authorKenny Root <kroot@google.com>2011-12-01 11:38:53 -0800
committerKenny Root <kroot@google.com>2011-12-01 14:54:13 -0800
commitb953fc284983ac8eac9174bcfffedd18cb48ac7e (patch)
tree14853eb9e54beaf9c8a5b93a384f7c4f1d2f2028 /toolbox
parent6940ec41d1ed10157b61ba4967b1ab2e79fcb808 (diff)
downloadsystem_core-b953fc284983ac8eac9174bcfffedd18cb48ac7e.zip
system_core-b953fc284983ac8eac9174bcfffedd18cb48ac7e.tar.gz
system_core-b953fc284983ac8eac9174bcfffedd18cb48ac7e.tar.bz2
Use strlcpy instead of strncpy
Also make sure the read cmdline is terminated with a null byte. Change-Id: I6b4aa197ce9bc072a912b7163e8616a03b39c3fe
Diffstat (limited to 'toolbox')
-rw-r--r--toolbox/lsof.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/toolbox/lsof.c b/toolbox/lsof.c
index 4e2f77a..376a642 100644
--- a/toolbox/lsof.c
+++ b/toolbox/lsof.c
@@ -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);
}
@@ -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);