Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MXv9h-0006Fa-OP for qemu-devel@nongnu.org; Mon, 03 Aug 2009 06:52:37 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MXv9c-0006B2-MJ for qemu-devel@nongnu.org; Mon, 03 Aug 2009 06:52:37 -0400 Received: from [199.232.76.173] (port=44776 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MXv9c-0006Ap-FI for qemu-devel@nongnu.org; Mon, 03 Aug 2009 06:52:32 -0400 Received: from mx2.redhat.com ([66.187.237.31]:45365) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MXv9b-0000Np-S9 for qemu-devel@nongnu.org; Mon, 03 Aug 2009 06:52:32 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n73AqUKd029475 for ; Mon, 3 Aug 2009 06:52:30 -0400 Subject: Re: [Qemu-devel] [PATCH] fix qdev_print_devinfo() References: <1249291608-14766-1-git-send-email-kraxel@redhat.com> From: Markus Armbruster Date: Mon, 03 Aug 2009 12:52:28 +0200 In-Reply-To: <1249291608-14766-1-git-send-email-kraxel@redhat.com> (Gerd Hoffmann's message of "Mon\, 3 Aug 2009 11\:26\:48 +0200") Message-ID: <877hxlf9ub.fsf@pike.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Gerd Hoffmann Cc: qemu-devel@nongnu.org Gerd Hoffmann writes: > snprintf returns number of bytes needed for the output, not the number > of bytes actually written. Thus the math is wrong ... > > Spotted by Markus Armbruster. > > Signed-off-by: Gerd Hoffmann > --- > hw/qdev.c | 26 +++++++++++++++++--------- > 1 files changed, 17 insertions(+), 9 deletions(-) > > diff --git a/hw/qdev.c b/hw/qdev.c > index 9488dba..fc95115 100644 > --- a/hw/qdev.c > +++ b/hw/qdev.c > @@ -108,15 +108,23 @@ DeviceState *qdev_create(BusState *bus, const char *name) > static int qdev_print_devinfo(DeviceInfo *info, char *dest, int len) > { > int pos = 0; > + int ret; > > - pos += snprintf(dest+pos, len-pos, "name \"%s\", bus %s", > - info->name, info->bus_info->name); > - if (info->alias) > - pos += snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias); > - if (info->desc) > - pos += snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc); > - if (info->no_user) > - pos += snprintf(dest+pos, len-pos, ", no-user"); > + ret = snprintf(dest+pos, len-pos, "name \"%s\", bus %s", > + info->name, info->bus_info->name); > + pos += MIN(len-pos,ret); > + if (info->alias) { > + ret = snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias); > + pos += MIN(len-pos,ret); > + } > + if (info->desc) { > + ret = snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc); > + pos += MIN(len-pos,ret); > + } > + if (info->no_user) { > + ret = snprintf(dest+pos, len-pos, ", no-user"); > + pos += MIN(len-pos,ret); > + } > return pos; > } Looks good. For a general purpose formatting function, I'd return how much buffer is needed to avoid truncation, just like snprintf(), to let the caller resize its buffer. But qdev_print_devinfo()'s callers don't care.