qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <anthony@codemonkey.ws>
To: Stefan Hajnoczi <stefanha@gmail.com>
Cc: qemu-devel@nongnu.org, Adam Litke <aglitke@linux.vnet.ibm.com>,
	Markus Armbruster <armbru@redhat.com>,
	Luiz Capitulino <lcapitulino@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 03/22] qapi: add Error object
Date: Mon, 07 Mar 2011 07:36:00 -0600	[thread overview]
Message-ID: <4D74DF40.1070100@codemonkey.ws> (raw)
In-Reply-To: <AANLkTi=sLKk3dS8KfodZcqjw5yzeL99h6n79CwGPokdb@mail.gmail.com>

On 03/07/2011 05:38 AM, Stefan Hajnoczi wrote:
> On Mon, Mar 7, 2011 at 1:22 AM, Anthony Liguori<aliguori@us.ibm.com>  wrote:
>    
>> +struct Error
>> +{
>> +    QDict *obj;
>> +    const char *fmt;
>> +    char *msg;
>> +};
>>      
> I wonder why fmt is const char * but msg is char *.  Users should use
> error_get_pretty() instead of accessing msg directly and that function
> returns const char * so it seems that msg should be const char * to
> start with.
>    

fmt doesn't need to be free'd whereas msg does.  If you make msg const 
char *, the compiler will complain when you pass that to qemu_free().  I 
tend to think of the difference between 'const char *' and 'char *' as a 
string that I don't own vs. a string that I do own the reference to.

It's not universally true but it tends to work nicely most of the time.

