From: Florian Krohm Date: Fri, 19 Dec 2014 20:25:46 +0000 (+0000) Subject: Fix a buffer overflow (in case read had returned 1000). X-Git-Tag: svn/VALGRIND_3_11_0~763 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1faad1a40c41fbfc4efccfdf48c88530a641f6d1;p=thirdparty%2Fvalgrind.git Fix a buffer overflow (in case read had returned 1000). Write out the whole command line. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14823 --- diff --git a/coregrind/vgdb.c b/coregrind/vgdb.c index 82639cc9b7..2ca284970e 100644 --- a/coregrind/vgdb.c +++ b/coregrind/vgdb.c @@ -1066,33 +1066,39 @@ void standalone_send_commands(int pid, static void report_pid (int pid, Bool on_stdout) { - char cmdline_file[100]; - char cmdline[1000]; - int fd; - int i, sz; + char cmdline_file[50]; // large enough + int fd, i; + FILE *out = on_stdout ? stdout : stderr; sprintf(cmdline_file, "/proc/%d/cmdline", pid); fd = open (cmdline_file, O_RDONLY); if (fd == -1) { DEBUG(1, "error opening cmdline file %s %s\n", cmdline_file, strerror(errno)); - sprintf(cmdline, "(could not open process command line)"); + XERROR(errno, "could not open process command line for pid %d\n", pid); } else { - sz = read(fd, cmdline, 1000); - for (i = 0; i < sz; i++) - if (cmdline[i] == 0) - cmdline[i] = ' '; - if (sz >= 0) - cmdline[sz] = 0; - else { - DEBUG(1, "error reading cmdline file %s %s\n", - cmdline_file, strerror(errno)); - sprintf(cmdline, "(could not read process command line)"); + char cmdline[100]; + ssize_t sz; + + fprintf(out, "use --pid=%d for ", pid); + while ((sz = read(fd, cmdline, sizeof cmdline - 1)) != 0) { + if (sz == -1) { + DEBUG(1, "error reading cmdline file %s %s\n", + cmdline_file, strerror(errno)); + XERROR(errno, "could not read process command line for pid %d\n", + pid); + } else { + for (i = 0; i < sz; i++) + if (cmdline[i] == 0) + cmdline[i] = ' '; + cmdline[sz] = 0; + fprintf(out, "%s", cmdline); + } } + fprintf(out, "\n"); close (fd); } - fprintf((on_stdout ? stdout : stderr), "use --pid=%d for %s\n", pid, cmdline); - fflush((on_stdout ? stdout : stderr)); + fflush(out); } static