@@ -294,6 +294,7 @@ void test_json_dup_keys()
294
294
flb_free (data_out );
295
295
}
296
296
297
+ /* https://github.com/fluent/fluent-bit/issues/342 */
297
298
void test_json_pack_bug342 ()
298
299
{
299
300
int i = 0 ;
@@ -989,7 +990,7 @@ void test_json_pack_empty_array()
989
990
990
991
/* unpack just to validate the msgpack buffer */
991
992
msgpack_unpacked result ;
992
- msgpack_object obj ;
993
+
993
994
size_t off = 0 ;
994
995
msgpack_unpacked_init (& result );
995
996
off = 0 ;
@@ -1103,7 +1104,7 @@ void test_json_pack_large_uint64()
1103
1104
};
1104
1105
1105
1106
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 ;
1107
1108
len_in = strlen (p_in );
1108
1109
expected = test_cases [i ].expected_val ;
1109
1110
@@ -1137,6 +1138,50 @@ void test_json_pack_large_uint64()
1137
1138
}
1138
1139
}
1139
1140
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
+
1140
1185
TEST_LIST = {
1141
1186
/* JSON maps iteration */
1142
1187
{ "json_pack" , test_json_pack },
@@ -1161,7 +1206,7 @@ TEST_LIST = {
1161
1206
/* Mixed bytes, check JSON encoding */
1162
1207
{ "utf8_to_json" , test_utf8_to_json },
1163
1208
{ "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 },
1166
1211
{ 0 }
1167
1212
};
0 commit comments