[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAMuHMdWu_N6=qvQT1Z794XtAGUZ7kbNvspzE0HH4ZLdiD0_whA@mail.gmail.com>
Date: Mon, 20 Oct 2014 21:33:42 +0200
From: Geert Uytterhoeven <geert@...ux-m68k.org>
To: Jiri Olsa <jolsa@...hat.com>
Cc: Andi Kleen <andi@...stfloor.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
namhyung@...nel.org, acme@...nel.org,
Andi Kleen <ak@...ux.intel.com>
Subject: Re: [PATCH 8/8] tools, perf: Add asprintf replacement
On Mon, Oct 20, 2014 at 9:21 PM, Jiri Olsa <jolsa@...hat.com> wrote:
> On Mon, Oct 20, 2014 at 09:13:58PM +0200, Geert Uytterhoeven wrote:
>> On Mon, Oct 20, 2014 at 8:28 PM, Jiri Olsa <jolsa@...hat.com> wrote:
>> >> +int vasprintf(char **str, const char *fmt, va_list ap)
>> >> +{
>> >> + char buf[1024];
>> >
>> > could you please make it work for generic buf len?
>>
>> The actual size above doesn't matter, except for stack usage.
>> It could be 1 (are there any bugs triggered when using zero? ;-).
>> The real buffer is allocated by malloc() below.
>>
>> >> + int len = vsnprintf(buf, sizeof buf, fmt, ap);
>
> hum, really? the message is formated in here ^^^
> into buffer on stack 'buf[1024]'
>
>> >
>> > WARNING: sizeof buf should be sizeof(buf)
>> >
>> >> +
>> >> + *str = malloc(len + 1);
>> >> + if (!*str)
>> >> + return -1;
>> >> + strcpy(*str, buf);
>
> and copied into allocated buffer in here
Bummer, I should have read this more carefully...
In a typical asprintf() implementation, the string is not copied, but
formatted again, now the needed buffer size is known.
So if you replace the strcpy() by
vsnprintf(*str, len + 1, fmt, ap);
(and add a va_copy(), of course!), it'll work for any needed length.
Note that the strcpy() can still be done as an optimization, if len <=
sizeof(buf).
> the buf size 1024 limits the maximum formated string length to 1024,
> which is probably not crossed by current perf usage.. but still making
> it generic is not that hard
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@...ux-m68k.org
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists