Skip to content

Commit

Permalink
fix: bad pointer handling
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
lcsmuller committed Sep 6, 2023
1 parent 169d544 commit 5b731d6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions jsmn-find.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,9 @@ jsmnf_find_path(const struct jsmnf_pair *head,
const unsigned new_size = *(prev_size)*2; \
void *tmp = realloc((ptr), new_size * sizeof *(ptr)); \
if (!tmp) return JSMN_ERROR_NOMEM; \
memset((tmp) + *(prev_size), 0, \
(new_size - *(prev_size)) * sizeof *(ptr)); \
(ptr) = tmp; \
memset((ptr) + *(prev_size), 0, \
(new_size - *(prev_size)) * sizeof *(ptr)); \
*(prev_size) = new_size; \
} while (0)

Expand Down
30 changes: 29 additions & 1 deletion test/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ print_jsmnerr(enum jsmnerr code)
return "Unknown error";
}

TEST
check_not_corrupted(void)
{
const char json[] =
"{\"status\":\"enabled\",\"timestamp\":\"2023-08-24T07:59:03Z\"}\n";
jsmn_parser parser;
jsmntok_t *toks = NULL;
unsigned num_tokens = 0;
long ret;

jsmn_init(&parser);

ret = jsmn_parse_auto(&parser, json, strlen(json), &toks, &num_tokens);
ASSERT_GTm(print_jsmnerr(ret),
ret = jsmn_parse_auto(&parser, json, sizeof(json) - 1, &toks,
&num_tokens),
0);

PASS();
}

SUITE(fn__jsmn_load_auto)
{
RUN_TEST(check_not_corrupted);
}

TEST
check_load_dynamic_pairs(void)
{
Expand Down Expand Up @@ -385,7 +411,9 @@ check_find_array(void)
PASS();
}

#define OBJ1_NEST "{ \"username\": null, \"avatar\": null, \"avatar_decoration\": null, \"bar\": null, \"foo\": 1 }"
#define OBJ1_NEST \
"{ \"username\": null, \"avatar\": null, \"avatar_decoration\": null, " \
"\"bar\": null, \"foo\": 1 }"
#define OBJ1 "{ \"foo\": " OBJ1_NEST " }"
#define OBJ2 "{ \"foo\": null }"
TEST
Expand Down

0 comments on commit 5b731d6

Please sign in to comment.