@@ -36,6 +36,7 @@ typedef struct sHashEntry {
36
36
*/
37
37
static const unsigned int TableSize = 2039 ; /* prime */
38
38
static hashEntry * * HashTable = NULL ;
39
+ static unsigned int MaxEntryLen = 0 ;
39
40
40
41
/*
41
42
* FUNCTION DEFINITIONS
@@ -70,7 +71,8 @@ static hashEntry *getHashTableEntry (unsigned long hashedValue)
70
71
return entry ;
71
72
}
72
73
73
- static unsigned int hashValue (const char * const string , langType language )
74
+ static unsigned int hashValue (const char * const string , langType language ,
75
+ unsigned int maxLen , bool * maxLenReached )
74
76
{
75
77
const signed char * p ;
76
78
unsigned int h = 5381 ;
@@ -79,11 +81,19 @@ static unsigned int hashValue (const char *const string, langType language)
79
81
80
82
/* "djb" hash as used in g_str_hash() in glib */
81
83
for (p = (const signed char * )string ; * p != '\0' ; p ++ )
84
+ {
82
85
h = (h << 5 ) + h + tolower (* p );
86
+ if (p - (const signed char * )string > maxLen )
87
+ {
88
+ * maxLenReached = true;
89
+ return 0 ;
90
+ }
91
+ }
83
92
84
93
/* consider language as an extra "character" and add it to the hash */
85
94
h = (h << 5 ) + h + language ;
86
95
96
+ * maxLenReached = false;
87
97
return h ;
88
98
}
89
99
@@ -107,8 +117,13 @@ static hashEntry *newEntry (
107
117
*/
108
118
extern void addKeyword (const char * const string , langType language , int value )
109
119
{
110
- const unsigned int index = hashValue (string , language ) % TableSize ;
120
+ bool dummy ;
121
+ const unsigned int index = hashValue (string , language , 1000 , & dummy ) % TableSize ;
111
122
hashEntry * entry = getHashTableEntry (index );
123
+ size_t len = strlen (string );
124
+
125
+ if (len > MaxEntryLen )
126
+ MaxEntryLen = len ;
112
127
113
128
if (entry == NULL )
114
129
{
@@ -139,10 +154,16 @@ extern void addKeyword (const char *const string, langType language, int value)
139
154
140
155
static int lookupKeywordFull (const char * const string , bool caseSensitive , langType language )
141
156
{
142
- const unsigned int index = hashValue (string , language ) % TableSize ;
143
- hashEntry * entry = getHashTableEntry (index );
157
+ bool maxLenReached ;
158
+ const unsigned int index = hashValue (string , language , MaxEntryLen , & maxLenReached ) % TableSize ;
159
+ hashEntry * entry ;
144
160
int result = KEYWORD_NONE ;
145
161
162
+ if (maxLenReached )
163
+ return KEYWORD_NONE ;
164
+
165
+ entry = getHashTableEntry (index );
166
+
146
167
while (entry != NULL )
147
168
{
148
169
if (language == entry -> language &&
0 commit comments