From: David Vrabel <david.vrabel@citrix.com>
To: Paul Durrant <Paul.Durrant@citrix.com>
Cc: "xen-devel@lists.xensource.com" <xen-devel@lists.xensource.com>,
David Vrabel <david.vrabel@citrix.com>
Subject: Re: [PATCH] Convert hvmloader sprintf() into snprintf()
Date: Tue, 29 Nov 2011 14:19:36 +0000 [thread overview]
Message-ID: <4ED4E9F8.90208@citrix.com> (raw)
In-Reply-To: <291EDFCB1E9E224A99088639C4762022B5988E4F0C@LONPMAILBOX01.citrite.net>
On 29/11/11 14:12, Paul Durrant wrote:
>> -----Original Message-----
>> From: David Vrabel
>> Sent: 29 November 2011 13:57
>> To: Paul Durrant
>> Cc: xen-devel@lists.xensource.com
>> Subject: Re: [Xen-devel] [PATCH] Convert hvmloader sprintf() into
>> snprintf()
>>
> [snip]
>>> +static void __copy(void *arg, char c)
>>> {
>>> - **buf = c;
>>> - (*buf)++;
>>> + struct __copy_context *ctxt = arg;
>>> +
>>> + ctxt->emitted++;
>>> +
>>> + if (ctxt->remaining == 0)
>>> + return;
>>> +
>>> + *(ctxt->ptr++) = c;
>>> + --ctxt->remaining;
>>> }
>>>
>>> -int sprintf(char *buf, const char *fmt, ...)
>>> +int snprintf(char *buf, size_t size, const char *fmt, ...)
>>> {
>>> va_list ap;
>>> + struct __copy_context ctxt;
>>> +
>>> + ctxt.ptr = buf;
>>> + ctxt.emitted = 0;
>>> + ctxt.remaining = size;
>>>
>>> va_start(ap, fmt);
>>> - _doprint(__copy, &buf, fmt, ap);
>>> + _doprint(__copy, &ctxt, fmt, ap);
>>> va_end(ap);
>>>
>>> - *buf = '\0';
>>> - return 0;
>>> + if (ctxt.remaining != 0)
>>> + *ctxt.ptr = '\0';
>>> +
>>> + return ctxt.emitted;
>>> }
>>
>> This doesn't return the correct value according the C99. From the
>> snprintf() man page:
>>
>> "The functions snprintf() and vsnprintf() do not write more than
>> size bytes (including the trailing '\0'). If the output was
>> truncated due to this limit then the return value is the number of
>> characters (not including the trailing '\0') which would have been
>> written to the final string if enough space had been available.
>> Thus, a return value of size or more means that the output
>> was truncated."
>>
>
> Actually, reading the code again, it is correct isn't it?
> ctxt.emitted is bumped for every character emitted by _doprint()
> regardless of whether it makes it into the buffer or not so in an
> overflow case the value returned will be the number of characters
> which would have been written not including the nul terminator.
Er. Yes, it is correct. My mistake.
David
prev parent reply other threads:[~2011-11-29 14:19 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-29 13:42 [PATCH] Convert hvmloader sprintf() into snprintf() Paul Durrant
2011-11-29 13:57 ` David Vrabel
2011-11-29 14:03 ` Paul Durrant
2011-11-29 14:10 ` David Vrabel
2011-11-29 14:13 ` Paul Durrant
2011-11-29 14:12 ` Paul Durrant
2011-11-29 14:19 ` David Vrabel [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=4ED4E9F8.90208@citrix.com \
--to=david.vrabel@citrix.com \
--cc=Paul.Durrant@citrix.com \
--cc=xen-devel@lists.xensource.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.