WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

Re: [Xen-devel] [PATCH V2 09/11] libxl_json, Handle number abrove LONG_M

To: Ian Campbell <Ian.Campbell@xxxxxxxxxx>
Subject: Re: [Xen-devel] [PATCH V2 09/11] libxl_json, Handle number abrove LONG_MAX.
From: Anthony PERARD <anthony.perard@xxxxxxxxxx>
Date: Mon, 24 Oct 2011 16:12:53 +0100
Cc: Xen Devel <xen-devel@xxxxxxxxxxxxxxxxxxx>, Stefano Stabellini <Stefano.Stabellini@xxxxxxxxxxxxx>
Delivery-date: Mon, 24 Oct 2011 08:18:13 -0700
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=qmWydgHuHwe5ab3ilnZ8MakuQLVcvs82AGMbGMQKT+Q=; b=afpGSNyr5phr3V1P1LF5tiu00bcnAbbQBzwLVo9o6vPgzoa6Zi1uagInN7eQkcMyY4 ZHT/XcvT81jCo5crOnCOjQvhvCbFKs0VG9xP4aKJIwBiFwTJgI+pW4rpoCGhvtcE6YDG SBoz6YA1fpa1OZQSDhX5m/pRnNfa7taQ78g5Q=
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
In-reply-to: <1319450257.3385.175.camel@xxxxxxxxxxxxxxxxxxxxxx>
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
References: <1319133573-25891-1-git-send-email-anthony.perard@xxxxxxxxxx> <1319133573-25891-10-git-send-email-anthony.perard@xxxxxxxxxx> <1319450257.3385.175.camel@xxxxxxxxxxxxxxxxxxxxxx>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
On Mon, Oct 24, 2011 at 10:57, Ian Campbell <Ian.Campbell@xxxxxxxxxx> wrote:
> On Thu, 2011-10-20 at 18:59 +0100, Anthony PERARD wrote:
>> The integers are now "long long" in the json_object. If strtoll failed to
>> convert a string into a number, the number is stored as it (a char*).
>>
>> Signed-off-by: Anthony PERARD <anthony.perard@xxxxxxxxxx>
>> ---
>>  tools/libxl/libxl_internal.h |    7 +++--
>>  tools/libxl/libxl_json.c     |   52 
>> +++++++++++++++++++++++------------------
>>  2 files changed, 33 insertions(+), 26 deletions(-)
>>
>> diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
>> index 5720b31..849b251 100644
>> --- a/tools/libxl/libxl_internal.h
>> +++ b/tools/libxl/libxl_internal.h
>> @@ -465,7 +465,8 @@ typedef enum {
>>      JSON_TRUE,
>>      JSON_FALSE,
>>      JSON_INTEGER,
>> -    JSON_DOUBLE,
>
> Did you accidentally remove this ...
>
>> +    /* number is store in string, it's too big to be a long long */
>> +    JSON_NUMBER,
>>      JSON_STRING,
>>      JSON_MAP,
>>      JSON_ARRAY,
>> @@ -475,7 +476,7 @@ typedef enum {
>>  typedef struct libxl__json_object {
>>      libxl__json_node_type type;
>>      union {
>> -        long i;
>> +        long long i;
>>          double d;
>
> ... or accidentally leave this?

I've accidentally leave this double, because I do not handle float
number as I do'nt need them yet. But I probably should parse them as
well, and keep double in the structure.

>>          char *string;
>>          /* List of libxl__json_object */
>> @@ -534,7 +535,7 @@ flexarray_t *libxl__json_object_get_array(const 
>> libxl__json_object *o)
>>      else
>>          return NULL;
>>  }
>> -static inline long libxl__json_object_get_integer(const libxl__json_object 
>> *o)
>> +static inline long long libxl__json_object_get_integer(const 
>> libxl__json_object *o)
>>  {
>>      if (libxl__json_object_is_integer(o))
>>          return o->u.i;
>> diff --git a/tools/libxl/libxl_json.c b/tools/libxl/libxl_json.c
>> index c743114..2d8f61e 100644
>> --- a/tools/libxl/libxl_json.c
>> +++ b/tools/libxl/libxl_json.c
>> @@ -44,6 +44,7 @@ struct libxl__yajl_ctx {
>>  #  define DEBUG_GEN(ctx, type)              yajl_gen_##type(ctx->g)
>>  #  define DEBUG_GEN_VALUE(ctx, type, value) yajl_gen_##type(ctx->g, value)
>>  #  define DEBUG_GEN_STRING(ctx, str, n)     yajl_gen_string(ctx->g, str, n)
>> +#  define DEBUG_GEN_NUMBER(ctx, str, n)     yajl_gen_number(ctx->g, str, n)
>>  #  define DEBUG_GEN_REPORT(yajl_ctx) \
>>      do { \
>>          const unsigned char *buf = NULL; \
>> @@ -60,6 +61,7 @@ struct libxl__yajl_ctx {
>>  #  define DEBUG_GEN(ctx, type)                  ((void)0)
>>  #  define DEBUG_GEN_VALUE(ctx, type, value)     ((void)0)
>>  #  define DEBUG_GEN_STRING(ctx, value, lenght)  ((void)0)
>> +#  define DEBUG_GEN_NUMBER(ctx, value, lenght)  ((void)0)
>
> that typo got propagated...
>
>>  #  define DEBUG_GEN_REPORT(ctx)                 ((void)0)
>>  #endif
>>
>> @@ -363,6 +365,7 @@ void libxl__json_object_free(libxl__gc *gc, 
>> libxl__json_object *obj)
>>          return;
>>      switch (obj->type) {
>>      case JSON_STRING:
>> +    case JSON_NUMBER:
>>          free(obj->u.string);
>>          break;
>>      case JSON_MAP: {
>> @@ -504,35 +507,38 @@ static int json_callback_boolean(void *opaque, int 
>> boolean)
>>      return 1;
>>  }
>>
>> -static int json_callback_integer(void *opaque, long value)
>> +static int json_callback_number(void *opaque, const char *s, unsigned int 
>> len)
>>  {
>>      libxl__yajl_ctx *ctx = opaque;
>> -    libxl__json_object *obj;
>> -
>> -    DEBUG_GEN_VALUE(ctx, integer, value);
>> +    libxl__json_object *obj = NULL;
>> +    long long i;
>>
>> -    if ((obj = json_object_alloc(ctx->gc, JSON_INTEGER)) == NULL)
>> -        return 0;
>> -    obj->u.i = value;
>> +    /* should be replace by number */
>> +    DEBUG_GEN_NUMBER(ctx, s, len);
>>
>> -    if (json_object_append_to(ctx->gc, obj, ctx->current) == -1) {
>> -        libxl__json_object_free(ctx->gc, obj);
>> -        return 0;
>> -    }
>> +    i = strtoll(s, NULL, 10);
>>
>> -    return 1;
>> -}
>> +    if ((i == LLONG_MIN || i == LLONG_MAX) && errno == ERANGE) {
>> +        char *t = NULL;
>>
>> -static int json_callback_double(void *opaque, double value)
>> -{
>> -    libxl__yajl_ctx *ctx = opaque;
>> -    libxl__json_object *obj;
>> +        if ((obj = json_object_alloc(ctx->gc, JSON_NUMBER)) == NULL)
>> +            return 0;
>>
>> -    DEBUG_GEN_VALUE(ctx, double, value);
>> +        t = malloc(len + 1);
>> +        if (t == NULL) {
>> +            LIBXL__LOG_ERRNO(libxl__gc_owner(ctx->gc), LIBXL__LOG_ERROR,
>> +                             "Failed to allocate");
>> +            return 0;
>> +        }
>> +        strncpy(t, s, len);
>> +        t[len] = 0;
>>
>> -    if ((obj = json_object_alloc(ctx->gc, JSON_DOUBLE)) == NULL)
>> -        return 0;
>> -    obj->u.d = value;
>> +        obj->u.string = t;
>> +    } else {
>> +        if ((obj = json_object_alloc(ctx->gc, JSON_INTEGER)) == NULL)
>> +            return 0;
>> +        obj->u.i = i;
>> +    }
>>
>>      if (json_object_append_to(ctx->gc, obj, ctx->current) == -1) {
>>          libxl__json_object_free(ctx->gc, obj);
>> @@ -706,9 +712,9 @@ static int json_callback_end_array(void *opaque)
>>  static yajl_callbacks callbacks = {
>>      json_callback_null,
>>      json_callback_boolean,
>> -    json_callback_integer,
>> -    json_callback_double,
>>      NULL,
>> +    NULL,
>> +    json_callback_number,
>>      json_callback_string,
>>      json_callback_start_map,
>>      json_callback_map_key,
>
>
>
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@xxxxxxxxxxxxxxxxxxx
> http://lists.xensource.com/xen-devel
>



-- 
Anthony PERARD

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel