Sfoglia il codice sorgente

libdpkg: Handle compression level as an integer

Change compress_filter() to take an int for compress_level, and add
a new function in dpkg-deb to parse and validate the argument option
to not accept bogus values.
Guillem Jover 16 anni fa
parent
commit
feb248df15
6 ha cambiato i file con 32 aggiunte e 14 eliminazioni
  1. 1 0
      debian/changelog
  2. 2 2
      dpkg-deb/build.c
  3. 1 1
      dpkg-deb/dpkg-deb.h
  4. 18 2
      dpkg-deb/main.c
  5. 9 8
      lib/dpkg/compress.c
  6. 1 1
      lib/dpkg/compress.h

+ 1 - 0
debian/changelog

@@ -56,6 +56,7 @@ dpkg (1.15.6) UNRELEASED; urgency=low
   * Fix misspellings of “explicitly” all over the place.
   * Normalize ar member names when reading (removing trailing spaces and
     slash), this allows deb-split packages be created with GNU ar.
+  * Validate compression level on dpkg-deb argument parsing.
 
   [ Modestas Vainius ]
   * Implement symbol patterns (Closes: #563752). From now on, it is possible to

+ 2 - 2
dpkg-deb/build.c

@@ -432,7 +432,7 @@ void do_build(const char *const *argv) {
   c2 = subproc_fork();
   if (!c2) {
     m_dup2(p1[0],0); m_dup2(gzfd,1); close(p1[0]); close(gzfd);
-    compress_filter(compress_type_gzip, 0, 1, "9", _("control"));
+    compress_filter(compress_type_gzip, 0, 1, 9, _("control"));
   }
   close(p1[0]);
   subproc_wait_check(c2, "gzip -9c", 0);
@@ -498,7 +498,7 @@ void do_build(const char *const *argv) {
     close(p1[1]);
     m_dup2(p2[0],0); close(p2[0]);
     m_dup2(oldformatflag ? fileno(ar) : gzfd,1);
-    compress_filter(compress_type, 0, 1, compression, _("data"));
+    compress_filter(compress_type, 0, 1, compress_level, _("data"));
   }
   close(p2[0]);
   /* All the pipes are set, now lets run find, and start feeding

+ 1 - 1
dpkg-deb/dpkg-deb.h

@@ -33,9 +33,9 @@ extern dofunction *action;
 void extracthalf(const char *debar, const char *directory,
                  const char *taroption, int admininfo);
 
-extern const char *compression;
 extern const char* showformat;
 extern enum compress_type compress_type;
+extern int compress_level;
 
 #define ARCHIVEVERSION		"2.0"
 

+ 18 - 2
dpkg-deb/main.c

@@ -134,14 +134,30 @@ const char printforhelp[]=
      "Type dpkg --help for help about installing and deinstalling packages.");
 
 int debugflag=0, nocheckflag=0, oldformatflag=BUILDOLDPKGFORMAT;
-const char* compression=NULL;
 enum compress_type compress_type = compress_type_gzip;
+int compress_level = -1;
 const struct cmdinfo *cipaction = NULL;
 dofunction *action = NULL;
 
 static void setaction(const struct cmdinfo *cip, const char *value);
 static void setcompresstype(const struct cmdinfo *cip, const char *value);
 
+static void
+set_compress_level(const struct cmdinfo *cip, const char *value)
+{
+  long level;
+  char *end;
+
+  level = strtol(value, &end, 0);
+  if (value == end || *end || level > INT_MAX)
+    badusage(_("invalid integer for -%c: '%.250s'"), cip->oshort, value);
+
+  if (level < 0 || level > 9)
+    badusage(_("invalid compression level for -%c: %ld'"), cip->oshort, level);
+
+  compress_level = level;
+}
+
 static dofunction *const dofunctions[]= {
   do_build,
   do_contents,
@@ -171,7 +187,7 @@ static const struct cmdinfo cmdinfos[]= {
   { "old",           0,   0, &oldformatflag, NULL,         NULL,          1 },
   { "debug",         'D', 0, &debugflag,     NULL,         NULL,          1 },
   { "nocheck",       0,   0, &nocheckflag,   NULL,         NULL,          1 },
-  { "compression",   'z', 1, NULL,           &compression, NULL,          1 },
+  { "compression",   'z', 1, NULL,           NULL,         set_compress_level },
   { "compress_type", 'Z', 1, NULL,           NULL,         setcompresstype  },
   { "showformat",    0,   1, NULL,           &showformat,  NULL             },
   { "help",          'h', 0, NULL,           NULL,         usage            },

+ 9 - 8
lib/dpkg/compress.c

@@ -153,7 +153,7 @@ decompress_filter(enum compress_type type, int fd_in, int fd_out,
 
 void
 compress_filter(enum compress_type type, int fd_in, int fd_out,
-                const char *compression, const char *desc, ...)
+                int compress_level, const char *desc, ...)
 {
   va_list al;
   struct varbuf v = VARBUF_INIT;
@@ -163,8 +163,9 @@ compress_filter(enum compress_type type, int fd_in, int fd_out,
   varbufvprintf(&v, desc, al);
   va_end(al);
 
-  if(compression == NULL) compression= "9";
-  else if (*compression == '0')
+  if (compress_level < 0)
+    compress_level = 9;
+  else if (compress_level == 0)
     type = compress_type_none;
 
   switch(type) {
@@ -174,7 +175,7 @@ compress_filter(enum compress_type type, int fd_in, int fd_out,
         char buffer[4096];
         gzFile gzfile;
 
-        snprintf(combuf, sizeof(combuf), "w%c", *compression);
+        snprintf(combuf, sizeof(combuf), "w%d", compress_level);
         gzfile = gzdopen(fd_out, combuf);
 
         for (;;) {
@@ -199,7 +200,7 @@ compress_filter(enum compress_type type, int fd_in, int fd_out,
         exit(0);
       }
 #else
-      snprintf(combuf, sizeof(combuf), "-c%c", *compression);
+      snprintf(combuf, sizeof(combuf), "-c%d", compress_level);
       fd_fd_filter(fd_in, fd_out, v.buf, GZIP, combuf, NULL);
 #endif
     case compress_type_bzip2:
@@ -208,7 +209,7 @@ compress_filter(enum compress_type type, int fd_in, int fd_out,
         char buffer[4096];
         BZFILE *bzfile;
 
-        snprintf(combuf, sizeof(combuf), "w%c", *compression);
+        snprintf(combuf, sizeof(combuf), "w%d", compress_level);
         bzfile = BZ2_bzdopen(fd_out, combuf);
 
         for (;;) {
@@ -233,11 +234,11 @@ compress_filter(enum compress_type type, int fd_in, int fd_out,
         exit(0);
       }
 #else
-      snprintf(combuf, sizeof(combuf), "-c%c", *compression);
+      snprintf(combuf, sizeof(combuf), "-c%d", compress_level);
       fd_fd_filter(fd_in, fd_out, v.buf, BZIP2, combuf, NULL);
 #endif
     case compress_type_lzma:
-      snprintf(combuf, sizeof(combuf), "-c%c", *compression);
+      snprintf(combuf, sizeof(combuf), "-c%d", compress_level);
       fd_fd_filter(fd_in, fd_out, v.buf, LZMA, combuf, NULL);
     case compress_type_none:
       fd_fd_copy(fd_in, fd_out, -1, _("%s: compression"), v.buf);

+ 1 - 1
lib/dpkg/compress.h

@@ -41,7 +41,7 @@ void decompress_filter(enum compress_type type, int fd_in, int fd_out,
                        const char *desc, ...) DPKG_ATTR_NORET
                        DPKG_ATTR_PRINTF(4);
 void compress_filter(enum compress_type type, int fd_in, int fd_out,
-                     const char *compression, const char *desc, ...)
+                     int compress_level, const char *desc, ...)
                      DPKG_ATTR_NORET DPKG_ATTR_PRINTF(5);
 
 DPKG_END_DECLS