From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 109180 invoked by alias); 24 Oct 2018 17:03:16 -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 108545 invoked by uid 89); 24 Oct 2018 17:03:09 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,SPF_HELO_PASS autolearn=no version=3.3.2 spammy=prints, Waroquiers, waroquiers, Philippe X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 24 Oct 2018 17:03:08 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 448EE308213F; Wed, 24 Oct 2018 17:03:07 +0000 (UTC) Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id A11625DD64; Wed, 24 Oct 2018 17:03:06 +0000 (UTC) Subject: Re: [RFAv3 1/5] New cli-utils.h/.c function extract_info_print_args To: Philippe Waroquiers , gdb-patches@sourceware.org References: <20180923214209.985-1-philippe.waroquiers@skynet.be> <20180923214209.985-2-philippe.waroquiers@skynet.be> <1540331965.12106.8.camel@skynet.be> From: Pedro Alves Message-ID: <71de39a2-81ed-ac7e-aa3a-dcd60341319e@redhat.com> Date: Wed, 24 Oct 2018 17:03:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <1540331965.12106.8.camel@skynet.be> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-SW-Source: 2018-10/txt/msg00552.txt.bz2 On 10/23/2018 10:59 PM, Philippe Waroquiers wrote: > On Mon, 2018-10-22 at 15:15 +0100, Pedro Alves wrote: >> +/* See documentation in cli-utils.h. */ >>> + >>> +const char* >>> +info_print_args_help (const char* prefix, >>> + const char* entity_kind) > > >> While at it, why return a copy of the string, instead of returning >> the std::string directly? > The returned value is used as argument to add_prefix_cmd, > that expects a char * that must stay valid when the std::string is destroyed. > So, at the call site, I cannot use info_print_args_help (...).c_str (), > as this gives a memory corruption (confirmed by valgrind). Ah. > So, I have done: > +const char * > +info_print_args_help (const char *prefix, > +                     const char *entity_kind) > +{ > +  /*  Note : this returns a string allocated with xstrdup, as this > +      is typically used as argument to add_prefix_cmd, which needs a > +      string that stays valid after destruction of the std::string.  */ > +  return xstrdup > +    (string_printf (_("\ > +%sIf NAMEREGEXP is provided, only prints the %s whose name\n   \ > .... > > Does this sound ok, or is there a better way to do (e.g. at the call site) ? Keep the function's interface, but use xstrprintf instead of 'xstrdup + string_printf' then. Thanks, Pedro Alves