-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Added a function to print to pre-allocated buffer #72
Changes from all commits
2387d9d
1193d24
df093c3
42496b2
4150c30
6ce0915
6e1b4ba
652bb77
e559516
e3e7726
32f3316
7083240
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,6 +244,7 @@ typedef struct | |
char *buffer; | ||
int length; | ||
int offset; | ||
cjbool noalloc; | ||
} printbuffer; | ||
|
||
/* realloc printbuffer if necessary to have at least "needed" bytes more */ | ||
|
@@ -261,12 +262,17 @@ static char* ensure(printbuffer *p, int needed) | |
return p->buffer + p->offset; | ||
} | ||
|
||
if (p->noalloc) { | ||
return NULL; | ||
} | ||
|
||
newsize = pow2gt(needed); | ||
newbuffer = (char*)cJSON_malloc(newsize); | ||
if (!newbuffer) | ||
{ | ||
cJSON_free(p->buffer); | ||
p->length = 0; | ||
p->noalloc = false; | ||
p->buffer = NULL; | ||
|
||
return NULL; | ||
|
@@ -882,10 +888,20 @@ char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, cjbool fmt) | |
} | ||
p.length = prebuffer; | ||
p.offset = 0; | ||
p.noalloc = false; | ||
|
||
return print_value(item, 0, fmt, &p); | ||
} | ||
|
||
int cJSON_PrintPreallocated(cJSON *item,char *buf, const int len, const cjbool fmt) | ||
{ | ||
printbuffer p; | ||
p.buffer = buf; | ||
p.length = len; | ||
p.offset = 0; | ||
p.noalloc = true; | ||
return (print_value(item,0,fmt,&p) != NULL ? 0 : -1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant it exactly the other way around. Think of the return value as a bool. So |
||
} | ||
|
||
/* Parser core - when encountering text, process appropriately. */ | ||
static const char *parse_value(cJSON *item, const char *value, const char **ep) | ||
|
@@ -1137,7 +1153,10 @@ static char *print_array(const cJSON *item, int depth, cjbool fmt, printbuffer * | |
child = item->child; | ||
while (child && !fail) | ||
{ | ||
print_value(child, depth + 1, fmt, p); | ||
if (!print_value(child, depth + 1, fmt, p)) | ||
{ | ||
return NULL; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to: if (!print_value(child, depth + 1, fmt, p))
{
return NULL;
} Thank you for finding this problem btw. after that I was taking a closer look to the rest of the library and there are quite some cases where out of memory errors are not handled properly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will also fix these other cases in the next release. |
||
p->offset = update(p); | ||
if (child->next) | ||
{ | ||
|
@@ -1451,7 +1470,10 @@ static char *print_object(const cJSON *item, int depth, cjbool fmt, printbuffer | |
p->offset+=len; | ||
|
||
/* print value */ | ||
print_value(child, depth, fmt, p); | ||
if (!print_value(child, depth, fmt, p)) | ||
{ | ||
return NULL; | ||
}; | ||
p->offset = update(p); | ||
|
||
/* print comma if not last */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't try to readd changes that I merged to the master branch already. I will handle merge conflicts myself.