From: Laszlo Ersek <lersek@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] check for available room when formatting OpenFirmware device path
Date: Mon, 23 Jul 2012 17:29:19 +0200 [thread overview]
Message-ID: <500D6DCF.2090905@redhat.com> (raw)
In-Reply-To: <87394ibjy6.fsf@blackfin.pond.sub.org>
On 07/23/12 17:01, Markus Armbruster wrote:
> Laszlo Ersek <lersek@redhat.com> writes:
>
>> On 07/23/12 14:46, Markus Armbruster wrote:
>>> Laszlo Ersek <lersek@redhat.com> writes:
>>>
>>>> Signed-off-by: Laszlo Ersek <lersek@redhat.com>
>>>> ---
>>>> hw/qdev.c | 14 +++++++++++++-
>>>> vl.c | 7 ++++++-
>>>> 2 files changed, 19 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/hw/qdev.c b/hw/qdev.c
>>>> index af54467..f1e83a4 100644
>>>> --- a/hw/qdev.c
>>>> +++ b/hw/qdev.c
>>>> @@ -502,6 +502,10 @@ static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
>>>> if (dev && dev->parent_bus) {
>>>> char *d;
>>>> l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
>>>> + if (l >= size) {
>>>> + return l;
>>>> + }
>>>> +
>>>> d = bus_get_fw_dev_path(dev->parent_bus, dev);
>>>> if (d) {
>>>> l += snprintf(p + l, size - l, "%s", d);
>>>> @@ -509,6 +513,10 @@ static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
>>>> } else {
>>>> l += snprintf(p + l, size - l, "%s", object_get_typename(OBJECT(dev)));
>>>> }
>>>> +
>>>> + if (l >= size) {
>>>> + return l;
>>>> + }
>>>> }
>>>> l += snprintf(p + l , size - l, "/");
>>>>
>>>
>>> If the return value is less than the size argument, it's the length of
>>> the string written into p[]. Else, it means p[] has insufficient
>>> space.
>>
>> Yes. (snprintf() returns the number of bytes it would store, excluding
>> the terminating NUL, had there been enough room.
>> <http://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html#tag_16_159_04>)
>>
>> Did I make a mistake?
>>
>> Supposing snprintf() encounters no error, it returns a positive value P
>> in the above.
>>
>> P = snprintf(..., size - l0, ...)
>> l1 = l0 + P;
>>
>> l1 >= size
>> <-> l0 + P >= size
>> <-> P >= size - l0
>>
>>
>> The return value of qdev_get_fw_dev_path_helper() comes from another
>> invocation of itself, or from the trailing snprintf(), so it behaves
>> like snprintf.
>>
>> Or what do you have in mind?
>
> If I read your code correctly, qdev_get_fw_dev_path_helper() bails out
> after the first snprintf() that goes beyond the buffer size. When that
> happens before the job's done, the return value is less than the length
> of the full path.
Yes, but still greater than what would fit in the buffer. Is it a problem?
... Oh, did you mean the first comment in connection with the second?
Ie. we should continue to format (just for length counting's sake) and
then retry? IOW the early return isn't problematic in itself, but it
doesn't support the reallocation?
Thanks,
Laszlo
next prev parent reply other threads:[~2012-07-23 15:28 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-07-23 11:33 [Qemu-devel] [PATCH] check for available room when formatting OpenFirmware device path Laszlo Ersek
2012-07-23 12:34 ` Peter Maydell
2012-07-23 12:39 ` Peter Maydell
2012-07-23 12:46 ` Markus Armbruster
2012-07-23 13:08 ` Laszlo Ersek
2012-07-23 15:01 ` Markus Armbruster
2012-07-23 15:29 ` Laszlo Ersek [this message]
2012-07-23 15:42 ` Markus Armbruster
2012-07-25 0:19 ` [Qemu-devel] [PATCH v2 0/2] assorted device path formatting cleanups Laszlo Ersek
2012-07-25 0:19 ` [Qemu-devel] [PATCH v2 1/2] accomodate OpenFirmware device paths in sufficient storage Laszlo Ersek
2012-07-25 7:34 ` Peter Maydell
2012-07-25 8:11 ` Markus Armbruster
2012-07-25 0:19 ` [Qemu-devel] [PATCH v2 2/2] get_fw_dev_path() impls should allocate memory with glib functions Laszlo Ersek
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=500D6DCF.2090905@redhat.com \
--to=lersek@redhat.com \
--cc=armbru@redhat.com \
--cc=qemu-devel@nongnu.org \
/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;
as well as URLs for NNTP newsgroup(s).