--- 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);