File 0392-Fix-out-of-memory-bug-in-the-implementation-of-maps.patch of Package erlang

From 3640c4ed502a93fc11480e48cc817e28dfe831e4 Mon Sep 17 00:00:00 2001
From: Kjell Winblad <kjellwinblad@gmail.com>
Date: Thu, 28 Mar 2019 15:46:04 +0100
Subject: [PATCH] Fix out of memory bug in the implementation of maps

This commit fixes a bug that could cause a crash or memory usage to
grow until the machine ran out of memory when adding a key-value pair
to a map. This could happen when inserting a new key-value pair with a
key K1 containing a binary B1 into a map M having a key K2 with a
binary B2 if the following conditions were met:

* size(B1) >= 4294967296
* size(B2) >= 4294967296
* size(M) >= 32
* (size(B1) rem 4294967296)  == (size(B2) rem 4294967296)
* the first (size(B1) rem 4294967296) bytes are the same both in B1
  and B2
* substituting B1 in K1 with B2 would result in a term with the same
  value as K2

The root cause of the bug is that the map implementation only hashed
the first (X modulo 4294967296) bytes of binaries so that different
binaries could get hashed to the same hash value independently of the
hash seed.
---
 erts/emulator/beam/erl_utils.h   |  1 -
 erts/emulator/beam/utils.c       |  8 ++--
 erts/emulator/test/map_SUITE.erl | 92 ++++++++++++++++++++++++++++++++++++++--
 3 files changed, 93 insertions(+), 8 deletions(-)

diff --git a/erts/emulator/beam/erl_utils.h b/erts/emulator/beam/erl_utils.h
index 880febba8b..430ac305c5 100644
--- a/erts/emulator/beam/erl_utils.h
+++ b/erts/emulator/beam/erl_utils.h
@@ -118,7 +118,6 @@ int erts_fit_in_bits_int32(Sint32);
 int erts_list_length(Eterm);
 int erts_is_builtin(Eterm, Eterm, int);
 Uint32 make_broken_hash(Eterm);
-Uint32 block_hash(byte *, unsigned, Uint32);
 Uint32 make_hash2(Eterm);
 Uint32 make_hash(Eterm);
 Uint32 make_internal_hash(Eterm);
diff --git a/erts/emulator/beam/utils.c b/erts/emulator/beam/utils.c
index 36cfe0548e..0bbae65e28 100644
--- a/erts/emulator/beam/utils.c
+++ b/erts/emulator/beam/utils.c
@@ -1069,11 +1069,11 @@ do {                               \
 
 #define HCONST 0x9e3779b9UL /* the golden ratio; an arbitrary value */
 
-Uint32
-block_hash(byte *k, unsigned length, Uint32 initval)
+static Uint32
+block_hash(byte *k, Uint length, Uint32 initval)
 {
    Uint32 a,b,c;
-   unsigned len;
+   Uint len;
 
    /* Set up the internal state */
    len = length;
@@ -1749,7 +1749,7 @@ make_internal_hash(Eterm term, Uint32 salt)
 	    case SUB_BINARY_SUBTAG:
 	    {
 		byte* bptr;
-		unsigned sz = binary_size(term);
+		Uint sz = binary_size(term);
 		Uint32 con = HCONST_13 + hash;
 		Uint bitoffs;
 		Uint bitsize;
-- 
2.16.4

openSUSE Build Service is sponsored by