Skip to content

crash in deflateInit2 function #582

@dynasty1215

Description

@dynasty1215

when i used deflateInit2 to compress , crash occur random.
Crash analysis in my app online
0 libsystem_platform.dylib 0x000000019f5230dc 0x19f51b000 + 32988 _bzero (in libsystem_platform.dylib) + 28
1 libz.1.dylib 0x00000001d4e9360c 0x1d4e92000 + 5644 deflateInit2
(in libz.1.dylib) + 404
2 xxxxCollectKit 0x00000001085135f4 -[NSData(xxxxAddition) gzipDeflate] + 439796 (xxxxAdditions.m:411)

“gzipDeflate” is a addtions method for NSData below:

  • (NSData *)gzipDeflate {
    if ([self length] == 0) return self;

    z_stream strm;

    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.total_out = 0;
    strm.next_in=(Bytef *)[self bytes];
    strm.avail_in = (uInt)[self length];

    if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil;

    NSMutableData *compressed = [NSMutableData dataWithLength:16384]; // 16K chunks for expansion

    do {

      if (strm.total_out >= [compressed length])
          [compressed increaseLengthBy: 16384];
      
      strm.next_out = [compressed mutableBytes] + strm.total_out;
      strm.avail_out = (uInt)([compressed length] - strm.total_out);
      
      deflate(&strm, Z_FINISH);
    

    } while (strm.avail_out == 0);

    deflateEnd(&strm);

    [compressed setLength: strm.total_out];
    return [NSData dataWithData:compressed];
    }

It looks like this article “compress2 crash
some one gvie suggestion. Thx.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions