Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lja mods #517

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
113 changes: 113 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,116 @@
/include
/libjson-c.a
/libjson-c.so
/Makefile.in
/aclocal.m4
/bits.h
/compile
/config.guess
/config.h.in
/config.sub
/configure
/depcomp
/doc/html/README_8md.html
/doc/html/annotated.html
/doc/html/arraylist_8h.html
/doc/html/bc_s.png
/doc/html/bdwn.png
/doc/html/bits_8h.html
/doc/html/classes.html
/doc/html/closed.png
/doc/html/debug_8h.html
/doc/html/deprecated.html
/doc/html/doc.png
/doc/html/doxygen.css
/doc/html/doxygen.png
/doc/html/dynsections.js
/doc/html/files.html
/doc/html/folderclosed.png
/doc/html/folderopen.png
/doc/html/ftv2blank.png
/doc/html/ftv2doc.png
/doc/html/ftv2folderclosed.png
/doc/html/ftv2folderopen.png
/doc/html/ftv2lastnode.png
/doc/html/ftv2link.png
/doc/html/ftv2mlastnode.png
/doc/html/ftv2mnode.png
/doc/html/ftv2node.png
/doc/html/ftv2plastnode.png
/doc/html/ftv2pnode.png
/doc/html/ftv2splitbar.png
/doc/html/ftv2vertline.png
/doc/html/functions.html
/doc/html/functions_vars.html
/doc/html/globals.html
/doc/html/globals_a.html
/doc/html/globals_defs.html
/doc/html/globals_e.html
/doc/html/globals_enum.html
/doc/html/globals_eval.html
/doc/html/globals_f.html
/doc/html/globals_func.html
/doc/html/globals_h.html
/doc/html/globals_i.html
/doc/html/globals_j.html
/doc/html/globals_l.html
/doc/html/globals_m.html
/doc/html/globals_n.html
/doc/html/globals_p.html
/doc/html/globals_s.html
/doc/html/globals_t.html
/doc/html/globals_type.html
/doc/html/globals_vars.html
/doc/html/index.html
/doc/html/issues__closed__for__0_813_8md.html
/doc/html/jquery.js
/doc/html/json_8h.html
/doc/html/json__c__version_8h.html
/doc/html/json__inttypes_8h.html
/doc/html/json__object_8h.html
/doc/html/json__object__iterator_8h.html
/doc/html/json__object__private_8h.html
/doc/html/json__pointer_8h.html
/doc/html/json__tokener_8h.html
/doc/html/json__util_8h.html
/doc/html/json__visit_8h.html
/doc/html/linkhash_8h.html
/doc/html/math__compat_8h.html
/doc/html/md_issues_closed_for_0_813.html
/doc/html/menu.js
/doc/html/menudata.js
/doc/html/nav_f.png
/doc/html/nav_g.png
/doc/html/nav_h.png
/doc/html/open.png
/doc/html/pages.html
/doc/html/printbuf_8h.html
/doc/html/random__seed_8h.html
/doc/html/snprintf__compat_8h.html
/doc/html/splitbar.png
/doc/html/strdup__compat_8h.html
/doc/html/strerror__override_8h.html
/doc/html/strerror__override__private_8h.html
/doc/html/structarray__list.html
/doc/html/structjson__object.html
/doc/html/structjson__object__iter.html
/doc/html/structjson__object__iterator.html
/doc/html/structjson__tokener.html
/doc/html/structjson__tokener__srec.html
/doc/html/structlh__entry.html
/doc/html/structlh__table.html
/doc/html/structprintbuf.html
/doc/html/sync_off.png
/doc/html/sync_on.png
/doc/html/tab_a.png
/doc/html/tab_b.png
/doc/html/tab_h.png
/doc/html/tab_s.png
/doc/html/tabs.css
/doc/html/unionjson__object_1_1data.html
/doc/html/vasprintf__compat_8h.html
/install-sh
/ltmain.sh
/missing
/test-driver
/tests/Makefile.in
85 changes: 63 additions & 22 deletions json_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static struct json_object* json_object_new(enum json_type o_type)
{
struct json_object *jso;

jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
jso = (struct json_object*)calloc(1, sizeof(struct json_object));
if (!jso)
return NULL;
jso->o_type = o_type;
Expand Down Expand Up @@ -325,27 +325,35 @@ const char* json_object_to_json_string_length(struct json_object *jso, int flags
const char *r = NULL;
size_t s = 0;

if (!jso)
{
s = 4;
r = "null";
}
else if ((jso->_pb) || (jso->_pb = printbuf_new()))
/* Failure case set first */
s = 4;
r = "null";

/* no clutter and no fail cases after */
if (jso)
{
printbuf_reset(jso->_pb);
if ( jso->_pb == NULL )
jso->_pb = printbuf_new();

if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
if ( jso->_pb != NULL )
{
s = (size_t)jso->_pb->bpos;
r = jso->_pb->buf;
printbuf_reset(jso->_pb);

if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
{
s = (size_t)jso->_pb->bpos;
r = jso->_pb->buf;
}
}
}

if (length)
*length = s;

return r;
}


const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
{
return json_object_to_json_string_length(jso, flags, NULL);
Expand Down Expand Up @@ -400,10 +408,12 @@ static int json_object_object_to_json_string(struct json_object* jso,
indent(pb, level+1, flags);
printbuf_strappend(pb, "\"");
json_escape_str(pb, iter.key, strlen(iter.key), flags);

if (flags & JSON_C_TO_STRING_SPACED)
printbuf_strappend(pb, "\": ");
else
printbuf_strappend(pb, "\":");

if(iter.val == NULL)
printbuf_strappend(pb, "null");
else
Expand Down Expand Up @@ -467,6 +477,19 @@ struct lh_table* json_object_get_object(const struct json_object *jso)
}
}

static char *stringdup(const char *s)
{
size_t len = strnlen (s, 1024) + 1;
char *new = malloc (len);

if (new == NULL)
return NULL;

new[len] = 0;
return (char *) memcpy (new, s, len);
}


int json_object_object_add_ex(struct json_object* jso,
const char *const key,
struct json_object *const val,
Expand All @@ -481,9 +504,9 @@ int json_object_object_add_ex(struct json_object* jso,
// We lookup the entry and replace the value, rather than just deleting
// and re-adding it, so the existing key remains valid.
hash = lh_get_hash(jso->o.c_object, (const void *)key);
existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
lh_table_lookup_entry_w_hash(jso->o.c_object,
(const void *)key, hash);
existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ?
NULL :
lh_table_lookup_entry_w_hash(jso->o.c_object, (const void *)key, hash);

// The caller must avoid creating loops in the object tree, but do a
// quick check anyway to make sure we're not creating a trivial loop.
Expand All @@ -492,17 +515,26 @@ int json_object_object_add_ex(struct json_object* jso,

if (!existing_entry)
{
const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
(const void *)key : strdup(key);
char *key_dup = stringdup(key);

/* key duplicate must be done first */
const void *const k = ((opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? (const void *)key : (const void *)key_dup);

/* key duplicate must be freed here if constant */
if (opts & JSON_C_OBJECT_KEY_IS_CONSTANT)
free(key_dup);

if (k == NULL)
return -1;

return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
} else {
existing_value = (json_object *) lh_entry_v(existing_entry);
if (existing_value)
json_object_put(existing_value);
existing_entry->v = val;
return 0;
}
existing_value = (json_object *) lh_entry_v(existing_entry);
if (existing_value)
json_object_put(existing_value);
existing_entry->v = val;
return 0;
}

int json_object_object_add(struct json_object* jso, const char *key,
Expand Down Expand Up @@ -1491,7 +1523,15 @@ int json_object_deep_copy(struct json_object *src, struct json_object **dst, jso
int rc;

/* Check if arguments are sane ; *dst must not point to a non-NULL object */
if (!src || !dst || *dst) {
if (!src) {
errno = EINVAL;
return -1;
}
if (!dst) {
errno = EINVAL;
return -1;
}
if (*dst) {
errno = EINVAL;
return -1;
}
Expand All @@ -1501,6 +1541,7 @@ int json_object_deep_copy(struct json_object *src, struct json_object **dst, jso

rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
if (rc < 0) {

json_object_put(*dst);
*dst = NULL;
}
Expand Down