bunzip2 when exec()-ed from perl script outputs garbage data.

From: Thomas Covello (pmjcovello_at_shaw.ca)
Date: 02/02/04

Date: Mon, 02 Feb 2004 00:14:48 GMT

When the following perl script is executed:

#!/usr/bin/env perl
use strict;
use diagnostics;
use warnings;

# Header bytes for different zip formats
my $GZIP_HEADER = "\x1f\x8b\x08\x08";
my $BZIP_HEADER = "BZh9"; # 42 5a 68 39
my $header_bytes;
read STDIN, $header_bytes, 4 or die "Trouble reading input: $!";
if ($header_bytes eq $GZIP_HEADER) {
        exec "gunzip -f";
        die "gunzip doesn't exist or can't be accessed: $!";
} elsif ($header_bytes eq $BZIP_HEADER) {
        exec "bunzip2 -f";
        die "bunzip2 doesn't exist or can't be accessed: $!";
} else { die "Not a proper zip file or unsupported zip format." }

I get output like this:
BZh91AY (blah blah blah.... I can't copy it because it contains NULs)

I've used print statements to prove that it executed bzip2 and found the
correct magic number.