>> +
>> +void error_set(Error **errp, const char *fmt, ...)
>> +{
>> +    Error *err;
>> +    va_list ap;
>> +
>> +    if (errp == NULL) {
>> +        return;
>> +    }
>> +
>> +    err = qemu_mallocz(sizeof(*err));
>> +
>> +    va_start(ap, fmt);
>> +    err->obj = qobject_to_qdict(qobject_from_jsonv(fmt,&ap));
>>      
> vsprintf() and friends pass va_list by value, they don't use a
> pointer.  Perhaps you want to follow that idiom?
>    

This va_list is passed to a recursive decent parser.  The nature of 
va_list is such that if you pass by value, you cannot access it within a 
function after you've passed it to another function.  Passing by 
reference seems to fix this (at least with GCC).  I'm not 100% confident 
this is a strictly standards compliant solution but it's been working so 
far.  Note that this isn't introduecd by this series.

>> +bool error_is_type(Error *err, const char *fmt)
>> +{
>> +    char *ptr;
>> +    char *end;
>> +    char classname[1024];
>> +
>> +    ptr = strstr(fmt, "'class': '");
>> +    assert(ptr != NULL);
>> +    ptr += strlen("'class': '");
>> +
>> +    end = strchr(ptr, '\'');
>> +    assert(end != NULL);
>> +
>> +    memcpy(classname, ptr, (end - ptr));
>> +    classname[(end - ptr)] = 0;
>> +
>> +    return strcmp(classname, error_get_field(err, "class")) == 0;
>>      
> I'd get rid of the buffer/memcpy and use strncmp in-place instead:
>
> const char *error_class = error_get_field(err, "class");
> if (strlen(error_class) != end - ptr) {
>      return false;
> }
> return strncmp(ptr, error_class, end - ptr) == 0;
>    

Yeah, that's definitely better.

Regards,

Anthony Liguori

> Stefan
>
>    

  reply	other threads:[~2011-03-07 13:36 UTC|newest]

Thread overview: 106+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-07  1:22 [Qemu-devel] [PATCH 00/22] QAPI Round 1 Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 01/22] Add hard build dependency on glib Anthony Liguori
2011-03-07 10:59   ` Daniel P. Berrange
2011-03-07 13:55     ` Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 02/22] qerror: expose a function to format an error Anthony Liguori
2011-03-07 11:14   ` Stefan Hajnoczi
2011-03-07 13:38     ` Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 03/22] qapi: add Error object Anthony Liguori
2011-03-07 11:06   ` Daniel P. Berrange
2011-03-07 13:59     ` Anthony Liguori
2011-03-07 14:24     ` Anthony Liguori
2011-03-07 11:38   ` Stefan Hajnoczi
2011-03-07 13:36     ` Anthony Liguori [this message]
2011-03-07 13:55       ` Stefan Hajnoczi
2011-03-07  1:22 ` [Qemu-devel] [PATCH 04/22] qerror: split out the reporting bits of QError Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 05/22] qerror: add new error message for invalid enum values Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 06/22] qapi: add JSON parsing error message Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 07/22] json: propagate error from parser Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 08/22] qapi: add code generator for qmp-types Anthony Liguori
2011-03-07  1:57   ` Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 09/22] qapi: add code generator for type marshallers Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 10/22] qapi: add core QMP server support Anthony Liguori
2011-03-07 13:09   ` Stefan Hajnoczi
2011-03-07 13:39     ` Anthony Liguori
2011-03-07 13:46       ` Daniel P. Berrange
2011-03-07 13:54         ` Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 11/22] qapi: add signal support to core QMP server Anthony Liguori
2011-03-07 13:21   ` Stefan Hajnoczi
2011-03-07 13:53     ` Anthony Liguori
2011-03-07 14:36       ` Stefan Hajnoczi
2011-03-07 14:41         ` Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 12/22] qapi: add QAPI module type Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 13/22] qapi: add code generators for QMP command marshaling Anthony Liguori
2011-03-07 13:27   ` Stefan Hajnoczi
2011-03-07 13:44     ` Anthony Liguori
2011-03-07 14:38       ` Stefan Hajnoczi
2011-03-07  1:22 ` [Qemu-devel] [PATCH 14/22] qapi: add query-version QMP command Anthony Liguori
2011-03-07 13:35   ` Stefan Hajnoczi
2011-03-07 13:41     ` Anthony Liguori
2011-03-09 13:28       ` Avi Kivity
2011-03-09 13:44         ` Anthony Liguori
2011-03-09 13:51           ` Avi Kivity
2011-03-09 14:13             ` Anthony Liguori
2011-03-09 13:36   ` Avi Kivity
2011-03-09 14:11     ` Anthony Liguori
2011-03-09 14:37       ` Avi Kivity
2011-03-09 14:47         ` Anthony Liguori
2011-03-10 12:41           ` Avi Kivity
2011-03-10 12:46             ` Avi Kivity
2011-03-10 13:52             ` Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 15/22] qapi: add new QMP server that uses CharDriverState Anthony Liguori
2011-03-07 13:52   ` Stefan Hajnoczi
2011-03-07 14:02     ` Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 16/22] vl: add a new -qmp2 option to expose experimental QMP server Anthony Liguori
2011-03-07  1:22 ` [Qemu-devel] [PATCH 17/22] qapi: add QMP quit command Anthony Liguori
2011-03-07  1:23 ` [Qemu-devel] [PATCH 18/22] qapi: add QMP qmp_capabilities command Anthony Liguori
2011-03-07  1:23 ` [Qemu-devel] [PATCH 19/22] qapi: add QMP put-event command Anthony Liguori
2011-03-09 13:31   ` Avi Kivity
2011-03-09 13:48     ` Anthony Liguori
2011-03-09 13:58       ` Avi Kivity
2011-03-09 14:26         ` Anthony Liguori
2011-03-10 12:39           ` Avi Kivity
2011-03-10 14:12             ` Anthony Liguori
2011-03-10 14:24               ` Avi Kivity
2011-03-10 15:30                 ` Avi Kivity
2011-03-10 15:41                   ` Anthony Liguori
2011-03-10 15:49                     ` Avi Kivity
2011-03-10 16:42                       ` Anthony Liguori
2011-03-12 20:37                         ` Avi Kivity
2011-03-10 15:33                 ` Anthony Liguori
2011-03-10 15:45                   ` Avi Kivity
2011-03-10 16:04                     ` Anthony Liguori
2011-03-12 20:42                       ` Avi Kivity
2011-03-12 23:30                         ` Anthony Liguori
2011-03-07  1:23 ` [Qemu-devel] [PATCH 20/22] qapi: add code generator for libqmp Anthony Liguori
2011-03-07  1:23 ` [Qemu-devel] [PATCH 21/22] qapi: add test-libqmp Anthony Liguori
2011-03-07  1:23 ` [Qemu-devel] [PATCH 22/22] qapi: generate HTML report for test-libqmp Anthony Liguori
2011-03-07  2:11   ` Anthony Liguori
2011-03-07  1:26 ` [Qemu-devel] [PATCH] qapi: qmp-types.c and qmp-types.h Anthony Liguori
2011-03-07  1:27 ` [Qemu-devel] [PATCH] qapi: qmp-marshal-types.c and qmp-marshal-types.h Anthony Liguori
2011-03-07  1:28 ` [Qemu-devel] [PATCH] qapi: add qmp-marshal.c and qmp.h Anthony Liguori
2011-03-07  1:29 ` [Qemu-devel] [PATCH] qapi: add libqmp.c and libqmp.h Anthony Liguori
2011-03-07 15:08 ` [Qemu-devel] [PATCH 00/22] QAPI Round 1 Stefan Hajnoczi
2011-03-07 15:59   ` Anthony Liguori
2011-03-08 11:12     ` Avi Kivity
2011-03-08 13:35       ` Anthony Liguori
2011-03-08 13:45         ` Avi Kivity
2011-03-08 13:54           ` Anthony Liguori
2011-03-08 14:00             ` Avi Kivity
2011-03-08 14:10               ` Anthony Liguori
2011-03-08 14:17                 ` Avi Kivity
2011-03-08 14:20                   ` Anthony Liguori
2011-03-08 14:38                   ` Anthony Liguori
2011-03-08 14:52                     ` Avi Kivity
2011-03-08 15:03                       ` Anthony Liguori
2011-03-08 17:44                         ` Avi Kivity
2011-03-08 19:19                           ` Anthony Liguori
2011-03-09  8:51                             ` Avi Kivity
2011-03-09 13:12                               ` Anthony Liguori
2011-03-09 13:14                                 ` Avi Kivity
2011-03-09 13:51                                   ` Anthony Liguori
2011-03-09 13:55                                     ` Avi Kivity
2011-03-09 14:15                                       ` Anthony Liguori
2011-03-09 13:51                                   ` Michael Roth
2011-03-07 20:59   ` Anthony Liguori
2011-03-07 22:00     ` Stefan Hajnoczi

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=4D74DF40.1070100@codemonkey.ws \
    --to=anthony@codemonkey.ws \
    --cc=aglitke@linux.vnet.ibm.com \
    --cc=armbru@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    /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).