From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16492 invoked by alias); 18 Jul 2006 09:56:25 -0000 Received: (qmail 16482 invoked by uid 22791); 18 Jul 2006 09:56:24 -0000 X-Spam-Check-By: sourceware.org Received: from potter.codesourcery.com (HELO mail.codesourcery.com) (65.74.133.4) by sourceware.org (qpsmtpd/0.31) with ESMTP; Tue, 18 Jul 2006 09:56:23 +0000 Received: (qmail 15621 invoked from network); 18 Jul 2006 09:56:21 -0000 Received: from unknown (HELO zigzag.lvk.cs.msu.su) (vladimir@127.0.0.2) by mail.codesourcery.com with ESMTPA; 18 Jul 2006 09:56:21 -0000 From: Vladimir Prus To: gdb-patches@sources.redhat.com Subject: [PATCH] zero-terminate result of target_read_alloc Date: Tue, 18 Jul 2006 09:56:00 -0000 User-Agent: KMail/1.7.2 MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_ABLvEAIpZOYgAV7" Message-Id: <200607181356.16071.vladimir@codesourcery.com> Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2006-07/txt/msg00220.txt.bz2 --Boundary-00=_ABLvEAIpZOYgAV7 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Content-length: 327 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 * target.c (target_read_alloc): Zero-terminate result. --Boundary-00=_ABLvEAIpZOYgAV7 Content-Type: text/x-diff; charset="us-ascii"; name="2_target_read_alloc_zero_terminate.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="2_target_read_alloc_zero_terminate.diff" Content-length: 1449 --- 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); --Boundary-00=_ABLvEAIpZOYgAV7--