This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[commit/60] fix buffer full test


Hello,

Ref: http://sources.redhat.com/ml/gdb-patches/2002-03/msg00113.html

The attached should fix the buffer full bug that fred found in remote.c. vis:

(gdb) set *(long long*)0 = 1
Sending packet: $X0,7:\001\000\000\000\000\000\000#26...Ack
Packet received: OK
Sending packet: $X7,1:\000#26...Ack
Packet received: OK
(gdb) set remotewritesize 17
(gdb) set *(long long*)0 = 1
Sending packet: $X0,8:\001\000\000\000\000\000\000\000#27...Ack
Packet received: OK

the first one contains exactly 16 characters, the second 17.

commmitted to 6.0 and mainline.

Andrew
2003-06-30  Andrew Cagney  <cagney@redhat.com>

	* remote.c (remote_write_bytes): Explicitly compute and then use
	the payload size.  Update comments to reflect.  Fixes problem of
	GDB not sending small packets as found by Fred Fish.

Index: remote.c
===================================================================
RCS file: /cvs/src/src/gdb/remote.c,v
retrieving revision 1.109
diff -u -r1.109 remote.c
--- remote.c	30 Jun 2003 15:03:07 -0000	1.109
+++ remote.c	30 Jun 2003 15:49:38 -0000
@@ -3758,42 +3758,45 @@
 remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
 {
   unsigned char *buf;
-  int max_buf_size;		/* Max size of packet output buffer */
   unsigned char *p;
   unsigned char *plen;
   long sizeof_buf;
   int plenlen;
   int todo;
   int nr_bytes;
+  int payload_size;
+  unsigned char *payload_start;
 
-  /* Verify that the target can support a binary download */
+  /* Verify that the target can support a binary download.  */
   check_binary_download (memaddr);
 
-  /* Determine the max packet size. */
-  max_buf_size = get_memory_write_packet_size ();
-  sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
+  /* Compute the size, and then allocate space for the largest
+     possible packet.  Include space for an extra trailing NUL.  */
+  sizeof_buf = get_memory_write_packet_size () + 1;
   buf = alloca (sizeof_buf);
 
-  /* Subtract header overhead from max payload size -  $M<memaddr>,<len>:#nn */
-  max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
+  /* Compute the size of the actual payload by subtracting out the
+     packet header and footer overhead: "$M<memaddr>,<len>:...#nn".  */
+  payload_size = (get_memory_write_packet_size () - (strlen ("$M,:#NN")
+						     + hexnumlen (memaddr)
+						     + hexnumlen (len)));
 
-  /* construct "M"<memaddr>","<len>":" */
-  /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
-  p = buf;
+  /* Construct the packet header: "[MX]<memaddr>,<len>:".   */
 
-  /* Append [XM].  Compute a best guess of the number of bytes
+  /* Append "[XM]".  Compute a best guess of the number of bytes
      actually transfered. */
+  p = buf;
   switch (remote_protocol_binary_download.support)
     {
     case PACKET_ENABLE:
       *p++ = 'X';
       /* Best guess at number of bytes that will fit. */
-      todo = min (len, max_buf_size);
+      todo = min (len, payload_size);
       break;
     case PACKET_DISABLE:
       *p++ = 'M';
       /* num bytes that will fit */
-      todo = min (len, max_buf_size / 2);
+      todo = min (len, payload_size / 2);
       break;
     case PACKET_SUPPORT_UNKNOWN:
       internal_error (__FILE__, __LINE__,
@@ -3802,20 +3805,25 @@
       internal_error (__FILE__, __LINE__, "bad switch");
     }
   
-  /* Append <memaddr> */
+  /* Append "<memaddr>".  */
   memaddr = remote_address_masked (memaddr);
   p += hexnumstr (p, (ULONGEST) memaddr);
+
+  /* Append ",".  */
   *p++ = ',';
   
-  /* Append <len>.  Retain the location/size of <len>.  It may
-     need to be adjusted once the packet body has been created. */
+  /* Append <len>.  Retain the location/size of <len>.  It may need to
+     be adjusted once the packet body has been created.  */
   plen = p;
   plenlen = hexnumstr (p, (ULONGEST) todo);
   p += plenlen;
+
+  /* Append ":".  */
   *p++ = ':';
   *p = '\0';
   
-  /* Append the packet body. */
+  /* Append the packet body.  */
+  payload_start = p;
   switch (remote_protocol_binary_download.support)
     {
     case PACKET_ENABLE:
@@ -3823,7 +3831,7 @@
 	 increasing byte addresses.  Only escape certain critical
 	 characters.  */
       for (nr_bytes = 0;
-	   (nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
+	   (nr_bytes < todo) && (p - payload_start) < payload_size;
 	   nr_bytes++)
 	{
 	  switch (myaddr[nr_bytes] & 0xff)
@@ -3846,7 +3854,6 @@
 	     and we have actually sent fewer bytes than planned.
 	     Fix-up the length field of the packet.  Use the same
 	     number of characters as before.  */
-	  
 	  plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
 	  *plen = ':';  /* overwrite \0 from hexnumnstr() */
 	}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]