Skip to content

Instantly share code, notes, and snippets.

/C Secret

Created April 24, 2013 11:27
Show Gist options
  • Save anonymous/2c5a88ca9ac6f4c2a064 to your computer and use it in GitHub Desktop.
Save anonymous/2c5a88ca9ac6f4c2a064 to your computer and use it in GitHub Desktop.
The bug happens using an ASM optimized static zlib on Windows x86. Supposed is to have no output, but currently it outs "%Cë". Non ASM versions on Windows do that right, as well both ASM and non ASM builds on Linux. The bad data can be fetched from http://188.40.74.4/corrupted.gz .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <zlib.h>
#define CHUNK 0x4000
/* Fetch the bad file here http://188.40.74.4/corrupted.gz */
int main ()
{
const char * file_name = "corrupted.gz";
FILE * file;
z_stream strm = {0};
unsigned char in[CHUNK];
unsigned char out[CHUNK];
int status;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = in;
strm.avail_in = 0;
strm.next_out = out;
status = inflateInit2(&strm, -15);
if (0 > status) {
fprintf(stderr, "inflateInit2(): %s\n", zError(status));
return 3;
}
/* Open the file. */
file = fopen(file_name, "rb");
if (!file) {
fprintf(stderr, "fopen(): %s\n", strerror(errno));
return 3;
}
while (1) {
int bytes_read;
bytes_read = fread(in, sizeof(char), sizeof(in), file);
if (ferror(file)){
fprintf(stderr, "fread(): %s\n", strerror(errno));
return 3;
}
strm.avail_in = bytes_read;
do {
strm.avail_out = CHUNK;
status = inflate(& strm, Z_SYNC_FLUSH);
if (0 > status) {
inflateEnd(&strm);
fprintf(stderr, "inflate(): %s\n", zError(status));
return 1;
}
printf("%s", out);
}
while (strm.avail_out == 0);
if (feof(file)) {
inflateEnd(&strm);
break;
}
}
if (fclose(file)) {
fprintf(stderr, "fclose(): %s\n", strerror(errno));
return 3;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment