* Don't use this as a variable, so we can compile with a C++ compiler

Michael Clark, <michael@metaparadigm.com>


git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@32 327403b1-1117-474d-bef2-5cb71233fd97
This commit is contained in:
Michael Clark
2009-02-25 01:55:31 +00:00
parent 22dee7cb59
commit 266a3fd301
3 changed files with 177 additions and 175 deletions

View File

@@ -1,4 +1,6 @@
0.9 0.9
* Don't use this as a variable, so we can compile with a C++ compiler
Michael Clark, <michael@metaparadigm.com>
* Null pointer dereference fix. Fix json_object_get_boolean strlen test * Null pointer dereference fix. Fix json_object_get_boolean strlen test
to not return TRUE for zero length string. Remove redundant includes. to not return TRUE for zero length string. Remove redundant includes.
Erik Hovland, erik at hovland dot org Erik Hovland, erik at hovland dot org

View File

@@ -26,68 +26,68 @@
struct array_list* struct array_list*
array_list_new(array_list_free_fn *free_fn) array_list_new(array_list_free_fn *free_fn)
{ {
struct array_list *this; struct array_list *arr;
if(!(this = calloc(1, sizeof(struct array_list)))) return NULL; if(!(arr = calloc(1, sizeof(struct array_list)))) return NULL;
this->size = ARRAY_LIST_DEFAULT_SIZE; arr->size = ARRAY_LIST_DEFAULT_SIZE;
this->length = 0; arr->length = 0;
this->free_fn = free_fn; arr->free_fn = free_fn;
if(!(this->array = calloc(sizeof(void*), this->size))) { if(!(arr->array = calloc(sizeof(void*), arr->size))) {
free(this); free(arr);
return NULL; return NULL;
} }
return this; return arr;
} }
extern void extern void
array_list_free(struct array_list *this) array_list_free(struct array_list *arr)
{ {
int i; int i;
for(i = 0; i < this->length; i++) for(i = 0; i < arr->length; i++)
if(this->array[i]) this->free_fn(this->array[i]); if(arr->array[i]) arr->free_fn(arr->array[i]);
free(this->array); free(arr->array);
free(this); free(arr);
} }
void* void*
array_list_get_idx(struct array_list *this, int i) array_list_get_idx(struct array_list *arr, int i)
{ {
if(i >= this->length) return NULL; if(i >= arr->length) return NULL;
return this->array[i]; return arr->array[i];
} }
static int array_list_expand_internal(struct array_list *this, int max) static int array_list_expand_internal(struct array_list *arr, int max)
{ {
void *t; void *t;
int new_size; int new_size;
if(max < this->size) return 0; if(max < arr->size) return 0;
new_size = max(this->size << 1, max); new_size = max(arr->size << 1, max);
if(!(t = realloc(this->array, new_size*sizeof(void*)))) return -1; if(!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
this->array = t; arr->array = t;
(void)memset(this->array + this->size, 0, (new_size-this->size)*sizeof(void*)); (void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
this->size = new_size; arr->size = new_size;
return 0; return 0;
} }
int int
array_list_put_idx(struct array_list *this, int idx, void *data) array_list_put_idx(struct array_list *arr, int idx, void *data)
{ {
if(array_list_expand_internal(this, idx)) return -1; if(array_list_expand_internal(arr, idx)) return -1;
if(this->array[idx]) this->free_fn(this->array[idx]); if(arr->array[idx]) arr->free_fn(arr->array[idx]);
this->array[idx] = data; arr->array[idx] = data;
if(this->length <= idx) this->length = idx + 1; if(arr->length <= idx) arr->length = idx + 1;
return 0; return 0;
} }
int int
array_list_add(struct array_list *this, void *data) array_list_add(struct array_list *arr, void *data)
{ {
return array_list_put_idx(this, this->length, data); return array_list_put_idx(arr, arr->length, data);
} }
int int
array_list_length(struct array_list *this) array_list_length(struct array_list *arr)
{ {
return this->length; return arr->length;
} }

View File

@@ -44,7 +44,7 @@ static const char* json_type_name[] = {
}; };
#endif /* REFCOUNT_DEBUG */ #endif /* REFCOUNT_DEBUG */
static void json_object_generic_delete(struct json_object* this); static void json_object_generic_delete(struct json_object* jso);
static struct json_object* json_object_new(enum json_type o_type); static struct json_object* json_object_new(enum json_type o_type);
@@ -127,82 +127,82 @@ static int json_escape_str(struct printbuf *pb, char *str)
/* reference counting */ /* reference counting */
extern struct json_object* json_object_get(struct json_object *this) extern struct json_object* json_object_get(struct json_object *jso)
{ {
if(this) { if(jso) {
this->_ref_count++; jso->_ref_count++;
} }
return this; return jso;
} }
extern void json_object_put(struct json_object *this) extern void json_object_put(struct json_object *jso)
{ {
if(this) { if(jso) {
this->_ref_count--; jso->_ref_count--;
if(!this->_ref_count) this->_delete(this); if(!jso->_ref_count) jso->_delete(jso);
} }
} }
/* generic object construction and destruction parts */ /* generic object construction and destruction parts */
static void json_object_generic_delete(struct json_object* this) static void json_object_generic_delete(struct json_object* jso)
{ {
#ifdef REFCOUNT_DEBUG #ifdef REFCOUNT_DEBUG
MC_DEBUG("json_object_delete_%s: %p\n", MC_DEBUG("json_object_delete_%s: %p\n",
json_type_name[this->o_type], this); json_type_name[jso->o_type], jso);
lh_table_delete(json_object_table, this); lh_table_delete(json_object_table, jso);
#endif /* REFCOUNT_DEBUG */ #endif /* REFCOUNT_DEBUG */
printbuf_free(this->_pb); printbuf_free(jso->_pb);
free(this); free(jso);
} }
static struct json_object* json_object_new(enum json_type o_type) static struct json_object* json_object_new(enum json_type o_type)
{ {
struct json_object *this = calloc(sizeof(struct json_object), 1); struct json_object *jso = calloc(sizeof(struct json_object), 1);
if(!this) return NULL; if(!jso) return NULL;
this->o_type = o_type; jso->o_type = o_type;
this->_ref_count = 1; jso->_ref_count = 1;
this->_delete = &json_object_generic_delete; jso->_delete = &json_object_generic_delete;
#ifdef REFCOUNT_DEBUG #ifdef REFCOUNT_DEBUG
lh_table_insert(json_object_table, this, this); lh_table_insert(json_object_table, jso, jso);
MC_DEBUG("json_object_new_%s: %p\n", json_type_name[this->o_type], this); MC_DEBUG("json_object_new_%s: %p\n", json_type_name[jso->o_type], jso);
#endif /* REFCOUNT_DEBUG */ #endif /* REFCOUNT_DEBUG */
return this; return jso;
} }
/* type checking functions */ /* type checking functions */
int json_object_is_type(struct json_object *this, enum json_type type) int json_object_is_type(struct json_object *jso, enum json_type type)
{ {
return (this->o_type == type); return (jso->o_type == type);
} }
enum json_type json_object_get_type(struct json_object *this) enum json_type json_object_get_type(struct json_object *jso)
{ {
return this->o_type; return jso->o_type;
} }
/* json_object_to_json_string */ /* json_object_to_json_string */
const char* json_object_to_json_string(struct json_object *this) const char* json_object_to_json_string(struct json_object *jso)
{ {
if(!this) return "null"; if(!jso) return "null";
if(!this->_pb) { if(!jso->_pb) {
if(!(this->_pb = printbuf_new())) return NULL; if(!(jso->_pb = printbuf_new())) return NULL;
} else { } else {
printbuf_reset(this->_pb); printbuf_reset(jso->_pb);
} }
if(this->_to_json_string(this, this->_pb) < 0) return NULL; if(jso->_to_json_string(jso, jso->_pb) < 0) return NULL;
return this->_pb->buf; return jso->_pb->buf;
} }
/* json_object_object */ /* json_object_object */
static int json_object_object_to_json_string(struct json_object* this, static int json_object_object_to_json_string(struct json_object* jso,
struct printbuf *pb) struct printbuf *pb)
{ {
int i=0; int i=0;
@@ -211,7 +211,7 @@ static int json_object_object_to_json_string(struct json_object* this,
/* CAW: scope operator to make ANSI correctness */ /* CAW: scope operator to make ANSI correctness */
/* CAW: switched to json_object_object_foreachC which uses an iterator struct */ /* CAW: switched to json_object_object_foreachC which uses an iterator struct */
json_object_object_foreachC(this, iter) { json_object_object_foreachC(jso, iter) {
if(i) sprintbuf(pb, ","); if(i) sprintbuf(pb, ",");
sprintbuf(pb, " \""); sprintbuf(pb, " \"");
json_escape_str(pb, iter.key); json_escape_str(pb, iter.key);
@@ -230,82 +230,82 @@ static void json_object_lh_entry_free(struct lh_entry *ent)
json_object_put((struct json_object*)ent->v); json_object_put((struct json_object*)ent->v);
} }
static void json_object_object_delete(struct json_object* this) static void json_object_object_delete(struct json_object* jso)
{ {
lh_table_free(this->o.c_object); lh_table_free(jso->o.c_object);
json_object_generic_delete(this); json_object_generic_delete(jso);
} }
struct json_object* json_object_new_object(void) struct json_object* json_object_new_object(void)
{ {
struct json_object *this = json_object_new(json_type_object); struct json_object *jso = json_object_new(json_type_object);
if(!this) return NULL; if(!jso) return NULL;
this->_delete = &json_object_object_delete; jso->_delete = &json_object_object_delete;
this->_to_json_string = &json_object_object_to_json_string; jso->_to_json_string = &json_object_object_to_json_string;
this->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
NULL, &json_object_lh_entry_free); NULL, &json_object_lh_entry_free);
return this; return jso;
} }
struct lh_table* json_object_get_object(struct json_object *this) struct lh_table* json_object_get_object(struct json_object *jso)
{ {
if(!this) return NULL; if(!jso) return NULL;
switch(this->o_type) { switch(jso->o_type) {
case json_type_object: case json_type_object:
return this->o.c_object; return jso->o.c_object;
default: default:
return NULL; return NULL;
} }
} }
void json_object_object_add(struct json_object* this, const char *key, void json_object_object_add(struct json_object* jso, const char *key,
struct json_object *val) struct json_object *val)
{ {
lh_table_delete(this->o.c_object, key); lh_table_delete(jso->o.c_object, key);
lh_table_insert(this->o.c_object, strdup(key), val); lh_table_insert(jso->o.c_object, strdup(key), val);
} }
struct json_object* json_object_object_get(struct json_object* this, const char *key) struct json_object* json_object_object_get(struct json_object* jso, const char *key)
{ {
return (struct json_object*) lh_table_lookup(this->o.c_object, key); return (struct json_object*) lh_table_lookup(jso->o.c_object, key);
} }
void json_object_object_del(struct json_object* this, const char *key) void json_object_object_del(struct json_object* jso, const char *key)
{ {
lh_table_delete(this->o.c_object, key); lh_table_delete(jso->o.c_object, key);
} }
/* json_object_boolean */ /* json_object_boolean */
static int json_object_boolean_to_json_string(struct json_object* this, static int json_object_boolean_to_json_string(struct json_object* jso,
struct printbuf *pb) struct printbuf *pb)
{ {
if(this->o.c_boolean) return sprintbuf(pb, "true"); if(jso->o.c_boolean) return sprintbuf(pb, "true");
else return sprintbuf(pb, "false"); else return sprintbuf(pb, "false");
} }
struct json_object* json_object_new_boolean(boolean b) struct json_object* json_object_new_boolean(boolean b)
{ {
struct json_object *this = json_object_new(json_type_boolean); struct json_object *jso = json_object_new(json_type_boolean);
if(!this) return NULL; if(!jso) return NULL;
this->_to_json_string = &json_object_boolean_to_json_string; jso->_to_json_string = &json_object_boolean_to_json_string;
this->o.c_boolean = b; jso->o.c_boolean = b;
return this; return jso;
} }
boolean json_object_get_boolean(struct json_object *this) boolean json_object_get_boolean(struct json_object *jso)
{ {
if(!this) return FALSE; if(!jso) return FALSE;
switch(this->o_type) { switch(jso->o_type) {
case json_type_boolean: case json_type_boolean:
return this->o.c_boolean; return jso->o.c_boolean;
case json_type_int: case json_type_int:
return (this->o.c_int != 0); return (jso->o.c_int != 0);
case json_type_double: case json_type_double:
return (this->o.c_double != 0); return (jso->o.c_double != 0);
case json_type_string: case json_type_string:
return (strlen(this->o.c_string) != 0); return (strlen(jso->o.c_string) != 0);
default: default:
return TRUE; return TRUE;
} }
@@ -314,35 +314,35 @@ boolean json_object_get_boolean(struct json_object *this)
/* json_object_int */ /* json_object_int */
static int json_object_int_to_json_string(struct json_object* this, static int json_object_int_to_json_string(struct json_object* jso,
struct printbuf *pb) struct printbuf *pb)
{ {
return sprintbuf(pb, "%d", this->o.c_int); return sprintbuf(pb, "%d", jso->o.c_int);
} }
struct json_object* json_object_new_int(int i) struct json_object* json_object_new_int(int i)
{ {
struct json_object *this = json_object_new(json_type_int); struct json_object *jso = json_object_new(json_type_int);
if(!this) return NULL; if(!jso) return NULL;
this->_to_json_string = &json_object_int_to_json_string; jso->_to_json_string = &json_object_int_to_json_string;
this->o.c_int = i; jso->o.c_int = i;
return this; return jso;
} }
int json_object_get_int(struct json_object *this) int json_object_get_int(struct json_object *jso)
{ {
int cint; int cint;
if(!this) return 0; if(!jso) return 0;
switch(this->o_type) { switch(jso->o_type) {
case json_type_int: case json_type_int:
return this->o.c_int; return jso->o.c_int;
case json_type_double: case json_type_double:
return (int)this->o.c_double; return (int)jso->o.c_double;
case json_type_boolean: case json_type_boolean:
return this->o.c_boolean; return jso->o.c_boolean;
case json_type_string: case json_type_string:
if(sscanf(this->o.c_string, "%d", &cint) == 1) return cint; if(sscanf(jso->o.c_string, "%d", &cint) == 1) return cint;
default: default:
return 0; return 0;
} }
@@ -351,35 +351,35 @@ int json_object_get_int(struct json_object *this)
/* json_object_double */ /* json_object_double */
static int json_object_double_to_json_string(struct json_object* this, static int json_object_double_to_json_string(struct json_object* jso,
struct printbuf *pb) struct printbuf *pb)
{ {
return sprintbuf(pb, "%lf", this->o.c_double); return sprintbuf(pb, "%lf", jso->o.c_double);
} }
struct json_object* json_object_new_double(double d) struct json_object* json_object_new_double(double d)
{ {
struct json_object *this = json_object_new(json_type_double); struct json_object *jso = json_object_new(json_type_double);
if(!this) return NULL; if(!jso) return NULL;
this->_to_json_string = &json_object_double_to_json_string; jso->_to_json_string = &json_object_double_to_json_string;
this->o.c_double = d; jso->o.c_double = d;
return this; return jso;
} }
double json_object_get_double(struct json_object *this) double json_object_get_double(struct json_object *jso)
{ {
double cdouble; double cdouble;
if(!this) return 0.0; if(!jso) return 0.0;
switch(this->o_type) { switch(jso->o_type) {
case json_type_double: case json_type_double:
return this->o.c_double; return jso->o.c_double;
case json_type_int: case json_type_int:
return this->o.c_int; return jso->o.c_int;
case json_type_boolean: case json_type_boolean:
return this->o.c_boolean; return jso->o.c_boolean;
case json_type_string: case json_type_string:
if(sscanf(this->o.c_string, "%lf", &cdouble) == 1) return cdouble; if(sscanf(jso->o.c_string, "%lf", &cdouble) == 1) return cdouble;
default: default:
return 0.0; return 0.0;
} }
@@ -388,66 +388,66 @@ double json_object_get_double(struct json_object *this)
/* json_object_string */ /* json_object_string */
static int json_object_string_to_json_string(struct json_object* this, static int json_object_string_to_json_string(struct json_object* jso,
struct printbuf *pb) struct printbuf *pb)
{ {
sprintbuf(pb, "\""); sprintbuf(pb, "\"");
json_escape_str(pb, this->o.c_string); json_escape_str(pb, jso->o.c_string);
sprintbuf(pb, "\""); sprintbuf(pb, "\"");
return 0; return 0;
} }
static void json_object_string_delete(struct json_object* this) static void json_object_string_delete(struct json_object* jso)
{ {
free(this->o.c_string); free(jso->o.c_string);
json_object_generic_delete(this); json_object_generic_delete(jso);
} }
struct json_object* json_object_new_string(const char *s) struct json_object* json_object_new_string(const char *s)
{ {
struct json_object *this = json_object_new(json_type_string); struct json_object *jso = json_object_new(json_type_string);
if(!this) return NULL; if(!jso) return NULL;
this->_delete = &json_object_string_delete; jso->_delete = &json_object_string_delete;
this->_to_json_string = &json_object_string_to_json_string; jso->_to_json_string = &json_object_string_to_json_string;
this->o.c_string = strdup(s); jso->o.c_string = strdup(s);
return this; return jso;
} }
struct json_object* json_object_new_string_len(const char *s, int len) struct json_object* json_object_new_string_len(const char *s, int len)
{ {
struct json_object *this = json_object_new(json_type_string); struct json_object *jso = json_object_new(json_type_string);
if(!this) return NULL; if(!jso) return NULL;
this->_delete = &json_object_string_delete; jso->_delete = &json_object_string_delete;
this->_to_json_string = &json_object_string_to_json_string; jso->_to_json_string = &json_object_string_to_json_string;
this->o.c_string = strndup(s, len); jso->o.c_string = strndup(s, len);
return this; return jso;
} }
const char* json_object_get_string(struct json_object *this) const char* json_object_get_string(struct json_object *jso)
{ {
if(!this) return NULL; if(!jso) return NULL;
switch(this->o_type) { switch(jso->o_type) {
case json_type_string: case json_type_string:
return this->o.c_string; return jso->o.c_string;
default: default:
return json_object_to_json_string(this); return json_object_to_json_string(jso);
} }
} }
/* json_object_array */ /* json_object_array */
static int json_object_array_to_json_string(struct json_object* this, static int json_object_array_to_json_string(struct json_object* jso,
struct printbuf *pb) struct printbuf *pb)
{ {
int i; int i;
sprintbuf(pb, "["); sprintbuf(pb, "[");
for(i=0; i < json_object_array_length(this); i++) { for(i=0; i < json_object_array_length(jso); i++) {
struct json_object *val; struct json_object *val;
if(i) { sprintbuf(pb, ", "); } if(i) { sprintbuf(pb, ", "); }
else { sprintbuf(pb, " "); } else { sprintbuf(pb, " "); }
val = json_object_array_get_idx(this, i); val = json_object_array_get_idx(jso, i);
if(val == NULL) { sprintbuf(pb, "null"); } if(val == NULL) { sprintbuf(pb, "null"); }
else { val->_to_json_string(val, pb); } else { val->_to_json_string(val, pb); }
} }
@@ -459,52 +459,52 @@ static void json_object_array_entry_free(void *data)
json_object_put((struct json_object*)data); json_object_put((struct json_object*)data);
} }
static void json_object_array_delete(struct json_object* this) static void json_object_array_delete(struct json_object* jso)
{ {
array_list_free(this->o.c_array); array_list_free(jso->o.c_array);
json_object_generic_delete(this); json_object_generic_delete(jso);
} }
struct json_object* json_object_new_array(void) struct json_object* json_object_new_array(void)
{ {
struct json_object *this = json_object_new(json_type_array); struct json_object *jso = json_object_new(json_type_array);
if(!this) return NULL; if(!jso) return NULL;
this->_delete = &json_object_array_delete; jso->_delete = &json_object_array_delete;
this->_to_json_string = &json_object_array_to_json_string; jso->_to_json_string = &json_object_array_to_json_string;
this->o.c_array = array_list_new(&json_object_array_entry_free); jso->o.c_array = array_list_new(&json_object_array_entry_free);
return this; return jso;
} }
struct array_list* json_object_get_array(struct json_object *this) struct array_list* json_object_get_array(struct json_object *jso)
{ {
if(!this) return NULL; if(!jso) return NULL;
switch(this->o_type) { switch(jso->o_type) {
case json_type_array: case json_type_array:
return this->o.c_array; return jso->o.c_array;
default: default:
return NULL; return NULL;
} }
} }
int json_object_array_length(struct json_object *this) int json_object_array_length(struct json_object *jso)
{ {
return array_list_length(this->o.c_array); return array_list_length(jso->o.c_array);
} }
int json_object_array_add(struct json_object *this,struct json_object *val) int json_object_array_add(struct json_object *jso,struct json_object *val)
{ {
return array_list_add(this->o.c_array, val); return array_list_add(jso->o.c_array, val);
} }
int json_object_array_put_idx(struct json_object *this, int idx, int json_object_array_put_idx(struct json_object *jso, int idx,
struct json_object *val) struct json_object *val)
{ {
return array_list_put_idx(this->o.c_array, idx, val); return array_list_put_idx(jso->o.c_array, idx, val);
} }
struct json_object* json_object_array_get_idx(struct json_object *this, struct json_object* json_object_array_get_idx(struct json_object *jso,
int idx) int idx)
{ {
return (struct json_object*)array_list_get_idx(this->o.c_array, idx); return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
} }