Base temporary output buffer on maximum mallocable, not maxram

This commit is contained in:
Con Kolivas 2016-06-09 10:51:55 +10:00
parent 21496ded07
commit c7a111bd32

20
lrzip.c
View file

@ -492,13 +492,25 @@ bool read_tmpinfile(rzip_control *control, int fd_in)
* a pseudo-temporary file */
static bool open_tmpoutbuf(rzip_control *control)
{
i64 maxlen = control->maxram;
void *buf;
while (42) {
round_to_page(&maxlen);
buf = malloc(maxlen);
if (buf) {
print_maxverbose("Malloced %"PRId64" for tmp_outbuf\n", maxlen);
break;
}
maxlen = maxlen / 3 * 2;
if (maxlen < 100000000)
fatal_return(("Unable to even malloc 100MB for tmp_outbuf\n"), false);
}
control->flags |= FLAG_TMP_OUTBUF;
control->out_maxlen = control->maxram;
/* Allocate slightly more so we can cope when the buffer overflows and
* fall back to a real temporary file */
control->tmp_outbuf = malloc(control->maxram + control->page_size);
if (unlikely(!control->tmp_outbuf))
fatal_return(("Failed to malloc tmp_outbuf in open_tmpoutbuf\n"), false);
control->out_maxlen = maxlen - control->page_size;
control->tmp_outbuf = buf;
if (!DECOMPRESS && !TEST_ONLY)
control->out_ofs = control->out_len = MAGIC_LEN;\
return true;