Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-3]: src/usr.bin/gzip Pull up following revision(s) (requested by ...



details:   https://anonhg.NetBSD.org/src/rev/8ccfe2e33853
branches:  netbsd-3
changeset: 577070:8ccfe2e33853
user:      tron <tron%NetBSD.org@localhost>
date:      Wed Aug 31 10:34:39 2005 +0000

description:
Pull up following revision(s) (requested by mrg in ticket #721):
        usr.bin/gzip/gzip.c: revision 1.73
avoid an infinite loop while decompressing invalid gzip files.
some minor CSE.  compare stat return value consistently.
thanks to tron for testing the first change.

diffstat:

 usr.bin/gzip/gzip.c |  73 +++++++++++++++++++++++++++++++---------------------
 1 files changed, 43 insertions(+), 30 deletions(-)

diffs (210 lines):

diff -r 9c8f0e959523 -r 8ccfe2e33853 usr.bin/gzip/gzip.c
--- a/usr.bin/gzip/gzip.c       Tue Aug 30 05:56:05 2005 +0000
+++ b/usr.bin/gzip/gzip.c       Wed Aug 31 10:34:39 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gzip.c,v 1.71.2.1 2005/06/15 06:06:03 snj Exp $        */
+/*     $NetBSD: gzip.c,v 1.71.2.2 2005/08/31 10:34:39 tron Exp $       */
 
 /*
  * Copyright (c) 1997, 1998, 2003, 2004 Matthew R. Green
@@ -32,7 +32,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004 Matthew R. Green\n\
      All rights reserved.\n");
-__RCSID("$NetBSD: gzip.c,v 1.71.2.1 2005/06/15 06:06:03 snj Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.71.2.2 2005/08/31 10:34:39 tron Exp $");
 #endif /* not lint */
 
 /*
@@ -637,6 +637,8 @@
                 (int)(in_tot >> 24) & 0xff);
        if (i != 8)
                maybe_err("snprintf");
+       if (in_tot > 0xffffffff)
+               maybe_warn("input file size >= 4GB cannot be saved");
        if (write(out, outbufp, i) != i) {
                maybe_warn("write");
                in_tot = -1;
@@ -730,8 +732,7 @@
                                        print_test(filename, 0);
 #endif
                                maybe_warn("failed to read stdin");
-                               out_tot = -1;
-                               goto stop;
+                               goto stop_and_fail;
                        } else if (in_size == 0) {
                                done_reading = 1;
                        }
@@ -751,8 +752,7 @@
                case GZSTATE_MAGIC0:
                        if (*z.next_in != GZIP_MAGIC0) {
                                maybe_warnx("input not gziped (MAGIC0)");
-                               out_tot = -1;
-                               goto stop;
+                               goto stop_and_fail;
                        }
                        ADVANCE();
                        state++;
@@ -764,8 +764,7 @@
                        if (*z.next_in != GZIP_MAGIC1 &&
                            *z.next_in != GZIP_OMAGIC1) {
                                maybe_warnx("input not gziped (MAGIC1)");
-                               out_tot = -1;
-                               goto stop;
+                               goto stop_and_fail;
                        }
                        ADVANCE();
                        state++;
@@ -774,8 +773,7 @@
                case GZSTATE_METHOD:
                        if (*z.next_in != Z_DEFLATED) {
                                maybe_warnx("unknown compression method");
-                               out_tot = -1;
-                               goto stop;
+                               goto stop_and_fail;
                        }
                        ADVANCE();
                        state++;
@@ -859,18 +857,37 @@
                case GZSTATE_INIT:
                        if (inflateInit2(&z, -MAX_WBITS) != Z_OK) {
                                maybe_warnx("failed to inflateInit");
-                               out_tot = -1;
-                               goto stop;
+                               goto stop_and_fail;
                        }
                        state++;
                        break;
 
                case GZSTATE_READ:
                        error = inflate(&z, Z_FINISH);
+                       switch (error) {
                        /* Z_BUF_ERROR goes with Z_FINISH... */
-                       if (error != Z_STREAM_END && error != Z_BUF_ERROR)
-                               /* Just need more input */
+                       case Z_BUF_ERROR:
+                       case Z_STREAM_END:
+                       case Z_OK:
                                break;
+
+                       case Z_NEED_DICT:
+                               maybe_warnx("Z_NEED_DICT error");
+                               goto stop_and_fail;
+                       case Z_DATA_ERROR:
+                               maybe_warnx("data stream error");
+                               goto stop_and_fail;
+                       case Z_STREAM_ERROR:
+                               maybe_warnx("internal stream error");
+                               goto stop_and_fail;
+                       case Z_MEM_ERROR:
+                               maybe_warnx("memory allocation error");
+                               goto stop_and_fail;
+
+                       default:
+                               maybe_warn("unknown error from inflate(): %d",
+                                   error);
+                       }
                        wr = BUFLEN - z.avail_out;
 
                        if (wr != 0) {
@@ -882,8 +899,7 @@
 #endif
                                    write(out, outbufp, wr) != wr) {
                                        maybe_warn("error writing to output");
-                                       out_tot = -1;
-                                       goto stop;
+                                       goto stop_and_fail;
                                }
 
                                out_tot += wr;
@@ -909,8 +925,7 @@
                                                continue;
                                        }
                                        maybe_warnx("truncated input");
