@@ -435,6 +435,73 @@ void test_write_raw_str_special_bytes()
435435 write_raw_str_test_cases (cases );
436436}
437437
438+ void test_write_raw_str_invalid_sequences ()
439+ {
440+ struct write_str_case cases [] = {
441+ /*
442+ * Case 1: Stray continuation byte (0x80)
443+ */
444+ {
445+ "hello \x80 world" , 13 ,
446+ "hello \xEF\xBF\xBD world" ,
447+ FLB_TRUE
448+ },
449+
450+ /*
451+ * Case 2: Incomplete multi-byte sequence
452+ */
453+ {
454+ "a\xE6\x97" "b" , 4 ,
455+ "a" "\xEF\xBF\xBD" "\xEF\xBF\xBD" "b" ,
456+ FLB_TRUE
457+ },
458+
459+ /*
460+ * Case 3: Overlong encoding
461+ */
462+ {
463+ "a\xC0\xAF" "b" , 4 ,
464+ "a" "\xEF\xBF\xBD" "\xEF\xBF\xBD" "b" ,
465+ FLB_TRUE
466+ },
467+
468+ /*
469+ * Case 4: With an invalid starting byte
470+ */
471+ {
472+ "start-\xFF-end" , 11 ,
473+ "start-\xEF\xBF\xBD-end" ,
474+ FLB_TRUE
475+ },
476+
477+ /*
478+ * Case 5: Mix of valid and invalid sequences
479+ */
480+ {
481+ /* Input: "你好<stray_byte>世界" */
482+ "\xE4\xBD\xA0\xE5\xA5\xBD" "\x80" "\xE4\xB8\x96\xE7\x95\x8C" , 13 ,
483+ /* Output: "你好<replacement_char>世界" */
484+ "\xE4\xBD\xA0\xE5\xA5\xBD" "\xEF\xBF\xBD" "\xE4\xB8\x96\xE7\x95\x8C" ,
485+ FLB_TRUE
486+ },
487+
488+ /*
489+ * Case 6: Sequence with invalid continuation byte
490+ */
491+ {
492+ /* Input: "a" + 日(E6 97 A5) + ASCII "b" */
493+ "a\xE6\x97" "b" , 4 ,
494+ "a" "\xEF\xBF\xBD" "\xEF\xBF\xBD" "b" ,
495+ FLB_TRUE
496+ },
497+
498+ /* End of cases */
499+ { 0 }
500+ };
501+
502+ write_raw_str_test_cases (cases );
503+ }
504+
438505void test_write_str_invalid_leading_byte_case_2 ()
439506{
440507
@@ -858,6 +925,7 @@ TEST_LIST = {
858925 { "write_str" , test_write_str },
859926 { "write_str_special_bytes" , test_write_str_special_bytes },
860927 { "write_raw_str_special_bytes" , test_write_raw_str_special_bytes },
928+ { "write_raw_str_invalid_bytes" , test_write_raw_str_invalid_sequences },
861929 { "test_write_str_invalid_trailing_bytes" , test_write_str_invalid_trailing_bytes },
862930 { "test_write_str_invalid_leading_byte" , test_write_str_invalid_leading_byte },
863931 { "test_write_str_edge_cases" , test_write_str_edge_cases },
0 commit comments