@@ -36,6 +36,14 @@ static uint32_t get_utf8_codepoint_length(const char *buf, uint32_t len) {
3636 return codepoint_len ;
3737}
3838
39+ static uint32_t calculate_padded_length (uint32_t byte_len ) {
40+ const uint32_t bson_overhead = 5 ;
41+ // Calculate length with CBC padding:
42+ uint32_t encrypted_len = _mcFLE2v2AEADAlgorithm ()-> get_ciphertext_len (byte_len + bson_overhead , NULL )
43+ - MONGOCRYPT_IV_LEN - MONGOCRYPT_HMAC_LEN ;
44+ return encrypted_len - bson_overhead ;
45+ }
46+
3947static void test_nofold_suffix_prefix_case (_mongocrypt_tester_t * tester ,
4048 const char * str ,
4149 uint32_t lb ,
@@ -52,7 +60,7 @@ static void test_nofold_suffix_prefix_case(_mongocrypt_tester_t *tester,
5260 uint32_t byte_len = (uint32_t )strlen (str );
5361 uint32_t unfolded_codepoint_len = byte_len == 0 ? 1 : get_utf8_codepoint_length (str , byte_len );
5462 uint32_t folded_codepoint_len = byte_len == 0 ? 0 : unfolded_codepoint_len - foldable_codepoints ;
55- uint32_t padded_len = 16 * ( uint32_t )(( byte_len + 5 + 15 ) / 16 ) - 5 ;
63+ uint32_t padded_len = calculate_padded_length ( byte_len ) ;
5664 uint32_t max_affix_len = BSON_MIN (ub , folded_codepoint_len );
5765 uint32_t n_real_affixes = max_affix_len >= lb ? max_affix_len - lb + 1 : 0 ;
5866 uint32_t n_affixes = BSON_MIN (ub , padded_len ) - lb + 1 ;
@@ -234,7 +242,7 @@ static void test_nofold_substring_case(_mongocrypt_tester_t *tester,
234242 uint32_t byte_len = (uint32_t )strlen (str );
235243 uint32_t unfolded_codepoint_len = byte_len == 0 ? 1 : get_utf8_codepoint_length (str , byte_len );
236244 uint32_t folded_codepoint_len = byte_len == 0 ? 0 : unfolded_codepoint_len - foldable_codepoints ;
237- uint32_t padded_len = 16 * ( uint32_t )(( byte_len + 5 + 15 ) / 16 ) - 5 ;
245+ uint32_t padded_len = calculate_padded_length ( byte_len ) ;
238246 uint32_t n_substrings = calc_number_of_substrings (BSON_MIN (padded_len , mlen ), lb , ub );
239247
240248 mongocrypt_status_t * status = mongocrypt_status_new ();
@@ -572,6 +580,9 @@ static void _test_text_search_str_encode_suffix_prefix(_mongocrypt_tester_t *tes
572580 bson_free (short_s );
573581 bson_free (medium_s );
574582 bson_free (long_s );
583+
584+ // Test fixed strings where byte_len+5 is a multiple of 16. Regression test for MONGOCRYPT-917
585+ test_nofold_suffix_prefix_case (tester , "abcdefghijk" /* 11 chars */ , 1 , 30 , false, false, 0 );
575586}
576587
577588static void substring_run_folding_case (_mongocrypt_tester_t * tester ,
@@ -1125,6 +1136,9 @@ static void _test_text_search_str_encode_substring(_mongocrypt_tester_t *tester)
11251136 bson_free (short_s );
11261137 bson_free (medium_s );
11271138 bson_free (long_s );
1139+
1140+ // Test fixed strings where byte_len+5 is a multiple of 16. Regression test for MONGOCRYPT-917.
1141+ test_nofold_substring_case (tester , "abcdefghijk" /* 11 chars */ , 1 , 30 , 16 , false, false, 0 );
11281142}
11291143
11301144static void _test_text_search_str_encode_multiple (_mongocrypt_tester_t * tester ) {
0 commit comments