-                                       out_tot = -1;
-                                       goto stop;
+                                       goto stop_and_fail;
                                }
                                origcrc = ((unsigned)z.next_in[0] & 0xff) |
                                        ((unsigned)z.next_in[1] & 0xff) << 8 |
@@ -919,8 +934,7 @@
                                if (origcrc != crc) {
                                        maybe_warnx("invalid compressed"
                                             " data--crc error");
-                                       out_tot = -1;
-                                       goto stop;
+                                       goto stop_and_fail;
                                }
                        }
 
@@ -942,8 +956,7 @@
                                                continue;
                                        }
                                        maybe_warnx("truncated input");
-                                       out_tot = -1;
-                                       goto stop;
+                                       goto stop_and_fail;
                                }
                                origlen = ((unsigned)z.next_in[0] & 0xff) |
                                        ((unsigned)z.next_in[1] & 0xff) << 8 |
@@ -953,8 +966,7 @@
                                if (origlen != out_sub_tot) {
                                        maybe_warnx("invalid compressed"
                                             " data--length error");
-                                       out_tot = -1;
-                                       goto stop;
+                                       goto stop_and_fail;
                                }
                        }
                                
@@ -963,13 +975,14 @@
 
                        if (error < 0) {
                                maybe_warnx("decompression error");
-                               out_tot = -1;
-                               goto stop;
+                               goto stop_and_fail;
                        }
                        state = GZSTATE_MAGIC0;
                        break;
                }
                continue;
+stop_and_fail:
+               out_tot = 1;
 stop:
                break;
        }
@@ -1206,7 +1219,7 @@
                maybe_warn("couldn't close ouput");
 
 #ifndef SMALL
-       if (stat(outfile, &osb) < 0) {
+       if (stat(outfile, &osb) != 0) {
                maybe_warn("couldn't stat: %s", outfile);
                goto bad_outfile;
        }
@@ -1445,13 +1458,13 @@
        /*
         * if we can't stat the file don't remove the file.
         */
-       if (stat(outfile, &osb) < 0) {
+       if (stat(outfile, &osb) != 0) {
                maybe_warn("couldn't stat (leaving original): %s",
                           outfile);
                return -1;
        }
        if (osb.st_size != size) {
-               maybe_warn("stat gave different size: %" PRIdOFF
+               maybe_warnx("stat gave different size: %" PRIdOFF
                                " != %" PRIdOFF " (leaving original)",
                                size, osb.st_size);
                unlink(outfile);
@@ -1650,7 +1663,7 @@
        }
 
 retry:
-       if (stat(path, &sb) < 0) {
+       if (stat(path, &sb) != 0) {
                /* lets try <path>.gz if we're decompressing */
                if (dflag && s == NULL && errno == ENOENT) {
                        len = strlen(path);



Home | Main Index | Thread Index | Old Index