[cmake-developers] [PATCH] Avoid bad alloc for large files

Domen Vrankar domen.vrankar at gmail.com
Mon Dec 22 14:01:31 EST 2014


>> I received a bad alloc when uploading a large file with CTest. The patch
>> below resolved this.
>
> Your patch is line-wrapped and can't be applied. However, I did this by hand.
> This is basically the same, but it avoids the needless floating point
> arithmetic. Does it work for you?

snip

>  std::string cmCTest::Base64EncodeFile(std::string file)

snip

> -        static_cast<double>(len) * 1.5 + 5.0) ];
> +    = new unsigned char [ (len * 3) / 2 + 5 ];

snip

I came across a similar issue a few days ago in our code base (bad
alloc when compressing a large file in-memory with 256 MB data ulimit
per process) and I used a different formula to calculate the maximum
buffer size:

http://stackoverflow.com/questions/1533113/calculate-the-size-to-a-base-64-encoded-message

I used a bit modified answer from "kanaka".

size_t output_size = ((len - 1) / 3) * 4 + 4;
size_t final_size = output_size + (output_size / 64) * 2; // 64
instead of 76 since RFC 3548 and RFC 4648 allow CLRF line breaks every
64 characters

This formula would give less memory allocation overhead.

Regards,
Domen


More information about the cmake-developers mailing list