Bugfix: Allocate temporary space on the heap rather than the stack, and use the actual transaction data size for the buffer

This commit is contained in:
Luke Dashjr
2016-01-26 00:36:01 +00:00
parent 2763aba114
commit 24aabb0e55
2 changed files with 27 additions and 4 deletions

View File

@@ -47,6 +47,8 @@ bool _blkmk_dblsha256(void *hash, const void *data, size_t datasz) {
#define dblsha256 _blkmk_dblsha256
#define max_varint_size (9)
uint64_t blkmk_init_generation3(blktemplate_t * const tmpl, const void * const script, const size_t scriptsz, bool * const inout_newcb) {
if (tmpl->cbtxn && !(*inout_newcb && (tmpl->mutations & BMM_GENERATE)))
{
@@ -482,29 +484,48 @@ char varintEncode(unsigned char *out, uint64_t n) {
char *blkmk_assemble_submission_(blktemplate_t * const tmpl, const unsigned char * const data, const unsigned int dataid, blknonce_t nonce, const bool foreign)
{
unsigned char blk[80 + 8 + 1000000];
const bool incl_gentxn = (foreign || (!(tmpl->mutations & BMAb_TRUNCATE && !dataid)));
const bool incl_alltxn = (foreign || !(tmpl->mutations & BMAb_COINBASE));
size_t blkbuf_sz = libblkmaker_blkheader_size;
if (incl_gentxn) {
blkbuf_sz += max_varint_size + tmpl->cbtxn->datasz;
if (incl_alltxn) {
blkbuf_sz += tmpl->txns_datasz;
}
}
unsigned char * const blk = malloc(blkbuf_sz);
if (!blk) {
return NULL;
}
memcpy(blk, data, 76);
nonce = htonl(nonce);
memcpy(&blk[76], &nonce, 4);
size_t offs = 80;
if (foreign || (!(tmpl->mutations & BMAb_TRUNCATE && !dataid)))
{
if (incl_gentxn) {
offs += varintEncode(&blk[offs], 1 + tmpl->txncount);
if (!_blkmk_extranonce(tmpl, &blk[offs], dataid, &offs))
{
free(blk);
return NULL;
}
if (foreign || !(tmpl->mutations & BMAb_COINBASE))
if (incl_alltxn) {
for (unsigned long i = 0; i < tmpl->txncount; ++i)
{
memcpy(&blk[offs], tmpl->txns[i].data, tmpl->txns[i].datasz);
offs += tmpl->txns[i].datasz;
}
}
}
char *blkhex = malloc((offs * 2) + 1);
_blkmk_bin2hex(blkhex, blk, offs);
free(blk);
return blkhex;
}

View File

@@ -24,6 +24,8 @@ typedef uint32_t blktime_t;
typedef int16_t blktime_diff_t;
typedef uint32_t blknonce_t;
#define libblkmaker_blkheader_size (80)
struct blktxn_t {
unsigned char *data;
size_t datasz;