@@ -1303,50 +1303,68 @@ del_extensions_cache_value(void *raw)
13031303 }
13041304}
13051305
1306+ /* The key of the extensions cache: the raw content of two strings.
1307+
1308+ The UTF-8 encoding is not used, because the strings can contain lone
1309+ surrogates, e.g. a file name undecodable in the filesystem encoding. */
1310+ struct hashtable_key {
1311+ size_t size ; /* the total size of the key */
1312+ unsigned char kind1 ;
1313+ unsigned char kind2 ;
1314+ Py_ssize_t len1 ;
1315+ /* followed by the raw content of both strings */
1316+ };
1317+
13061318static void *
1307- hashtable_key_from_2_strings (PyObject * str1 , PyObject * str2 , const char sep )
1319+ hashtable_key_from_2_strings (PyObject * str1 , PyObject * str2 )
13081320{
1309- const char * str1_data = _PyUnicode_AsUTF8NoNUL (str1 );
1310- const char * str2_data = _PyUnicode_AsUTF8NoNUL (str2 );
1311- if (str1_data == NULL || str2_data == NULL ) {
1312- return NULL ;
1313- }
1314- Py_ssize_t str1_len = strlen (str1_data );
1315- Py_ssize_t str2_len = strlen (str2_data );
1321+ Py_ssize_t len1 = PyUnicode_GET_LENGTH (str1 );
1322+ Py_ssize_t len2 = PyUnicode_GET_LENGTH (str2 );
1323+ int kind1 = PyUnicode_KIND (str1 );
1324+ int kind2 = PyUnicode_KIND (str2 );
1325+ size_t size1 = (size_t )len1 * kind1 ;
1326+ size_t size2 = (size_t )len2 * kind2 ;
13161327
1317- /* Make sure sep and the NULL byte won't cause an overflow. */
1318- assert (SIZE_MAX - str1_len - str2_len > 2 );
1319- size_t size = str1_len + 1 + str2_len + 1 ;
1328+ assert (SIZE_MAX - sizeof (struct hashtable_key ) - size1 > size2 );
1329+ size_t size = sizeof (struct hashtable_key ) + size1 + size2 ;
13201330
13211331 // XXX Use a buffer if it's a temp value (every case but "set").
1322- char * key = PyMem_RawMalloc (size );
1332+ struct hashtable_key * key = PyMem_RawMalloc (size );
13231333 if (key == NULL ) {
13241334 PyErr_NoMemory ();
13251335 return NULL ;
13261336 }
13271337
1328- memcpy (key , str1_data , str1_len );
1329- key [str1_len ] = sep ;
1330- memcpy (key + str1_len + 1 , str2_data , str2_len );
1331- key [size - 1 ] = '\0' ;
1332- assert (strlen (key ) == size - 1 );
1338+ /* Clear the padding: the key is hashed and compared as raw bytes. */
1339+ memset (key , 0 , sizeof (struct hashtable_key ));
1340+ key -> size = size ;
1341+ key -> kind1 = (unsigned char )kind1 ;
1342+ key -> kind2 = (unsigned char )kind2 ;
1343+ key -> len1 = len1 ;
1344+ char * data = (char * )(key + 1 );
1345+ memcpy (data , PyUnicode_DATA (str1 ), size1 );
1346+ memcpy (data + size1 , PyUnicode_DATA (str2 ), size2 );
13331347 return key ;
13341348}
13351349
13361350static Py_uhash_t
1337- hashtable_hash_str (const void * key )
1351+ hashtable_hash_key (const void * key )
13381352{
1339- return Py_HashBuffer (key , strlen ((const char * )key ));
1353+ return Py_HashBuffer (key , ((const struct hashtable_key * )key )-> size );
13401354}
13411355
13421356static int
1343- hashtable_compare_str (const void * key1 , const void * key2 )
1357+ hashtable_compare_key (const void * key1 , const void * key2 )
13441358{
1345- return strcmp ((const char * )key1 , (const char * )key2 ) == 0 ;
1359+ size_t size = ((const struct hashtable_key * )key1 )-> size ;
1360+ if (size != ((const struct hashtable_key * )key2 )-> size ) {
1361+ return 0 ;
1362+ }
1363+ return memcmp (key1 , key2 , size ) == 0 ;
13461364}
13471365
13481366static void
1349- hashtable_destroy_str (void * ptr )
1367+ hashtable_destroy_key (void * ptr )
13501368{
13511369 PyMem_RawFree (ptr );
13521370}
@@ -1386,16 +1404,15 @@ _find_cached_def(PyModuleDef *def)
13861404}
13871405#endif
13881406
1389- #define HTSEP ':'
13901407
13911408static int
13921409_extensions_cache_init (void )
13931410{
13941411 _Py_hashtable_allocator_t alloc = {PyMem_RawMalloc , PyMem_RawFree };
13951412 EXTENSIONS .hashtable = _Py_hashtable_new_full (
1396- hashtable_hash_str ,
1397- hashtable_compare_str ,
1398- hashtable_destroy_str , // key
1413+ hashtable_hash_key ,
1414+ hashtable_compare_key ,
1415+ hashtable_destroy_key , // key
13991416 del_extensions_cache_value , // value
14001417 & alloc
14011418 );
@@ -1413,7 +1430,7 @@ _extensions_cache_find_unlocked(PyObject *path, PyObject *name,
14131430 if (EXTENSIONS .hashtable == NULL ) {
14141431 return NULL ;
14151432 }
1416- void * key = hashtable_key_from_2_strings (path , name , HTSEP );
1433+ void * key = hashtable_key_from_2_strings (path , name );
14171434 if (key == NULL ) {
14181435 return NULL ;
14191436 }
@@ -1423,7 +1440,7 @@ _extensions_cache_find_unlocked(PyObject *path, PyObject *name,
14231440 * p_key = key ;
14241441 }
14251442 else {
1426- hashtable_destroy_str (key );
1443+ hashtable_destroy_key (key );
14271444 }
14281445 return entry ;
14291446}
@@ -1561,7 +1578,7 @@ _extensions_cache_set(PyObject *path, PyObject *name,
15611578finally_oldvalue :
15621579 extensions_lock_release ();
15631580 if (key != NULL ) {
1564- hashtable_destroy_str (key );
1581+ hashtable_destroy_key (key );
15651582 }
15661583
15671584 return value ;
@@ -1605,7 +1622,6 @@ _extensions_cache_clear_all(void)
16051622 EXTENSIONS .hashtable = NULL ;
16061623}
16071624
1608- #undef HTSEP
16091625
16101626
16111627static bool
0 commit comments