Browse Source

Prevent division by zero in linkhash.

If a linkhash with a size of zero is created, then modulo operations
are prone to division by zero operations.

Purely protective measure against bad usage.
tags/json-c-0.15-20200726
Tobias Stoeckmann 5 years ago
parent
commit
77d935b7ae
1 changed files with 3 additions and 0 deletions
  1. +3
    -0
      linkhash.c

+ 3
- 0
linkhash.c View File

@@ -12,6 +12,7 @@


#include "config.h" #include "config.h"


#include <assert.h>
#include <limits.h> #include <limits.h>
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
@@ -499,6 +500,8 @@ struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *h
int i; int i;
struct lh_table *t; 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)); t = (struct lh_table *)calloc(1, sizeof(struct lh_table));
if (!t) if (!t)
return NULL; return NULL;


Loading…
Cancel
Save