public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Seebach <peter.seebach@windriver.com>
To: Ingo Molnar <mingo@elte.hu>
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: Wed, 7 Mar 2012 15:19:51 -0600	[thread overview]
Message-ID: <20120307151951.7e84adb6@wrlaptop> (raw)
In-Reply-To: <20120307203725.GA4333@elte.hu>

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".
 
>    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.  If it's the
wrong API, by all means, write a different one -- but I would suggest
not using the same name for it.  If a function is going to be called
snprintf, IMO it should have the semantics of snprintf.  If those are
the wrong semantics (and they may well be), then I would say use a
function which has the right semantics, and isn't named snprintf.
 
> 2) It's not even true that should some code want to
>    dynamically allocate the 'required' number of bytes is not
>    available. Some oddball side API could be added for that 1%:
 
> 	size_needed = snprintf_size(...);

That's where the "can write one in terms of the other" argument comes
into play.

If you have snprintf_needed(), it's easy to write snprintf_written()
in terms of it.  If you have only snprintf_written(), it is unreasonably
ugly and/or inefficient to write snprintf_needed() in terms of it.

> So this API could have been designed right but it was messed up 
> out of concern for an insane 1% case - FAIL.

Well, the thing is.  If there *exists* a reallocation case, those
semantics end up being needed.

I do agree that this is a source of errors in usage (and a quick audit
shows that I have at least one use which falls prey to this, as well as
several which check for it correctly).  In practice, I'd guess that
treating probably-negative sizes as an error would likely resolve
things, although that's also a semantics change -- it's just that it's
a semantics change which only affects single snprintf calls that were
expected to write to half the address space.

-s
-- 
Listen, get this.  Nobody with a good compiler needs to be justified.

  parent reply	other threads:[~2012-03-07 21:20 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 [this message]
2012-03-08  0:58         ` Arnaldo Carvalho de Melo
2012-03-08  7:48         ` Ingo Molnar
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=20120307151951.7e84adb6@wrlaptop \
    --to=peter.seebach@windriver.com \
    --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=mingo@elte.hu \
    --cc=paulus@samba.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox