Skip to content

Commit 5b731d6

Browse files
committed
fix: bad pointer handling
Closes #21
1 parent 169d544 commit 5b731d6

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Diff for: jsmn-find.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ jsmnf_find_path(const struct jsmnf_pair *head,
407407
const unsigned new_size = *(prev_size)*2; \
408408
void *tmp = realloc((ptr), new_size * sizeof *(ptr)); \
409409
if (!tmp) return JSMN_ERROR_NOMEM; \
410-
memset((tmp) + *(prev_size), 0, \
411-
(new_size - *(prev_size)) * sizeof *(ptr)); \
412410
(ptr) = tmp; \
411+
memset((ptr) + *(prev_size), 0, \
412+
(new_size - *(prev_size)) * sizeof *(ptr)); \
413413
*(prev_size) = new_size; \
414414
} while (0)
415415

Diff for: test/functions.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ print_jsmnerr(enum jsmnerr code)
2727
return "Unknown error";
2828
}
2929

30+
TEST
31+
check_not_corrupted(void)
32+
{
33+
const char json[] =
34+
"{\"status\":\"enabled\",\"timestamp\":\"2023-08-24T07:59:03Z\"}\n";
35+
jsmn_parser parser;
36+
jsmntok_t *toks = NULL;
37+
unsigned num_tokens = 0;
38+
long ret;
39+
40+
jsmn_init(&parser);
41+
42+
ret = jsmn_parse_auto(&parser, json, strlen(json), &toks, &num_tokens);
43+
ASSERT_GTm(print_jsmnerr(ret),
44+
ret = jsmn_parse_auto(&parser, json, sizeof(json) - 1, &toks,
45+
&num_tokens),
46+
0);
47+
48+
PASS();
49+
}
50+
51+
SUITE(fn__jsmn_load_auto)
52+
{
53+
RUN_TEST(check_not_corrupted);
54+
}
55+
3056
TEST
3157
check_load_dynamic_pairs(void)
3258
{
@@ -385,7 +411,9 @@ check_find_array(void)
385411
PASS();
386412
}
387413

388-
#define OBJ1_NEST "{ \"username\": null, \"avatar\": null, \"avatar_decoration\": null, \"bar\": null, \"foo\": 1 }"
414+
#define OBJ1_NEST \
415+
"{ \"username\": null, \"avatar\": null, \"avatar_decoration\": null, " \
416+
"\"bar\": null, \"foo\": 1 }"
389417
#define OBJ1 "{ \"foo\": " OBJ1_NEST " }"
390418
#define OBJ2 "{ \"foo\": null }"
391419
TEST

0 commit comments

Comments
 (0)