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

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
     is very simple, and a simple hand-written parser is smaller and avoids a
     build dependency.
+  * Be more strict when parsing the COLUMNS environment variable in dpkg-query.
   * Portability:
     - Move DPKG_ADMINDIR environment variable name out from update-alternatives
       code, to make life easier for non-dpkg-based systems.

+ 8 - 3
src/querycmd.c

@@ -32,6 +32,8 @@
 #if HAVE_LOCALE_H
 #include <locale.h>
 #endif
+#include <errno.h>
+#include <limits.h>
 #include <string.h>
 #include <fcntl.h>
 #include <dirent.h>
@@ -63,14 +65,17 @@ static int opt_loadavail = 0;
 
 static int getwidth(void) {
   int fd;
-  int res;
+  long res;
   struct winsize ws;
   const char *columns;
+  char *endptr;
 
   columns = getenv("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;
   }