Reputation: 49
I have some pre-compressed data (compressed with the help of zlib-flate on Linux) inside my RAM. To use this compressed data I want to umcompress it using zlib an inflate.
I have no dynamic memory management on this system but provided a big enough buffer for the uncompressed data. The problem is if I call the inflate routine after calling the inflateInit routine I get an unhandled exception.
But if I call the inflateInit function two times the following inflate (=decompressing) works fine and I get the correct decompressed data into my provided buffer. This is strange isn't it?
I can also do a compression at any time before calling the inflate and it will also work .. what the hell?
Let me show you the behaviour:
new run..
new run..
There is an array somewhere holding the compressed data:
uint8_t src [] = {.....};
This is my buffer which is definetly big enough to contain the complete decompressed data.
#define BUF_SIZE 1000
uint8_t buf[BUF_SIZE];
And this is the code of my decompressing:
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = srcLen;
strm.next_in = src;
strm.avail_out = BUF_SIZE;
strm.next_out = buf;
strm.data_type = Z_BINARY;
inflateInit(&strm);
inflateInit(&strm); // the follwing inflate only works with this second init
inflate(&strm, Z_NO_FLUSH);
I can see that the state
member of the stream is changing from 0x40193678
after the first init to 0x40195250
after the second init (maybe this is a important info for you). And both inits are response with Z_OK
.
And now I hope you can help me..
Upvotes: 0
Views: 1163
Reputation: 112502
What it's doing is allocating memory for the stream twice, using only the second allocation. I can only guess that you are overwriting just the memory allocated by the first inflateInit()
, due to some other error in your program. The overwriting crashes inflate()
when it is trying to use the first allocation, but succeeds on when using the second allocation, which is not overwritten by the other bug.
Upvotes: 0