From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 72682 invoked by alias); 19 Oct 2016 17:18:02 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 72653 invoked by uid 89); 19 Oct 2016 17:18:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_SOFTFAIL autolearn=no version=3.3.2 spammy=H*r:112, resize, H*UA:1.2.0, H*u:1.2.0 X-HELO: simark.ca Received: from simark.ca (HELO simark.ca) (158.69.221.121) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 19 Oct 2016 17:17:59 +0000 Received: by simark.ca (Postfix, from userid 112) id 9A7D31E486; Wed, 19 Oct 2016 13:17:57 -0400 (EDT) Received: from simark.ca (localhost [127.0.0.1]) by simark.ca (Postfix) with ESMTP id E72BE1E109; Wed, 19 Oct 2016 13:17:55 -0400 (EDT) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Wed, 19 Oct 2016 17:18:00 -0000 From: Simon Marchi To: Pedro Alves Cc: gdb-patches@sourceware.org Subject: Re: [PATCH v2 01/31] Introduce string_printf In-Reply-To: <1476839539-8374-2-git-send-email-palves@redhat.com> References: <1476839539-8374-1-git-send-email-palves@redhat.com> <1476839539-8374-2-git-send-email-palves@redhat.com> Message-ID: <8d8aa4e4f950dad9474f3fe17a14eee7@simark.ca> X-Sender: simon.marchi@polymtl.ca User-Agent: Roundcube Webmail/1.2.0 X-IsSubscribed: yes X-SW-Source: 2016-10/txt/msg00573.txt.bz2 On 2016-10-18 21:11, Pedro Alves wrote: > This introduces the string_printf function. Like asprintf, but > returns a std::string. > > gdb/ChangeLog: > yyyy-mm-yy Pedro Alves > > * common/common-utils.c (string_printf): New function. > * common/common-utils.h: Include . > (string_printf): Declare. > --- > gdb/common/common-utils.c | 30 ++++++++++++++++++++++++++++++ > gdb/common/common-utils.h | 6 ++++++ > 2 files changed, 36 insertions(+) > > diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c > index 5a346ec..05ba3aa 100644 > --- a/gdb/common/common-utils.c > +++ b/gdb/common/common-utils.c > @@ -150,6 +150,36 @@ xsnprintf (char *str, size_t size, const char > *format, ...) > return ret; > } > > +/* See documentation in common-utils.h. */ > + > +std::string > +string_printf (const char* fmt, ...) > +{ > + std::string str; > + va_list vp; > + > + /* Start by assuming some reasonable size will be sufficient. */ > + str.resize (1024); > + > + while (1) > + { > + size_t size; > + int result; > + > + va_start (vp, fmt); > + size = str.size (); > + result = vsnprintf (&str[0], size, fmt, vp); > + va_end (vp); > + > + str.resize (result); I think you have an off-by-one here, which causes an infinite loop if the 1024 bytes buffer is not large enough. vsnprintf returns the size needed without the terminating \0, so the resize here should be of "result + 1". To reproduce/test easily, just change your str.resize (1024) to something small. I thought the use of a while loop for this a bit strange, but I understand it's to avoid duplicating the code. I think you should try writing a unit test for this :). Simon