-
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 3 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 |
---|---|---|
|
@@ -886,6 +886,16 @@ char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, cjbool fmt) | |
return print_value(item, 0, fmt, &p); | ||
} | ||
|
||
int cJSON_PrintMallocedBuffer(cJSON *item,char *buf,const size_t len, cjbool fmt) | ||
{ | ||
char *out; | ||
printbuffer p; | ||
p.buffer = buf; | ||
p.length = len; | ||
p.offset = 0; | ||
out = print_value(item,0,fmt,&p); | ||
return (out != buf ? -1 : 0); | ||
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. This should return 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. If this function returns a char pointer (effectively providing an alias to the input 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. Hm. Good point. Considering this, your way of returning true or false might actually be better. An API should be hard to misuse in a dangerous way (like having a double free because of the alias). This will then not allow the use of If you do it the way you suggested, you can just 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. That would be more concise. Made the change. |
||
} | ||
|
||
/* Parser core - when encountering text, process appropriately. */ | ||
static const char *parse_value(cJSON *item, const char *value, const char **ep) | ||
|
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.
By doing this you risk crashes, because
print_value
will try to reallocate the printbuffer when it is too short.