libjson-c: backport security fixes
[openwrt/openwrt.git] / package / libs / libjson-c / patches / 001-Prevent-division-by-zero-in-linkhash.patch
1 From 77d935b7ae7871a1940cd827e850e6063044ec45 Mon Sep 17 00:00:00 2001
2 From: Tobias Stoeckmann <tobias@stoeckmann.org>
3 Date: Mon, 4 May 2020 19:46:45 +0200
4 Subject: [PATCH 2/2] Prevent division by zero in linkhash.
5
6 If a linkhash with a size of zero is created, then modulo operations
7 are prone to division by zero operations.
8
9 Purely protective measure against bad usage.
10 ---
11 linkhash.c | 3 +++
12 1 file changed, 3 insertions(+)
13
14 --- a/linkhash.c
15 +++ b/linkhash.c
16 @@ -10,6 +10,7 @@
17 *
18 */
19
20 +#include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 @@ -431,6 +432,8 @@ struct lh_table* lh_table_new(int size,
25 int i;
26 struct lh_table *t;
27
28 + /* Allocate space for elements to avoid divisions by zero. */
29 + assert(size > 0);
30 t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
31 if(!t) lh_abort("lh_table_new: calloc failed\n");
32 t->count = 0;