Skip to content

Commit 1eefbf3

Browse files
committed
tests: internal: pack: add test_json_pack_token_count_overflow test
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 1344816 commit 1eefbf3

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

tests/internal/pack.c

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ void test_json_dup_keys()
294294
flb_free(data_out);
295295
}
296296

297+
/* https://github.com/fluent/fluent-bit/issues/342 */
297298
void test_json_pack_bug342()
298299
{
299300
int i = 0;
@@ -989,7 +990,7 @@ void test_json_pack_empty_array()
989990

990991
/* unpack just to validate the msgpack buffer */
991992
msgpack_unpacked result;
992-
msgpack_object obj;
993+
993994
size_t off = 0;
994995
msgpack_unpacked_init(&result);
995996
off = 0;
@@ -1103,7 +1104,7 @@ void test_json_pack_large_uint64()
11031104
};
11041105

11051106
for (i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
1106-
p_in = test_cases[i].json_str;
1107+
p_in = (char *) test_cases[i].json_str;
11071108
len_in = strlen(p_in);
11081109
expected = test_cases[i].expected_val;
11091110

@@ -1137,6 +1138,50 @@ void test_json_pack_large_uint64()
11371138
}
11381139
}
11391140

1141+
void test_json_pack_token_count_overflow()
1142+
{
1143+
int i;
1144+
flb_sds_t json = NULL;
1145+
struct flb_pack_state state;
1146+
int ret;
1147+
1148+
flb_pack_state_init(&state);
1149+
1150+
/* Create a JSON array big enough to trigger realloc in flb_json_tokenise */
1151+
json = flb_sds_create("[");
1152+
for (i = 0; i < 300; i++) {
1153+
if (i > 0) {
1154+
flb_sds_cat_safe(&json, ",", 1);
1155+
}
1156+
json = flb_sds_printf(&json, "%d", i);
1157+
}
1158+
flb_sds_cat_safe(&json, "]", 1);
1159+
1160+
if (!TEST_CHECK(json != NULL)) {
1161+
TEST_MSG("Failed to allocate JSON string");
1162+
exit(1);
1163+
}
1164+
1165+
/* First parse: forces realloc at least once (because by default we have space for 256 tokens) */
1166+
ret = flb_json_tokenise(json, flb_sds_len(json), &state);
1167+
TEST_CHECK(ret == 0);
1168+
printf("\nFirst parse: tokens_count=%d\n", state.tokens_count);
1169+
1170+
/* Second parse with the same JSON and same state — should be ~301, but will be doubled if bug exists */
1171+
ret = flb_json_tokenise(json, flb_sds_len(json), &state);
1172+
TEST_CHECK(ret == 0);
1173+
printf("Second parse: tokens_count=%d (BUG if > ~301)\n", state.tokens_count);
1174+
1175+
TEST_CHECK(state.tokens_count == 301);
1176+
if (state.tokens_count != 301) {
1177+
TEST_MSG("tokens_count=%d (BUG if > ~301)\n", state.tokens_count);
1178+
exit(1);
1179+
}
1180+
1181+
flb_sds_destroy(json);
1182+
flb_pack_state_reset(&state);
1183+
}
1184+
11401185
TEST_LIST = {
11411186
/* JSON maps iteration */
11421187
{ "json_pack" , test_json_pack },
@@ -1161,7 +1206,7 @@ TEST_LIST = {
11611206
/* Mixed bytes, check JSON encoding */
11621207
{ "utf8_to_json", test_utf8_to_json},
11631208
{ "json_pack_surrogate_pairs", test_json_pack_surrogate_pairs},
1164-
{ "json_pack_surrogate_pairs_with_replacement",
1165-
test_json_pack_surrogate_pairs_with_replacement},
1209+
{ "json_pack_surrogate_pairs_with_replacement", test_json_pack_surrogate_pairs_with_replacement},
1210+
{ "json_pack_token_count_overflow", test_json_pack_token_count_overflow},
11661211
{ 0 }
11671212
};

0 commit comments

Comments
 (0)