Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] zero-terminate result of target_read_alloc
@ 2006-07-18  9:56 Vladimir Prus
  2006-07-18 11:25 ` Mark Kettenis
  0 siblings, 1 reply; 15+ messages in thread
From: Vladimir Prus @ 2006-07-18  9:56 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 327 bytes --]


This patch makes result of target_read_alloc zero-terminated.
The point is that often the object is not allowed to contain embedded zeros,
and working with zero-terminated strings is much easier.

OK?

- Volodya

2006-07-18  Vladimir Prus  <vladimir@codesourcery.com>

	* target.c (target_read_alloc): Zero-terminate result.


[-- Attachment #2: 2_target_read_alloc_zero_terminate.diff --]
[-- Type: text/x-diff, Size: 1449 bytes --]

--- target.c	(revision 174)
+++ target.c	(revision 175)
@@ -1413,6 +1413,11 @@ target_write (struct target_ops *ops,
    sufficiently large buffer will be allocated using xmalloc and
    returned in *BUF_P containing the contents of the object.
 
+   The content will be zero terminated.  The caller should consider 
+   if object can legitimately contain embedded zero, and either use 
+   string operations, or use the returned object length when 
+   manupulating the buffer.
+
    This method should be used for objects sufficiently small to store
    in a single xmalloc'd buffer, when no fixed bound on the object's
    size is known in advance.  Don't try to read TARGET_OBJECT_MEMORY
@@ -1442,7 +1447,7 @@ target_read_alloc (struct target_ops *op
   while (1)
     {
       n = target_read_partial (ops, object, annex, &buf[buf_pos],
-			       buf_pos, buf_alloc - buf_pos);
+			       buf_pos, buf_alloc - buf_pos - 1);
       if (n < 0)
 	{
 	  /* An error occurred.  */
@@ -1455,14 +1460,17 @@ target_read_alloc (struct target_ops *op
 	  if (buf_pos == 0)
 	    xfree (buf);
 	  else
-	    *buf_p = buf;
+            {
+              buf[buf_pos] = '\0';
+              *buf_p = buf;
+            }
 	  return buf_pos;
 	}
 
       buf_pos += n;
 
       /* If the buffer is filling up, expand it.  */
-      if (buf_alloc < buf_pos * 2)
+      if (buf_pos >= buf_alloc - 1)
 	{
 	  buf_alloc *= 2;
 	  buf = xrealloc (buf, buf_alloc);

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2006-07-27 21:25 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-18  9:56 [PATCH] zero-terminate result of target_read_alloc Vladimir Prus
2006-07-18 11:25 ` Mark Kettenis
2006-07-18 11:33   ` Vladimir Prus
2006-07-18 12:34   ` Daniel Jacobowitz
2006-07-18 13:15     ` Mark Kettenis
2006-07-18 13:39       ` Daniel Jacobowitz
2006-07-18 13:51         ` Vladimir Prus
2006-07-18 19:51           ` Jim Blandy
2006-07-24  4:09   ` Daniel Jacobowitz
2006-07-24 21:41     ` Mark Kettenis
2006-07-24 22:00       ` Daniel Jacobowitz
2006-07-26 21:53         ` Mark Kettenis
2006-07-26 22:25           ` Daniel Jacobowitz
2006-07-26 22:36             ` Mark Kettenis
2006-07-27 21:25               ` Daniel Jacobowitz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox