From 960ac434a4960b0524993544949ef892c08339ed Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Mon, 3 Jun 2013 12:10:30 -0700 Subject: [PATCH] fix strncat misuse The third argument of strncat() tells how much data to append from src, not the total size of the buffer. Change uses of strncat() to strlcat(), which has the buffer overflow protection behavior intended by the original author. This fixes the following compiler warning: In function 'strncat', inlined from 'print_type' at system/core/toolbox/lsof.c:76:12: bionic/libc/include/string.h:142:5: warning: call to __builtin___strncat_chk might overflow destination buffer [enabled by default] Change-Id: Id69edc641de3cb87d1867a409cd57b04b12f90a7 --- toolbox/lsof.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/toolbox/lsof.c b/toolbox/lsof.c index 376a642e7..113c120fc 100644 --- a/toolbox/lsof.c +++ b/toolbox/lsof.c @@ -54,7 +54,7 @@ struct pid_info_t { ssize_t parent_length; }; -void print_header() +static void print_header() { printf("%-9s %5s %10s %4s %9s %18s %9s %10s %s\n", "COMMAND", @@ -68,12 +68,12 @@ void print_header() "NAME"); } -void print_type(char *type, struct pid_info_t* info) +static void print_type(char *type, struct pid_info_t* info) { static ssize_t link_dest_size; static char link_dest[PATH_MAX]; - strncat(info->path, type, sizeof(info->path)); + strlcat(info->path, type, sizeof(info->path)); if ((link_dest_size = readlink(info->path, link_dest, sizeof(link_dest)-1)) < 0) { if (errno == ENOENT) goto out; @@ -96,7 +96,7 @@ out: } // Prints out all file that have been memory mapped -void print_maps(struct pid_info_t* info) +static void print_maps(struct pid_info_t* info) { FILE *maps; char buffer[PATH_MAX + 100]; @@ -107,7 +107,7 @@ void print_maps(struct pid_info_t* info) long int inode; char file[PATH_MAX]; - strncat(info->path, "maps", sizeof(info->path)); + strlcat(info->path, "maps", sizeof(info->path)); maps = fopen(info->path, "r"); if (!maps) @@ -131,10 +131,10 @@ out: } // Prints out all open file descriptors -void print_fds(struct pid_info_t* info) +static void print_fds(struct pid_info_t* info) { static char* fd_path = "fd/"; - strncat(info->path, fd_path, sizeof(info->path)); + strlcat(info->path, fd_path, sizeof(info->path)); int previous_length = info->parent_length; info->parent_length += strlen(fd_path); @@ -163,7 +163,7 @@ out: info->path[info->parent_length] = '\0'; } -void lsof_dumpinfo(pid_t pid) +static void lsof_dumpinfo(pid_t pid) { int fd; struct pid_info_t info; @@ -187,7 +187,7 @@ void lsof_dumpinfo(pid_t pid) } // Read the command line information; each argument is terminated with NULL. - strncat(info.path, "cmdline", sizeof(info.path)); + strlcat(info.path, "cmdline", sizeof(info.path)); fd = open(info.path, O_RDONLY); if (fd < 0) { fprintf(stderr, "Couldn't read %s\n", info.path);