qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
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 15:08:36 +0200	[thread overview]
Message-ID: <500D4CD4.5030706@redhat.com> (raw)
In-Reply-To: <874noyhcfz.fsf@blackfin.pond.sub.org>

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?


> 
>> @@ -520,8 +528,12 @@ char* qdev_get_fw_dev_path(DeviceState *dev)
>>      char path[128];
>>      int l;
>>  
>> -    l = qdev_get_fw_dev_path_helper(dev, path, 128);
>> +    l = qdev_get_fw_dev_path_helper(dev, path, sizeof(path));
>>  
>> +    assert(l > 0);
>> +    if (l >= sizeof(path)) {
>> +        return NULL;
>> +    }
> 
> Failure mode could be avoided with the common technique: make
> qdev_get_fw_dev_path_helper() return the true length.  If it fit into
> path[], return strdup(path).  Else, allocate a suitable buffer and try
> again.
> 
> What do you think?

You're right of course, but I didn't have (or wanted to spend) the time
to change the code that much (and to test it...), especially because it
would have been fixed already if it had caused actual problems. I think
the patch could work as a "last resort", small improvement, but I don't
mind if someone posts a better fix :)

Laszlo

  reply	other threads:[~2012-07-23 13:07 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 [this message]
2012-07-23 15:01     ` Markus Armbruster
2012-07-23 15:29       ` Laszlo Ersek
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=500D4CD4.5030706@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).