Increase buffer size, so that long debugger command lines don't crash
V.  Also add buffer overrun checks.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@5368 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/m_debugger.c b/coregrind/m_debugger.c
index 9a5cc0d..b527934 100644
--- a/coregrind/m_debugger.c
+++ b/coregrind/m_debugger.c
@@ -146,9 +146,10 @@
    continue, quit the debugger.  */
 void VG_(start_debugger) ( ThreadId tid )
 {
-  Int pid;
+#  define N_BUF 4096
+   Int pid;
 
-  if ((pid = VG_(fork)()) == 0) {
+   if ((pid = VG_(fork)()) == 0) {
       VG_(ptrace)(VKI_PTRACE_TRACEME, 0, NULL, NULL);
       VG_(kill)(VG_(getpid)(), VKI_SIGSTOP);
 
@@ -163,8 +164,8 @@
           VG_(ptrace)(VKI_PTRACE_DETACH, pid, NULL, 0) == 0)
       {
          Char pidbuf[15];
-         Char file[30];
-         Char buf[100];
+         Char file[50];
+         Char buf[N_BUF];
          Char *bufptr;
          Char *cmdptr;
          
@@ -175,6 +176,10 @@
          cmdptr = VG_(clo_db_command);
          
          while (*cmdptr) {
+            /* each iteration can advance bufptr by at most the length
+               of file[], so the following assertion is generously
+               over-paranoid. */
+            vg_assert(bufptr - buf < N_BUF-15-50-10/*paranoia*/);
             switch (*cmdptr) {
                case '%':
                   switch (*++cmdptr) {
@@ -183,20 +188,21 @@
                         bufptr += VG_(strlen)(file);
                         cmdptr++;
                         break;
-                  case 'p':
-                     VG_(memcpy)(bufptr, pidbuf, VG_(strlen)(pidbuf));
-                     bufptr += VG_(strlen)(pidbuf);
-                     cmdptr++;
-                     break;
-                  default:
-                     *bufptr++ = *cmdptr++;
-                     break;
+                     case 'p':
+                        VG_(memcpy)(bufptr, pidbuf, VG_(strlen)(pidbuf));
+                        bufptr += VG_(strlen)(pidbuf);
+                        cmdptr++;
+                        break;
+                     default:
+                        *bufptr++ = *cmdptr++;
+                        break;
                   }
                   break;
                default:
                   *bufptr++ = *cmdptr++;
                   break;
             }
+            vg_assert(bufptr - buf < N_BUF-15-50-10/*paranoia*/);
          }
          
          *bufptr++ = '\0';
@@ -216,6 +222,7 @@
       VG_(kill)(pid, VKI_SIGKILL);
       VG_(waitpid)(pid, &status, 0);
    }
+#  undef N_BUF
 }