json: Minor cleanups in cJSON.c

* src/cJSON.c: Add comments on the origin of the code.
(parse_string): Allocate an extra byte for safeness.
(cJSON_AddItemToArray): Allo ARRAY to be NULL.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2018-07-16 14:42:30 +02:00
parent 013a7f47ab
commit 1933f5b805
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B

View File

@ -22,7 +22,14 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
* *
* Note that this code has been modified from the original code taken * Note that this code has been modified from the original code taken
* from cjson-code-58.zip. * from cjson-code-58.zip before 2014 (my first local commit was in
* 2014 but I may used the code even earlier). Since 2016 the project
* was revived and moved to https://github.com/DaveGamble/cJSON.git.
* It is now a lot more complex and has substantial changes so that it
* is not possible to merge them directly. In any case we only need a
* simple parser and not a complete library. I have looked through
* the commits and fixed a few things which should apply; I also added
* a few references to the upstream code. Regression test are missing!
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -232,6 +239,9 @@ parse_string (cJSON * item, const char *str, const char **ep)
char *out; char *out;
int len = 0; int len = 0;
unsigned uc, uc2; unsigned uc, uc2;
/* FIXME: We should consider eary failure like it is done with
* commit 8656386c4f4a12f1cf3d6b26158407fd05e65029 in upstream. */
if (*str != '\"') if (*str != '\"')
{ {
*ep = str; *ep = str;
@ -242,8 +252,10 @@ parse_string (cJSON * item, const char *str, const char **ep)
if (*ptr++ == '\\') if (*ptr++ == '\\')
ptr++; /* Skip escaped quotes. */ ptr++; /* Skip escaped quotes. */
out = xtrymalloc (len + 1); /* This is how long we need for the out = xtrymalloc (len + 2); /* This is how long we need for the
string, roughly. */ * string, roughly. We add one extra
* byte in case the last input
* character is a backslash. */
if (!out) if (!out)
return 0; return 0;
@ -322,6 +334,8 @@ parse_string (cJSON * item, const char *str, const char **ep)
ptr2 += len; ptr2 += len;
break; break;
default: default:
/* Fixme: Should we fail here: See
* https://github.com/DaveGamble/cJSON/issues/10 */
*ptr2++ = *ptr; *ptr2++ = *ptr;
break; break;
} }
@ -934,9 +948,11 @@ create_reference (cJSON * item)
void void
cJSON_AddItemToArray (cJSON * array, cJSON * item) cJSON_AddItemToArray (cJSON * array, cJSON * item)
{ {
cJSON *c = array->child; cJSON *c;
if (!item)
if (!item || !array)
return; return;
c = array->child;
if (!c) if (!c)
{ {
array->child = item; array->child = item;
@ -1137,6 +1153,8 @@ cJSON_ReplaceItemInObject (cJSON * object, const char *string,
i++, c = c->next; i++, c = c->next;
if (c) if (c)
{ {
/* FIXME: I guess we should free newitem->string here. See
* upstream commit 0d10e279c8b604f71829b5d49d092719f4ae96b6. */
newitem->string = xtrystrdup (string); newitem->string = xtrystrdup (string);
cJSON_ReplaceItemInArray (object, i, newitem); cJSON_ReplaceItemInArray (object, i, newitem);
} }