From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51567) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WVHjb-0004Cs-9X for qemu-devel@nongnu.org; Wed, 02 Apr 2014 05:45:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WVHjR-0001LU-SS for qemu-devel@nongnu.org; Wed, 02 Apr 2014 05:45:27 -0400 Received: from mail-pd0-x235.google.com ([2607:f8b0:400e:c02::235]:42278) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WVHjR-0001LK-Lq for qemu-devel@nongnu.org; Wed, 02 Apr 2014 05:45:17 -0400 Received: by mail-pd0-f181.google.com with SMTP id p10so10853486pdj.40 for ; Wed, 02 Apr 2014 02:45:16 -0700 (PDT) Message-ID: <533BDC13.1060209@gmail.com> Date: Wed, 02 Apr 2014 17:44:51 +0800 From: Wenchao Xia MIME-Version: 1.0 References: <1395907390-18812-1-git-send-email-wenchaoqemu@gmail.com> <1395907390-18812-3-git-send-email-wenchaoqemu@gmail.com> <5334B023.5060602@redhat.com> <87ppl691ij.fsf@blackfin.pond.sub.org> In-Reply-To: <87ppl691ij.fsf@blackfin.pond.sub.org> Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [RFC PATCH V4 2/5] qapi: add event helper functions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster , Eric Blake Cc: lcapitulino@redhat.com, qemu-devel@nongnu.org, mdroth@linux.vnet.ibm.com ÓÚ 2014/3/28 16:21, Markus Armbruster дµÀ: > Eric Blake writes: > >> On 03/27/2014 02:03 AM, Wenchao Xia wrote: >>> This file holds some functions that do not need to be generated. >>> >>> Signed-off-by: Wenchao Xia >>> --- >>> include/qapi/qmp-event.h | 27 +++++++++++++++++ >>> qapi/Makefile.objs | 1 + >>> qapi/qmp-event.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ >>> 3 files changed, 98 insertions(+), 0 deletions(-) >>> create mode 100644 include/qapi/qmp-event.h >>> create mode 100644 qapi/qmp-event.c >>> >> >>> + err = qemu_gettimeofday(&tv); >>> + if (err < 0) { >>> + /* Put -1 to indicate failure of getting host time */ >>> + tv.tv_sec = -1; >>> + tv.tv_usec = -1; >> >> You fixed the problem with C promotion here, but... >> >>> + } >>> + >>> + obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", " >>> + "'microseconds': %" PRId64 " }", >>> + (int64_t) tv.tv_sec, (int64_t) tv.tv_usec); >> >> ...here, C promotion rules bite once again :( If tv_usec is uint32_t, >> then it zero-extends rather than sign-extends into int64_t, and you may >> end up with 0xffffffff instead of the intended -1. When doing >> potentially widening casts, it is only safe if you know the signedness >> of the pre-cast value; but with struct timeval, POSIX doesn't make that >> easy. >> >> Maybe it's easier to just rewrite things with known types: >> >> int64_t sec; >> int usec; >> qemu_timval tv; >> err = qemu_gettimeofday(&tv); >> if (err < 0) { >> sec = -1; >> usec = -1; >> } else { >> sec = tv.tv_sec; >> usec = tv.tv_usec; >> } >> qobject_from_jsonf("... %"PRId64 ", ...%d", sec, usec) >> >> since 'int' is guaranteed to be large enough for all the usec values we >> care about on all platforms we compile on (that is, we require 32-bit >> int, even if C allows for a 16-bit int implementation). > > If you go that route, then why not go all the way nad make both sec and > usec int64_t? Look ma, no type casts! > Yep, in this way code looks better... I will respin it later with a full conversion.