better checks for strto*l errors
authorRobert Swiecki <robert@swiecki.net>
Fri, 25 May 2018 21:53:11 +0000 (23:53 +0200)
committerRobert Swiecki <robert@swiecki.net>
Fri, 25 May 2018 21:53:11 +0000 (23:53 +0200)
cmdline.cc
contain.cc

index 33b55910e46b5c7712e8a1dcf350f42c0b39ace3..0872606a634661100f03ee3de0334036ed28ff04 100644 (file)
@@ -288,6 +288,7 @@ uint64_t parseRLimit(int res, const char* optarg, unsigned long mul) {
                    "provided)",
                    res, optarg);
        }
+       errno = 0;
        uint64_t val = strtoull(optarg, NULL, 0) * mul;
        if (val == ULLONG_MAX && errno != 0) {
                PLOG_F("strtoul('%s', 0)", optarg);
index c4beeeda885ab4ebb6ebbc6c3478cb2321757bf7..ea33b42da82ec0f5d073d5b601720cab381e4187 100644 (file)
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
+#include <limits.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <stddef.h>
@@ -218,28 +219,29 @@ static bool containMakeFdsCOEProc(nsjconf_t* nsjconf) {
                if (strcmp("..", entry->d_name) == 0) {
                        continue;
                }
-               int fd = strtoul(entry->d_name, NULL, 10);
-               if (errno == EINVAL) {
-                       LOG_W("Cannot convert /proc/self/fd/%s to a number", entry->d_name);
+               errno = 0;
+               long fd = strtol(entry->d_name, NULL, 10);
+               if (fd == LONG_MAX && errno != 0) {
+                       PLOG_W("Cannot convert /proc/self/fd/%s to a number", entry->d_name);
                        continue;
                }
                int flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
                if (flags == -1) {
-                       PLOG_D("fcntl(fd, F_GETFD, 0)");
+                       PLOG_D("fcntl(fd=%ld, F_GETFD, 0)", fd);
                        closedir(dir);
                        return false;
                }
                if (containPassFd(nsjconf, fd)) {
-                       LOG_D("FD=%d will be passed to the child process", fd);
+                       LOG_D("FD=%ld will be passed to the child process", fd);
                        if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, flags & ~(FD_CLOEXEC))) == -1) {
-                               PLOG_E("Could not clear FD_CLOEXEC for FD=%d", fd);
+                               PLOG_E("Could not clear FD_CLOEXEC for FD=%ld", fd);
                                closedir(dir);
                                return false;
                        }
                } else {
-                       LOG_D("FD=%d will be closed before execve()", fd);
+                       LOG_D("FD=%ld will be closed before execve()", fd);
                        if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, flags | FD_CLOEXEC)) == -1) {
-                               PLOG_E("Could not set FD_CLOEXEC for FD=%d", fd);
+                               PLOG_E("Could not set FD_CLOEXEC for FD=%ld", fd);
                                closedir(dir);
                                return false;
                        }