Skip to content

Commit 195a9a9

Browse files
committed
Fix potential integer overflow vulnerability when allocating memory
1 parent 1b90661 commit 195a9a9

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

libdispatch/nchashmap.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,16 @@ NC_hashmapnew(size_t startsize)
173173
hm = (NC_hashmap*)malloc(sizeof(NC_hashmap));
174174

175175
if(startsize == 0 || startsize < MINTABLESIZE)
176-
startsize = MINTABLESIZE;
176+
startsize = MINTABLESIZE;
177177
else {
178-
startsize *= 4;
179-
startsize /= 3;
180-
startsize = findPrimeGreaterThan(startsize);
181-
if(startsize == 0) {nullfree(hm); return 0;}
178+
if(startsize > MAX_SIZE / 4){nullfree(hm);return 0;}
179+
startsize *= 4;
180+
startsize /= 3;
181+
startsize = findPrimeGreaterThan(startsize);
182+
if(startsize == 0) {nullfree(hm); return 0;}
182183
}
183184
hm->table = (NC_hentry*)calloc(sizeof(NC_hentry), (size_t)startsize);
184-
if(hm->table == NULL) {
185-
nullfree(hm);
186-
return 0;
187-
}
185+
if(hm->table == NULL) {nullfree(hm);return 0;}
188186
hm->alloc = startsize;
189187
hm->active = 0;
190188
return hm;

0 commit comments

Comments
 (0)