From: Wenchao Xia <wenchaoqemu@gmail.com>
To: Markus Armbruster <armbru@redhat.com>, Eric Blake <eblake@redhat.com>
Cc: lcapitulino@redhat.com, qemu-devel@nongnu.org, mdroth@linux.vnet.ibm.com
Subject: Re: [Qemu-devel] [RFC PATCH V4 2/5] qapi: add event helper functions
Date: Wed, 02 Apr 2014 17:44:51 +0800 [thread overview]
Message-ID: <533BDC13.1060209@gmail.com> (raw)
In-Reply-To: <87ppl691ij.fsf@blackfin.pond.sub.org>
于 2014/3/28 16:21, Markus Armbruster 写道:
> Eric Blake <eblake@redhat.com> 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 <wenchaoqemu@gmail.com>
>>> ---
>>> 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.
next prev parent reply other threads:[~2014-04-02 9:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-27 8:03 [Qemu-devel] [RFC PATCH V4 0/5] add direct support of event in qapi schema Wenchao Xia
2014-03-27 8:03 ` [Qemu-devel] [RFC PATCH V4 1/5] os-posix: include sys/time.h Wenchao Xia
2014-03-27 8:03 ` [Qemu-devel] [RFC PATCH V4 2/5] qapi: add event helper functions Wenchao Xia
2014-03-27 23:11 ` Eric Blake
2014-03-28 8:21 ` Markus Armbruster
2014-04-02 9:44 ` Wenchao Xia [this message]
2014-03-27 8:03 ` [Qemu-devel] [RFC PATCH V4 3/5] qapi script: add event support Wenchao Xia
2014-03-27 8:03 ` [Qemu-devel] [RFC PATCH V4 4/5] test: add test cases for qapi event Wenchao Xia
2014-03-27 8:03 ` [Qemu-devel] [RFC PATCH V4 5/5] qapi event: convert RTC_CHANGE Wenchao Xia
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=533BDC13.1060209@gmail.com \
--to=wenchaoqemu@gmail.com \
--cc=armbru@redhat.com \
--cc=eblake@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=mdroth@linux.vnet.ibm.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.