@@ -1245,50 +1245,68 @@ del_extensions_cache_value(void *raw)
12451245 }
12461246}
12471247
1248+ /* The key of the extensions cache: the raw content of two strings.
1249+
1250+ The UTF-8 encoding is not used, because the strings can contain lone
1251+ surrogates, e.g. a file name undecodable in the filesystem encoding. */
1252+ struct hashtable_key {
1253+ size_t size ; /* the total size of the key */
1254+ unsigned char kind1 ;
1255+ unsigned char kind2 ;
1256+ Py_ssize_t len1 ;
1257+ /* followed by the raw content of both strings */
1258+ };
1259+
12481260static void *
1249- hashtable_key_from_2_strings (PyObject * str1 , PyObject * str2 , const char sep )
1261+ hashtable_key_from_2_strings (PyObject * str1 , PyObject * str2 )
12501262{
1251- const char * str1_data = _PyUnicode_AsUTF8NoNUL (str1 );
1252- const char * str2_data = _PyUnicode_AsUTF8NoNUL (str2 );
1253- if (str1_data == NULL || str2_data == NULL ) {
1254- return NULL ;
1255- }
1256- Py_ssize_t str1_len = strlen (str1_data );
1257- Py_ssize_t str2_len = strlen (str2_data );
1263+ Py_ssize_t len1 = PyUnicode_GET_LENGTH (str1 );
1264+ Py_ssize_t len2 = PyUnicode_GET_LENGTH (str2 );
1265+ int kind1 = PyUnicode_KIND (str1 );
1266+ int kind2 = PyUnicode_KIND (str2 );
1267+ size_t size1 = (size_t )len1 * kind1 ;
1268+ size_t size2 = (size_t )len2 * kind2 ;
12581269
1259- /* Make sure sep and the NULL byte won't cause an overflow. */
1260- assert (SIZE_MAX - str1_len - str2_len > 2 );
1261- size_t size = str1_len + 1 + str2_len + 1 ;
1270+ assert (SIZE_MAX - sizeof (struct hashtable_key ) - size1 > size2 );
1271+ size_t size = sizeof (struct hashtable_key ) + size1 + size2 ;
12621272
12631273 // XXX Use a buffer if it's a temp value (every case but "set").
1264- char * key = PyMem_RawMalloc (size );
1274+ struct hashtable_key * key = PyMem_RawMalloc (size );
12651275 if (key == NULL ) {
12661276 PyErr_NoMemory ();
12671277 return NULL ;
12681278 }
12691279
1270- memcpy (key , str1_data , str1_len );
1271- key [str1_len ] = sep ;
1272- memcpy (key + str1_len + 1 , str2_data , str2_len );
1273- key [size - 1 ] = '\0' ;
1274- assert (strlen (key ) == size - 1 );
1280+ /* Clear the padding: the key is hashed and compared as raw bytes. */
1281+ memset (key , 0 , sizeof (struct hashtable_key ));
1282+ key -> size = size ;
1283+ key -> kind1 = (unsigned char )kind1 ;
1284+ key -> kind2 = (unsigned char )kind2 ;
1285+ key -> len1 = len1 ;
1286+ char * data = (char * )(key + 1 );
1287+ memcpy (data , PyUnicode_DATA (str1 ), size1 );
1288+ memcpy (data + size1 , PyUnicode_DATA (str2 ), size2 );
12751289 return key ;
12761290}
12771291
12781292static Py_uhash_t
1279- hashtable_hash_str (const void * key )
1293+ hashtable_hash_key (const void * key )
12801294{
1281- return Py_HashBuffer (key , strlen ((const char * )key ));
1295+ return Py_HashBuffer (key , ((const struct hashtable_key * )key )-> size );
12821296}
12831297
12841298static int
1285- hashtable_compare_str (const void * key1 , const void * key2 )
1299+ hashtable_compare_key (const void * key1 , const void * key2 )
12861300{
1287- return strcmp ((const char * )key1 , (const char * )key2 ) == 0 ;
1301+ size_t size = ((const struct hashtable_key * )key1 )-> size ;
1302+ if (size != ((const struct hashtable_key * )key2 )-> size ) {
1303+ return 0 ;
1304+ }
1305+ return memcmp (key1 , key2 , size ) == 0 ;
12881306}
12891307
12901308static void
1291- hashtable_destroy_str (void * ptr )
1309+ hashtable_destroy_key (void * ptr )
12921310{
12931311 PyMem_RawFree (ptr );
12941312}
@@ -1328,16 +1346,15 @@ _find_cached_def(PyModuleDef *def)
13281346}
13291347#endif
13301348
1331- #define HTSEP ':'
13321349
13331350static int
13341351_extensions_cache_init (void )
13351352{
13361353 _Py_hashtable_allocator_t alloc = {PyMem_RawMalloc , PyMem_RawFree };
13371354 EXTENSIONS .hashtable = _Py_hashtable_new_full (
1338- hashtable_hash_str ,
1339- hashtable_compare_str ,
1340- hashtable_destroy_str , // key
1355+ hashtable_hash_key ,
1356+ hashtable_compare_key ,
1357+ hashtable_destroy_key , // key
13411358 del_extensions_cache_value , // value
13421359 & alloc
13431360 );
@@ -1355,7 +1372,7 @@ _extensions_cache_find_unlocked(PyObject *path, PyObject *name,
13551372 if (EXTENSIONS .hashtable == NULL ) {
13561373 return NULL ;
13571374 }
1358- void * key = hashtable_key_from_2_strings (path , name , HTSEP );
1375+ void * key = hashtable_key_from_2_strings (path , name );
13591376 if (key == NULL ) {
13601377 return NULL ;
13611378 }
@@ -1365,7 +1382,7 @@ _extensions_cache_find_unlocked(PyObject *path, PyObject *name,
13651382 * p_key = key ;
13661383 }
13671384 else {
1368- hashtable_destroy_str (key );
1385+ hashtable_destroy_key (key );
13691386 }
13701387 return entry ;
13711388}
@@ -1503,7 +1520,7 @@ _extensions_cache_set(PyObject *path, PyObject *name,
15031520finally_oldvalue :
15041521 extensions_lock_release ();
15051522 if (key != NULL ) {
1506- hashtable_destroy_str (key );
1523+ hashtable_destroy_key (key );
15071524 }
15081525
15091526 return value ;
@@ -1547,7 +1564,6 @@ _extensions_cache_clear_all(void)
15471564 EXTENSIONS .hashtable = NULL ;
15481565}
15491566
1550- #undef HTSEP
15511567
15521568
15531569static bool
0 commit comments