Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion fuzzing/cjson_read_fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
if(minify)
{
copied = (unsigned char*)malloc(size);
if(copied == NULL) return 0;
if(copied == NULL)
{
cJSON_Delete(json);
return 0;
}

memcpy(copied, data, size);

Expand Down
11 changes: 9 additions & 2 deletions fuzzing/fuzz_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); /* required by C89

int main(int argc, char **argv)
{
FILE *f;
FILE *f = NULL;
char *buf = NULL;
long siz_buf;

Expand All @@ -25,7 +25,10 @@ int main(int argc, char **argv)
goto err;
}

fseek(f, 0, SEEK_END);
if(fseek(f, 0, SEEK_END) != 0) {
fprintf(stderr, "fseek() to end failed\n");
goto err;
}

siz_buf = ftell(f);
rewind(f);
Expand All @@ -49,6 +52,10 @@ int main(int argc, char **argv)

err:
free(buf);
if (f != NULL)
{
fclose(f);
}

return 0;
}
7 changes: 7 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ static int print_preallocated(cJSON *root)

/* formatted print */
out = cJSON_Print(root);
if (out == NULL) {
printf("Failed to print cJSON object.\n");
return -1;
}

/* create buffer to succeed */
/* the extra 5 bytes are because of inaccuracies when reserving memory */
Expand All @@ -59,6 +63,7 @@ static int print_preallocated(cJSON *root)
if (buf == NULL)
{
printf("Failed to allocate memory.\n");
free(out);
exit(1);
}

Expand All @@ -68,6 +73,8 @@ static int print_preallocated(cJSON *root)
if (buf_fail == NULL)
{
printf("Failed to allocate memory.\n");
free(out);
free(buf);
exit(1);
}

Expand Down
7 changes: 6 additions & 1 deletion tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@

void reset(cJSON *item);
void reset(cJSON *item) {
if ((item != NULL) && (item->child != NULL))
if (item == NULL)
{
return;
}

if (item->child != NULL)
{
cJSON_Delete(item->child);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/json_patch_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ static cJSON_bool test_generate_test(cJSON *test)
TEST_ASSERT_NOT_NULL_MESSAGE(patch, "Failed to generate patches.");

printed_patch = cJSON_Print(patch);
printf("%s\n", printed_patch);
if (printed_patch)
{
printf("%s\n", printed_patch);
}
free(printed_patch);

/* apply the generated patch */
Expand Down