mirror of
https://github.com/DaveGamble/cJSON
synced 2025-03-23 19:37:35 +00:00
test: exit on malloc failure
This commit is contained in:
parent
eeb6d237b6
commit
f10e2f884a
15
test.c
15
test.c
@ -61,6 +61,11 @@ static void dofile(char *filename)
|
|||||||
fseek(f, 0, SEEK_SET);
|
fseek(f, 0, SEEK_SET);
|
||||||
|
|
||||||
data = (char*)malloc(len + 1);
|
data = (char*)malloc(len + 1);
|
||||||
|
if (data == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to allocate memory.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
fread(data, 1, len, f);
|
fread(data, 1, len, f);
|
||||||
data[len] = '\0';
|
data[len] = '\0';
|
||||||
@ -102,10 +107,20 @@ static int print_preallocated(cJSON *root)
|
|||||||
/* the extra 64 bytes are in case a floating point value is printed */
|
/* the extra 64 bytes are in case a floating point value is printed */
|
||||||
len = strlen(out) + 64;
|
len = strlen(out) + 64;
|
||||||
buf = malloc(len);
|
buf = malloc(len);
|
||||||
|
if (buf == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to allocate memory.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
/* create buffer to fail */
|
/* create buffer to fail */
|
||||||
len_fail = strlen(out);
|
len_fail = strlen(out);
|
||||||
buf_fail = malloc(len_fail);
|
buf_fail = malloc(len_fail);
|
||||||
|
if (buf_fail == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to allocate memory.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
/* Print to buffer */
|
/* Print to buffer */
|
||||||
if (cJSON_PrintPreallocated(root, buf, len, 1) != 0) {
|
if (cJSON_PrintPreallocated(root, buf, len, 1) != 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user