From: Alex Elder <elder@inktank.com>
To: Cong Ding <dinggnu@gmail.com>
Cc: Sage Weil <sage@inktank.com>,
"David S. Miller" <davem@davemloft.net>,
ceph-devel@vger.kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] net/ceph/osdmap.c: fix undefined behavior when using snprintf()
Date: Wed, 23 Jan 2013 11:47:54 -0600 [thread overview]
Message-ID: <5100224A.8010102@inktank.com> (raw)
In-Reply-To: <20130123174150.GA26336@gmail.com>
On 01/23/2013 11:41 AM, Cong Ding wrote:
> On Wed, Jan 23, 2013 at 10:48:07AM -0600, Alex Elder wrote:
>> On 01/22/2013 01:20 PM, Cong Ding wrote:
>>> The variable "str" is used as both the source and destination in function
>>> snprintf(), which is undefined behavior based on C11. The original description
>>> in C11 is:
>>> "If copying takes place between objects that
>>> overlap, the behavior is undefined."
>>
>> Yes, this was an ill-advised thing to do in this function.
>>
>> In fact, the only place this function is used (in osdmap_show()),
>> the non-static buffer was not initialized before the call. (It
>> might happen to work because the same stack space was getting
>> reused each time through the loop. Eeeeew!)
>>
>> This is just an awful couple of functions.
>>
>>> And, the function of ceph_osdmap_state_str() is to return the osdmap state, so
>>> it should return "doesn't exist" when all the conditions are not satisfied. I
>>> fix it in this patch.
>>>
>>> Based on C11, snprintf() does nothing if n==0:
>>> "If n is zero, nothing is written, and s may be a
>>> null pointer. Otherwise, output characters beyond
>>> the n-1st are discarded rather than being written to
>>> the array, and a null character is written at the
>>> end of the characters actually written into the
>>> array."
>>> so I remove the unnecessary check of len (because it is not a busy path and
>>> saves a few lines of code).
>>
>> True. But since you know it's not going to do anything why
>> not only make the call if len is non-zero? I.e.:
>>
>> else if (len)
>> snprintf(str, len, "doesn't exist");
>>
>> With your permission I'll make this change and will commit
>> this for you. OK?
> It's fine, thanks. But I think it's better to check len in the beginning
> because other conditions also call snprintf with parameter len. Like this:
OK. I'll do this. Thank you. -Alex
> if (!len)
> return str;
>
> if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP))
> snprintf(str, len, "exists, up");
> else if (state & CEPH_OSD_EXISTS)
> snprintf(str, len, "exists");
> else if (state & CEPH_OSD_UP)
> snprintf(str, len, "up");
> else
> snprintf(str, len, "doesn't exist");
>
> return str;
>
> or like this:
>
> if (len) {
> if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP))
> snprintf(str, len, "exists, up");
> else if (state & CEPH_OSD_EXISTS)
> snprintf(str, len, "exists");
> else if (state & CEPH_OSD_UP)
> snprintf(str, len, "up");
> else
> snprintf(str, len, "doesn't exist");
> }
> return str;
>
> Thanks,
> - cong
>
prev parent reply other threads:[~2013-01-23 17:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-22 19:20 [PATCH] net/ceph/osdmap.c: fix undefined behavior when using snprintf() Cong Ding
2013-01-23 16:48 ` Alex Elder
2013-01-23 17:41 ` Cong Ding
2013-01-23 17:47 ` Alex Elder [this message]
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=5100224A.8010102@inktank.com \
--to=elder@inktank.com \
--cc=ceph-devel@vger.kernel.org \
--cc=davem@davemloft.net \
--cc=dinggnu@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=sage@inktank.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