1

like title, here is my code:

int decompress_one_file(char *infilename, char *outfilename)
{
    gzFile infile = gzopen(infilename, "rb");
    FILE *outfile = fopen(outfilename, "wb");
    if (!infile || !outfile) return -1;

    char buffer[128] = {NULL};
    int num_read = 0;
    num_read = gzread(infile, buffer, sizeof(buffer)); // crash here
    while (num_read > 0) {
        fwrite(buffer, 1, num_read, outfile);
    }

    gzclose(infile);
    fclose(outfile);
    return 0;
}

when my console-app run to gzread(), its crash, I don't know what type of error is this?. zlib version: 1.2.11

update compress function:

int compress_one_file(char *infilename, char *outfilename)
{
    FILE *infile = fopen(infilename, "rb");
    gzFile outfile = gzopen(outfilename, "wb");
    if (!infile || !outfile) return -1;

    char inbuffer[128] = {NULL};
    int num_read = 0;
    unsigned long total_read = 0, total_wrote = 0;
    while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) {
        total_read += num_read;
        gzwrite(outfile, inbuffer, num_read);
    }
    fclose(infile);
    gzclose(outfile);
    return 0;
}

Error that shows up:

error windows

Anyone got an idea about this error?

yasuo
  • 11
  • 4
  • What did your debugger tell you? With Visual Studio there is no excuse not to use the debugger. – Jabberwocky May 15 '17 at 08:20
  • And [this](http://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) could give you a hint. Read the error message carefully. message – Jabberwocky May 15 '17 at 08:24
  • 1
    what you mean? I run it in debug-win32 mode, when step-over "that line", app crash. What the infos do you need? – yasuo May 15 '17 at 08:26
  • The address `0xbaadf00d` should raise a flag. – Jabberwocky May 15 '17 at 08:27
  • okay, i'll check it. – yasuo May 15 '17 at 08:30
  • Was the reason for this ever discovered? I'm having the precisely the same problem, gzread crashes (memory access violation) for 32-bit builds. (64-bit builds work fine.) – MiloDC Feb 11 '22 at 00:52
  • I found that the reason for this (at least on my machine) was a bug in the contributed (and unsupported) assembly code that ships with zlib: https://github.com/madler/zlib/issues/223 . The proposed fix is here: https://github.com/madler/zlib/pull/264/commits/62b93a862c4b2bd00bb3b18fd2afd90f59dcf040 . – MiloDC Jan 11 '23 at 00:08

1 Answers1

0

I checked and it works fine at my workstation. May be here is a issue with gzipped file? Could you show a file that give a crash?

But in any case you have a infinity loop of fwrite:

int num_read = 0;
while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0) {
    while (num_read > 0) {
        num_read -= fwrite(buffer, 1, num_read, outfile);
    }   
}
knst
  • 523
  • 2
  • 16
  • Do you build project with Optimization or with Debug options? If you build with optimization real place of crash could be in another place and real reason could be related with problem in fwrite() or in gzclose(). I checked and generated gzip file ungzipped correctly with tools "gunzip" – knst May 15 '17 at 10:04