[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



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


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.