All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: Programmingkid <programmingkidx@gmail.com>,
	qemu-devel qemu-devel <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [PATCH] qdev-monitor.c: Add device id generation
Date: Tue, 25 Aug 2015 14:42:02 +0200	[thread overview]
Message-ID: <87twrnmtet.fsf@blackfin.pond.sub.org> (raw)
In-Reply-To: <55DB98E1.600@redhat.com> (Eric Blake's message of "Mon, 24 Aug 2015 16:21:21 -0600")

My other reply is about design issues.  This one is about the actual
code.  Until we get rough consensus on the former, the latter doesn't
really matter, but here goes anyway.

Eric Blake <eblake@redhat.com> writes:

> On 08/24/2015 12:53 PM, Programmingkid wrote:
>> Add device ID generation to each device if an ID isn't given.
>> 
>> Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
>> 
>> ---
>> This patch can be tested by adding adding usb devices using the monitor.
>> Start QEMU with the -usb option. Then go to the monitor and type
>> "device_add usb-mouse". The ID of the device will be set to a number.
>> Since QEMU will not allow an user to add a device with an ID set to a
>> number, there is no chance for ID collisions. 
>>
>>  qdev-monitor.c |   24 ++++++++++++++++++------
>>  1 files changed, 18 insertions(+), 6 deletions(-)
>>
>> diff --git a/qdev-monitor.c b/qdev-monitor.c
>> index f9e2d62..98267c4 100644
>> --- a/qdev-monitor.c
>> +++ b/qdev-monitor.c
>> @@ -26,6 +26,10 @@
>>  #include "qapi/qmp/qerror.h"
>>  #include "qemu/config-file.h"
>>  #include "qemu/error-report.h"
>> +#include <math.h>
>> +
>> +/* USB's max number of devices is 127. This number is 3 digits long. */
>> +#define MAX_NUM_DIGITS_FOR_USB_ID 3

This limit makes no sense to me.

>>  
>>  /*
>>   * Aliases were a bad idea from the start.  Let's keep them
>> @@ -574,17 +578,25 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
>>      id = qemu_opts_id(opts);
>>      if (id) {
>>          dev->id = id;
>> +    } else { /* create an id for a device if none is provided */
>> +        static int device_id_count;
>> +
>> +        /* Add one for '\0' character */
>> +        char *device_id = (char *) malloc(sizeof(char) *
>> +                                            MAX_NUM_DIGITS_FOR_USB_ID + 1);
>> +        sprintf(device_id, "%d", device_id_count++);
>
> g_strdup_printf() is a lot nicer about avoiding the risk of arbitrary
> overflow...
>
>> +        dev->id = (const char *) device_id;
>> +
>> +        /* if device_id_count >= 10^MAX_NUM_DIGITS_FOR_USB_ID */
>> +        if (device_id_count >= pow(10, MAX_NUM_DIGITS_FOR_USB_ID)) {
>> +            printf("Warning: Maximum number of device ID's generated!\n\a");
>> +            printf("Time for you to make your own device ID's.\n");
>
> besides, printf() is probably the wrong way to do error reporting, and
> we don't use \a BEL sequences anywhere else in qemu code.
>
>> +        }
>>      }

When device_id_count reaches the limit, you warn.  Next time around, you
overrun the buffer.  Not good.

Eric is right, g_strdup_printf() is easier and safer.

I'd make the count 64 bits, so wrapping becomes vanishingly unlikely.

>>  
>>      if (dev->id) {
>
> This if would now be a dead check if your patch is applied.
>
>>          object_property_add_child(qdev_get_peripheral(), dev->id,
>>                                    OBJECT(dev), NULL);
>> -    } else {
>> -        static int anon_count;
>> -        gchar *name = g_strdup_printf("device[%d]", anon_count++);
>> -        object_property_add_child(qdev_get_peripheral_anon(), name,
>> -                                  OBJECT(dev), NULL);
>> -        g_free(name);
>>      }
>
> It looks like your goal was to move this code earlier, but you changed
> enough aspects of it that I'm not sure what the right fix should be.

Drop the conditional, it's both useless and confusing after your patch.

  reply	other threads:[~2015-08-25 12:42 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-24 18:53 [Qemu-devel] [PATCH] qdev-monitor.c: Add device id generation Programmingkid
2015-08-24 22:21 ` Eric Blake
2015-08-25 12:42   ` Markus Armbruster [this message]
2015-08-25 15:25     ` Programmingkid
2015-08-25 15:33       ` Peter Maydell
2015-08-25 15:50         ` Programmingkid
2015-08-25 18:30           ` Markus Armbruster
2015-08-25 19:05             ` Programmingkid
2015-08-25 14:33   ` Programmingkid
2015-08-25 12:38 ` [Qemu-devel] Should we auto-generate IDs? (was: [PATCH] qdev-monitor.c: Add device id generation) Markus Armbruster
2015-08-25 15:15   ` Programmingkid
2015-08-26 14:52   ` Programmingkid
2015-08-26 16:31     ` [Qemu-devel] Should we auto-generate IDs? Markus Armbruster
2015-08-26 17:16       ` Programmingkid
2015-08-26 18:45         ` Peter Maydell
2015-08-26 21:48           ` Programmingkid
2015-08-26 22:08             ` John Snow
2015-08-27  3:40               ` Programmingkid
2015-08-27  5:39                 ` Markus Armbruster
2015-08-27 15:39                   ` Programmingkid
2015-08-26 17:25       ` Jeff Cody
2015-08-26 17:29         ` Programmingkid
2015-08-26 18:08           ` Jeff Cody
2015-08-26 18:17             ` Programmingkid
2015-08-26 22:01               ` Jeff Cody
2015-08-26 22:04                 ` John Snow
2015-08-27  3:26                   ` Programmingkid
2015-08-27  3:22                 ` Programmingkid
2015-08-27 12:32                   ` Jeff Cody
2015-08-27 13:00                     ` Eric Blake
2015-08-27 13:39                       ` Programmingkid
2015-08-27 13:51                         ` Daniel P. Berrange
2015-08-27 14:01                           ` Eric Blake
2015-08-27 14:18                             ` Jeff Cody
2015-08-27 14:19                             ` Programmingkid
2015-08-27 14:01                           ` Programmingkid
2015-08-27 18:59                       ` John Snow
2015-08-27 19:20                         ` Eric Blake
2015-08-27 13:33                     ` Programmingkid
2015-08-27 13:49                       ` Daniel P. Berrange
2015-08-27 13:56                         ` Programmingkid
2015-08-27 14:02                           ` Eric Blake
2015-08-27 14:34                             ` Programmingkid
2015-08-27 14:42                               ` Daniel P. Berrange
2015-08-27 15:20                                 ` Programmingkid
2015-08-27 15:40                                   ` Jeff Cody
2015-08-27 15:58                                     ` Programmingkid
2015-08-27 16:02                                       ` Daniel P. Berrange
2015-08-27 16:08                                         ` Programmingkid
2015-08-27 16:22                                           ` Daniel P. Berrange
2015-08-27 16:49                                             ` Programmingkid
2015-08-27 20:15                                             ` Programmingkid
2015-08-27 19:08                                         ` Jeff Cody
2015-08-27 19:27                                           ` Eric Blake
2015-08-27 20:37                                             ` Jeff Cody
2015-08-27 14:06                           ` Daniel P. Berrange
2015-08-27 14:54                             ` Programmingkid
2015-08-27 14:07                       ` Jeff Cody
2015-08-27 15:13                         ` Programmingkid
2015-08-27 15:19                           ` Daniel P. Berrange
2015-08-27 15:22                             ` Programmingkid
2015-08-27 15:55                               ` Daniel P. Berrange
2015-08-27 16:03                                 ` Programmingkid
2015-08-27 16:06                                   ` Daniel P. Berrange
2015-08-27 16:08                                     ` Eric Blake
2015-09-01 12:34                     ` Kevin Wolf
2015-09-01 14:18                       ` Programmingkid
2015-09-01 14:43                         ` Kevin Wolf
2015-09-01 15:55                           ` Programmingkid
2015-09-03 14:34       ` Programmingkid
2015-09-03 14:43         ` Jeff Cody
2015-09-03 15:55           ` Programmingkid
2015-09-03 16:12           ` [Qemu-devel] [PATCH v3] qdev-monitor.c: Add device id generation Programmingkid
2015-08-26 17:28   ` [Qemu-devel] Should we auto-generate IDs? (was: [PATCH] qdev-monitor.c: Add device id generation) Daniel P. Berrange
2015-08-26 17:46     ` Programmingkid
2015-08-26 17:53       ` Daniel P. Berrange
2015-08-26 18:01         ` Programmingkid
2015-08-27 13:54           ` Daniel P. Berrange
2015-08-27 14:03             ` Programmingkid

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=87twrnmtet.fsf@blackfin.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=programmingkidx@gmail.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 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.