Преглед изворни кода

dpkg-query: Be more strict when parsing the COLUMNS environment variable

Use strtol() instead of atoi() which does not make it possible to check
for many error conditions.
Guillem Jover пре 10 година
родитељ
комит
3d258742df
2 измењених фајлова са 9 додато и 3 уклоњено
  1. 1 0
      debian/changelog
  2. 8 3
      src/querycmd.c

+ 1 - 0
debian/changelog

@@ -49,6 +49,7 @@ dpkg (1.18.5) UNRELEASED; urgency=medium
   * Rewrite the trigger deferred file parser from flex to manual. The format
   * Rewrite the trigger deferred file parser from flex to manual. The format
     is very simple, and a simple hand-written parser is smaller and avoids a
     is very simple, and a simple hand-written parser is smaller and avoids a
     build dependency.
     build dependency.
+  * Be more strict when parsing the COLUMNS environment variable in dpkg-query.
   * Portability:
   * Portability:
     - Move DPKG_ADMINDIR environment variable name out from update-alternatives
     - Move DPKG_ADMINDIR environment variable name out from update-alternatives
       code, to make life easier for non-dpkg-based systems.
       code, to make life easier for non-dpkg-based systems.

+ 8 - 3
src/querycmd.c

@@ -32,6 +32,8 @@
 #if HAVE_LOCALE_H
 #if HAVE_LOCALE_H
 #include <locale.h>
 #include <locale.h>
 #endif
 #endif
+#include <errno.h>
+#include <limits.h>
 #include <string.h>
 #include <string.h>
 #include <fcntl.h>
 #include <fcntl.h>
 #include <dirent.h>
 #include <dirent.h>
@@ -63,14 +65,17 @@ static int opt_loadavail = 0;
 
 
 static int getwidth(void) {
 static int getwidth(void) {
   int fd;
   int fd;
-  int res;
+  long res;
   struct winsize ws;
   struct winsize ws;
   const char *columns;
   const char *columns;
+  char *endptr;
 
 
   columns = getenv("COLUMNS");
   columns = getenv("COLUMNS");
   if (columns) {
   if (columns) {
-    res = atoi(columns);
-    if (res > 0)
+    errno = 0;
+    res = strtol(columns, &endptr, 10);
+    if (errno != 0 && columns != endptr && *endptr == '\0' &&
+        res > 0 && res < INT_MAX)
       return res;
       return res;
   }
   }