@@ -42,6 +42,7 @@ typedef struct TableElement{
4242# define LOAD 0.9
4343# define SCAN 16
4444
45+ const static size_t UCS4_SIZE = sizeof (Py_UCS4 );
4546
4647typedef enum KeysArrayType {
4748 KAT_LIST = 0 , // must be falsy
@@ -791,7 +792,8 @@ lookup_hash_unicode(
791792
792793 PyArrayObject * a = (PyArrayObject * )self -> keys ;
793794 // REVIEW: is this a new descr reference?
794- Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / sizeof (Py_UCS4 );
795+ Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / UCS4_SIZE ;
796+ Py_ssize_t cmp_bytes = Py_MIN (key_size , dt_size ) * UCS4_SIZE ;
795797
796798 Py_hash_t h = 0 ;
797799 Py_UCS4 * p_start = NULL ;
@@ -808,7 +810,7 @@ lookup_hash_unicode(
808810 }
809811 p_start = (Py_UCS4 * )PyArray_GETPTR1 (a , table [table_pos ].keys_pos );
810812 // memcmp returns 0 on match
811- if (!memcmp (p_start , key , Py_MIN ( key_size , dt_size ) )) {
813+ if (!memcmp (p_start , key , cmp_bytes )) {
812814 return table_pos ;
813815 }
814816 table_pos ++ ;
@@ -833,6 +835,7 @@ lookup_hash_string(
833835
834836 PyArrayObject * a = (PyArrayObject * )self -> keys ;
835837 Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / sizeof (char );
838+ Py_ssize_t cmp_bytes = Py_MIN (key_size , dt_size );
836839
837840 Py_hash_t h = 0 ;
838841 char * p_start = NULL ;
@@ -849,7 +852,7 @@ lookup_hash_string(
849852 }
850853 p_start = (char * )PyArray_GETPTR1 (a , table [table_pos ].keys_pos );
851854 // memcmp returns 0 on match
852- if (!memcmp (p_start , key , Py_MIN ( key_size , dt_size ) )) {
855+ if (!memcmp (p_start , key , cmp_bytes )) {
853856 return table_pos ;
854857 }
855858 table_pos ++ ;
@@ -1110,7 +1113,7 @@ lookup_unicode(FAMObject *self, PyObject* key) {
11101113 return -1 ;
11111114 }
11121115 PyArrayObject * a = (PyArrayObject * )self -> keys ;
1113- Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / sizeof ( Py_UCS4 ) ;
1116+ Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / UCS4_SIZE ;
11141117 // if the key_size is greater than the dtype size of the array, we know there cannot be a match
11151118 Py_ssize_t k_size = PyUnicode_GetLength (key );
11161119 if (k_size > dt_size ) {
@@ -1435,8 +1438,8 @@ copy_to_new(PyTypeObject *cls, FAMObject *self, FAMObject *new)
14351438 new -> key_buffer = NULL ;
14361439 if (new -> keys_array_type == KAT_UNICODE ) {
14371440 PyArrayObject * a = (PyArrayObject * )new -> keys ;
1438- Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / sizeof ( Py_UCS4 ) ;
1439- new -> key_buffer = (Py_UCS4 * )PyMem_Malloc ((dt_size + 1 ) * sizeof ( Py_UCS4 ) );
1441+ Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / UCS4_SIZE ;
1442+ new -> key_buffer = (Py_UCS4 * )PyMem_Malloc ((dt_size + 1 ) * UCS4_SIZE );
14401443 }
14411444
14421445 Py_ssize_t table_size_alloc = new -> table_size + SCAN - 1 ;
@@ -1922,8 +1925,8 @@ fam_init(PyObject *self, PyObject *args, PyObject *kwargs)
19221925 break ;
19231926 case KAT_UNICODE : {
19241927 // Over allocate buffer by 1 so there is room for null at end. This buffer is only used in lookup();
1925- Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / sizeof ( Py_UCS4 ) ;
1926- fam -> key_buffer = (Py_UCS4 * )PyMem_Malloc ((dt_size + 1 ) * sizeof ( Py_UCS4 ) );
1928+ Py_ssize_t dt_size = PyArray_DESCR (a )-> elsize / UCS4_SIZE ;
1929+ fam -> key_buffer = (Py_UCS4 * )PyMem_Malloc ((dt_size + 1 ) * UCS4_SIZE );
19271930 INSERT_FLEXIBLE (Py_UCS4 , insert_unicode , ucs4_get_end_p );
19281931 break ;
19291932 }
0 commit comments