Allow less than maxram to be malloced for checksum to fix Failed to malloc ckbuf in hash_search2

This commit is contained in:
Con Kolivas 2016-06-09 11:04:40 +10:00
parent c7a111bd32
commit 70bd5e9d3a

33
rzip.c
View file

@ -50,6 +50,7 @@
#ifdef HAVE_ARPA_INET_H #ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h> # include <arpa/inet.h>
#endif #endif
#include <inttypes.h>
#include "md5.h" #include "md5.h"
#include "stream.h" #include "stream.h"
@ -729,23 +730,35 @@ static inline void hash_search(rzip_control *control, struct rzip_state *st,
put_literal(control, st, st->last_match, st->chunk_size); put_literal(control, st, st->last_match, st->chunk_size);
if (st->chunk_size > cksum_limit) { if (st->chunk_size > cksum_limit) {
i64 cksum_len = control->maxram;
void *buf;
while (42) {
round_to_page(&cksum_len);
buf = malloc(cksum_len);
if (buf) {
print_maxverbose("Malloced %"PRId64" for checksum ckbuf\n", cksum_len);
break;
}
cksum_len = cksum_len / 3 * 2;
if (cksum_len < control->page_size)
failure("Failed to malloc any ram for checksum ckbuf\n");
}
control->checksum.buf = buf;
/* Compute checksum. If the entire chunk is longer than maxram, /* Compute checksum. If the entire chunk is longer than maxram,
* do it "per-partes" */ * do it "per-partes" */
cksem_wait(control, &control->cksumsem); cksem_wait(control, &control->cksumsem);
control->checksum.len = st->chunk_size - cksum_limit; control->checksum.len = st->chunk_size - cksum_limit;
cksum_chunks = control->checksum.len / control->maxram; cksum_chunks = control->checksum.len / cksum_len;
cksum_remains = control->checksum.len % control->maxram; cksum_remains = control->checksum.len % cksum_len;
control->checksum.buf = malloc(control->maxram);
if (unlikely(!control->checksum.buf))
failure("Failed to malloc ckbuf in hash_search2\n");
for (i = 0; i < cksum_chunks; i++) { for (i = 0; i < cksum_chunks; i++) {
control->do_mcpy(control, control->checksum.buf, cksum_limit, control->maxram); control->do_mcpy(control, control->checksum.buf, cksum_limit, cksum_len);
cksum_limit += control->maxram; cksum_limit += cksum_len;
st->cksum = CrcUpdate(st->cksum, control->checksum.buf, control->maxram); st->cksum = CrcUpdate(st->cksum, control->checksum.buf, cksum_len);
if (!NO_MD5) if (!NO_MD5)
md5_process_bytes(control->checksum.buf, control->maxram, &control->ctx); md5_process_bytes(control->checksum.buf, cksum_len, &control->ctx);
} }
/* Process end of the checksum buffer */ /* Process end of the checksum buffer */
control->do_mcpy(control, control->checksum.buf, cksum_limit, cksum_remains); control->do_mcpy(control, control->checksum.buf, cksum_limit, cksum_remains);