mirror of
https://github.com/json-c/json-c.git
synced 2026-03-28 09:29:06 +08:00
Fix CVE-2020-12762.
This commit is a squashed and slightly modified backport of the following commits on the master branch: *77d935b*d07b910*519dfe1*a59d5ac
This commit is contained in:
12
linkhash.c
12
linkhash.c
@@ -10,6 +10,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
@@ -62,6 +63,8 @@ struct lh_table* lh_table_new(int size, const char *name,
|
||||
int i;
|
||||
struct lh_table *t;
|
||||
|
||||
/* Allocate space for elements to avoid divisions by zero. */
|
||||
assert(size > 0);
|
||||
t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
|
||||
if(!t) lh_abort("lh_table_new: calloc failed\n");
|
||||
t->count = 0;
|
||||
@@ -126,7 +129,14 @@ int lh_table_insert(struct lh_table *t, void *k, const void *v)
|
||||
unsigned long h, n;
|
||||
|
||||
t->inserts++;
|
||||
if(t->count >= t->size * LH_LOAD_FACTOR) lh_table_resize(t, t->size * 2);
|
||||
if (t->count >= t->size * LH_LOAD_FACTOR) {
|
||||
/* Avoid signed integer overflow with large tables. */
|
||||
int new_size = (t->size > INT_MAX / 2) ? INT_MAX : (t->size * 2);
|
||||
if (t->size == INT_MAX)
|
||||
return -1;
|
||||
|
||||
lh_table_resize(t, new_size);
|
||||
}
|
||||
|
||||
h = t->hash_fn(k);
|
||||
n = h % t->size;
|
||||
|
||||
Reference in New Issue
Block a user