aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Rhodes <trhodes@FreeBSD.org>2003-06-14 13:41:31 +0000
committerTom Rhodes <trhodes@FreeBSD.org>2003-06-14 13:41:31 +0000
commit33ffdd8115baf8594e21e064862d5bcecbee7998 (patch)
tree7393e5d31b83afe0cddea6237f8e73918cbda739
parentf212249acf486a2dd0e3b6cbec52e23b49ca8110 (diff)
Notes
-rw-r--r--usr.bin/compress/compress.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/usr.bin/compress/compress.c b/usr.bin/compress/compress.c
index ceaac87c6bc4..584f283ffb88 100644
--- a/usr.bin/compress/compress.c
+++ b/usr.bin/compress/compress.c
@@ -300,14 +300,9 @@ decompress(const char *in, const char *out, int bits)
isreg = oreg = !exists || S_ISREG(sb.st_mode);
ifp = ofp = NULL;
- if ((ofp = fopen(out, "w")) == NULL) {
- cwarn("%s", out);
- return;
- }
-
if ((ifp = zopen(in, "r", bits)) == NULL) {
cwarn("%s", in);
- goto err;
+ return;
}
if (stat(in, &sb)) {
cwarn("%s", in);
@@ -316,6 +311,22 @@ decompress(const char *in, const char *out, int bits)
if (!S_ISREG(sb.st_mode))
isreg = 0;
+ /*
+ * Try to read the first few uncompressed bytes from the input file
+ * before blindly truncating the output file.
+ */
+ if ((nr = fread(buf, 1, sizeof(buf), ifp)) == 0) {
+ cwarn("%s", in);
+ (void)fclose(ifp);
+ return;
+ }
+ if ((ofp = fopen(out, "w")) == NULL ||
+ (nr != 0 && fwrite(buf, 1, nr, ofp) != nr)) {
+ cwarn("%s", out);
+ (void)fclose(ifp);
+ return;
+ }
+
while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
if (fwrite(buf, 1, nr, ofp) != nr) {
cwarn("%s", out);