Skip to content

Commit 728d3c3

Browse files
author
V Narayanan
committed
Bug#43572 Handle failures from hash_init
Failure to allocate memory for the hash->array element, caused hash_init to return without initializing the other members of the hash. Thus although the dynamic array buffer may be allocated at a later point in the code, the incompletely initialized hash caused fatal failures. This patch moves the initialization of the other members of the hash above the array allocation, so that the usage of this hash will not result in fatal failures.
1 parent 2b48caa commit 728d3c3

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

include/hash.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void hash_replace(HASH *hash, HASH_SEARCH_STATE *state, byte *new_row);
6363
my_bool hash_check(HASH *hash); /* Only in debug library */
6464

6565
#define hash_clear(H) bzero((char*) (H),sizeof(*(H)))
66-
#define hash_inited(H) ((H)->array.buffer != 0)
66+
#define hash_inited(H) ((H)->blength != 0)
6767
#define hash_init_opt(A,B,C,D,E,F,G,H) \
6868
(!hash_inited(A) && _hash_init(A,B,C,D,E,F,G, H CALLER_INFO))
6969

mysys/hash.c

+29-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ static uint calc_hash(const HASH *hash, const byte *key, uint length)
4545
return nr1;
4646
}
4747

48+
/**
49+
@brief Initialize the hash
50+
51+
@details
52+
53+
Initialize the hash, by defining and giving valid values for
54+
its elements. The failure to allocate memory for the
55+
hash->array element will not result in a fatal failure. The
56+
dynamic array that is part of the hash will allocate memory
57+
as required during insertion.
58+
59+
@param[in,out] hash The hash that is initialized
60+
@param[in] charset The charater set information
61+
@param[in] size The hash size
62+
@param[in] key_offest The key offset for the hash
63+
@param[in] key_length The length of the key used in
64+
the hash
65+
@param[in] get_key get the key for the hash
66+
@param[in] free_element pointer to the function that
67+
does cleanup
68+
@param[in] CALLER_INFO_PROTO flag that define the behaviour
69+
of the hash
70+
@return inidicates success or failure of initialization
71+
@retval 0 success
72+
@retval 1 failure
73+
*/
4874
my_bool
4975
_hash_init(HASH *hash,CHARSET_INFO *charset,
5076
uint size,uint key_offset,uint key_length,
@@ -55,19 +81,15 @@ _hash_init(HASH *hash,CHARSET_INFO *charset,
5581
DBUG_PRINT("enter",("hash: 0x%lx size: %d", (long) hash, size));
5682

5783
hash->records=0;
58-
if (my_init_dynamic_array_ci(&hash->array,sizeof(HASH_LINK),size,0))
59-
{
60-
hash->free=0; /* Allow call to hash_free */
61-
DBUG_RETURN(1);
62-
}
6384
hash->key_offset=key_offset;
6485
hash->key_length=key_length;
6586
hash->blength=1;
6687
hash->get_key=get_key;
6788
hash->free=free_element;
6889
hash->flags=flags;
6990
hash->charset=charset;
70-
DBUG_RETURN(0);
91+
DBUG_RETURN(my_init_dynamic_array_ci(&hash->array,
92+
sizeof(HASH_LINK), size, 0));
7193
}
7294

7395

@@ -113,6 +135,7 @@ void hash_free(HASH *hash)
113135
hash_free_elements(hash);
114136
hash->free= 0;
115137
delete_dynamic(&hash->array);
138+
hash->blength= 0;
116139
DBUG_VOID_RETURN;
117140
}
118141

0 commit comments

Comments
 (0)