[RFAv3 1/5] New cli-utils.h/.c function extract_info_print_args

Pedro Alves palves@redhat.com
Wed Oct 24 17:03:00 GMT 2018


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



More information about the Gdb-patches mailing list