tree-hash: allocate variable memory on heap, not stack

Large amounts might run out of stack

Reported by guidov
pull/221/head
moneromooo-monero 5 years ago committed by wowario
parent ddafd99cac
commit 8ad9d0f618
No known key found for this signature in database
GPG Key ID: 24DCBE762DE9C111

@ -30,6 +30,7 @@
#include <assert.h> #include <assert.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "hash-ops.h" #include "hash-ops.h"
@ -82,23 +83,24 @@ void tree_hash(const char (*hashes)[HASH_SIZE], size_t count, char *root_hash) {
size_t cnt = tree_hash_cnt( count ); size_t cnt = tree_hash_cnt( count );
char ints[cnt][HASH_SIZE]; char *ints = calloc(cnt, HASH_SIZE); // zero out as extra protection for using uninitialized mem
memset(ints, 0 , sizeof(ints)); // zero out as extra protection for using uninitialized mem assert(ints);
memcpy(ints, hashes, (2 * cnt - count) * HASH_SIZE); memcpy(ints, hashes, (2 * cnt - count) * HASH_SIZE);
for (i = 2 * cnt - count, j = 2 * cnt - count; j < cnt; i += 2, ++j) { for (i = 2 * cnt - count, j = 2 * cnt - count; j < cnt; i += 2, ++j) {
cn_fast_hash(hashes[i], 64, ints[j]); cn_fast_hash(hashes[i], 64, ints + j * HASH_SIZE);
} }
assert(i == count); assert(i == count);
while (cnt > 2) { while (cnt > 2) {
cnt >>= 1; cnt >>= 1;
for (i = 0, j = 0; j < cnt; i += 2, ++j) { for (i = 0, j = 0; j < cnt; i += 2, ++j) {
cn_fast_hash(ints[i], 64, ints[j]); cn_fast_hash(ints + i * HASH_SIZE, 64, ints + j * HASH_SIZE);
} }
} }
cn_fast_hash(ints[0], 64, root_hash); cn_fast_hash(ints, 64, root_hash);
free(ints);
} }
} }

Loading…
Cancel
Save