Skip to content

Commit cbd89b7

Browse files
committed
hash_table: simplify logic to free heap allocated key
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent 9755cda commit cbd89b7

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/flb_hash_table.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828

2929
#include <cfl/cfl.h>
3030

31-
static inline char *convert_string_to_lowercase(
32-
char *output_buffer,
33-
char *input_buffer,
34-
size_t length)
31+
static inline char *convert_string_to_lowercase(char *output_buffer, char *input_buffer, size_t length)
3532
{
3633
size_t index;
3734

35+
if (input_buffer == NULL) {
36+
return NULL;
37+
}
38+
3839
if (output_buffer == NULL) {
3940
output_buffer = flb_calloc(1, length + 1);
4041
if (output_buffer == NULL) {
@@ -47,23 +48,21 @@ static inline char *convert_string_to_lowercase(
4748
for (index = 0 ; index < length ; index++) {
4849
output_buffer[index] = tolower(input_buffer[index]);
4950
}
50-
5151
}
5252

5353
return output_buffer;
5454
}
5555

56-
static inline int flb_hash_table_compute_key_hash(
57-
uint64_t *hash,
58-
char *key, size_t key_len,
59-
int case_sensitivity)
56+
static inline int flb_hash_table_compute_key_hash(uint64_t *hash, char *key, size_t key_len, int case_sensitivity)
6057
{
58+
int converted_key_allocated = FLB_FALSE;
6159
char local_caseless_key_buffer[64];
6260
char *converted_key = key;
6361

6462
if (!case_sensitivity) {
65-
if (key_len > (sizeof(local_caseless_key_buffer) - 1)) {
63+
if (key_len >= (sizeof(local_caseless_key_buffer) - 1)) {
6664
converted_key = convert_string_to_lowercase(NULL, key, key_len);
65+
converted_key_allocated = FLB_TRUE;
6766
}
6867
else {
6968
converted_key = convert_string_to_lowercase(local_caseless_key_buffer,
@@ -76,8 +75,7 @@ static inline int flb_hash_table_compute_key_hash(
7675
}
7776

7877
*hash = cfl_hash_64bits(converted_key, key_len);
79-
80-
if (!case_sensitivity && converted_key != key) {
78+
if (converted_key_allocated) {
8179
flb_free(converted_key);
8280
}
8381

@@ -447,7 +445,6 @@ int flb_hash_table_add(struct flb_hash_table *ht, const char *key, int key_len,
447445
/*
448446
* Below is just code to handle the creation of a new entry in the table
449447
*/
450-
451448
ret = flb_hash_table_compute_key_hash(
452449
&hash,
453450
(char *) key, key_len,

0 commit comments

Comments
 (0)