From: Ingo Molnar <mingo@elte.hu>
To: Peter Seebach <peter.seebach@windriver.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>,
Anton Blanchard <anton@samba.org>,
paulus@samba.org, peterz@infradead.org, dsahern@gmail.com,
fweisbec@gmail.com, yanmin_zhang@linux.intel.com,
emunson@mgebm.net, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] perf: Incorrect use of snprintf results in SEGV
Date: Thu, 8 Mar 2012 08:48:37 +0100 [thread overview]
Message-ID: <20120308074837.GE20784@elte.hu> (raw)
In-Reply-To: <20120307151951.7e84adb6@wrlaptop>
* Peter Seebach <peter.seebach@windriver.com> wrote:
> On Wed, 7 Mar 2012 21:37:25 +0100
> Ingo Molnar <mingo@elte.hu> wrote:
>
> > You are missing two important aspects:
> >
> > 1) Dynamic reallocation on snprintf() failure is an utterly rare
> > thing - it is used in less than 1% of snprintf() invocations.
> > (Yes, I just checked a couple of codebases.)
>
> I would agree that it's very rare. But then, using the return
> value at all isn't especially common in my experience -- the
> only interesting part, most of the time, is "we're sure this
> didn't overrun the buffer".
Erm. Doing:
+= snprintf(...);
is a *very* common pattern within the kernel. It occurs more
than a thousand times - i.e. about 25% of all snprintf uses
(~5000 instances) within the kernel does care about the return
value.
I found only a single case that did a reallocation if the buffer
did not fit. Lets assume that I missed some and there's 4
altogether.
I.e. the API usage proportion, within the kernel project, looks
like this, approximately:
snprintf() call site that:
does not care about the return value: 75.0%
uses the return value as a 'written' count: 24.9%
wants to dynamically reallocate: 0.1%
> > We *DONT* want to make APIs more fragile just to accomodate a
> > rare, esoteric usecase!
>
> I would view snprintf as an API which already exists.
Changing it is obviously not possible anymore.
I was just countering your justification for it - which is still
wrong. People might read that and use it to justify newly
introduced, crappy APIs.
The 0.1% usecase is absolutely not a valid excuse to make an API
less robust - *especially* when a separate API could serve that
0.1% case just fine.
When designing APIs it is of utmost importance how average
developers intuitively *think* it works - not how the designer
thinks it should work ... Any severe mismatch between the two is
a serious design FAIL that should not be repeated in new code.
Thanks,
Ingo
next prev parent reply other threads:[~2012-03-08 7:49 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-07 0:42 [PATCH] perf: Incorrect use of snprintf results in SEGV Anton Blanchard
2012-03-07 0:49 ` Peter Seebach
2012-03-07 1:09 ` Arnaldo Carvalho de Melo
2012-03-07 1:29 ` Peter Seebach
2012-03-07 18:44 ` Nick Bowler
2012-03-07 20:24 ` Peter Seebach
2012-03-07 20:37 ` Ingo Molnar
2012-03-07 20:59 ` Peter Zijlstra
2012-03-07 21:28 ` Peter Seebach
2012-03-08 7:34 ` Ingo Molnar
2012-03-08 8:51 ` Peter Seebach
2012-03-07 21:19 ` Peter Seebach
2012-03-08 0:58 ` Arnaldo Carvalho de Melo
2012-03-08 7:48 ` Ingo Molnar [this message]
2012-03-08 7:52 ` Ingo Molnar
2012-03-09 19:00 ` Peter Seebach
2012-03-14 19:59 ` [tip:perf/urgent] perf tools: " tip-bot for Anton Blanchard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120308074837.GE20784@elte.hu \
--to=mingo@elte.hu \
--cc=acme@redhat.com \
--cc=anton@samba.org \
--cc=dsahern@gmail.com \
--cc=emunson@mgebm.net \
--cc=fweisbec@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=paulus@samba.org \
--cc=peter.seebach@windriver.com \
--cc=peterz@infradead.org \
--cc=yanmin_zhang@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.