better checks for strto*l errors

This commit is contained in:
Robert Swiecki 2018-05-25 23:53:11 +02:00
parent 5b7cfc7f00
commit 9ed90812c0
2 changed files with 11 additions and 8 deletions

View File

@ -288,6 +288,7 @@ uint64_t parseRLimit(int res, const char* optarg, unsigned long mul) {
"provided)", "provided)",
res, optarg); res, optarg);
} }
errno = 0;
uint64_t val = strtoull(optarg, NULL, 0) * mul; uint64_t val = strtoull(optarg, NULL, 0) * mul;
if (val == ULLONG_MAX && errno != 0) { if (val == ULLONG_MAX && errno != 0) {
PLOG_F("strtoul('%s', 0)", optarg); PLOG_F("strtoul('%s', 0)", optarg);

View File

@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <limits.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@ -218,28 +219,29 @@ static bool containMakeFdsCOEProc(nsjconf_t* nsjconf) {
if (strcmp("..", entry->d_name) == 0) { if (strcmp("..", entry->d_name) == 0) {
continue; continue;
} }
int fd = strtoul(entry->d_name, NULL, 10); errno = 0;
if (errno == EINVAL) { long fd = strtol(entry->d_name, NULL, 10);
LOG_W("Cannot convert /proc/self/fd/%s to a number", entry->d_name); if (fd == LONG_MAX && errno != 0) {
PLOG_W("Cannot convert /proc/self/fd/%s to a number", entry->d_name);
continue; continue;
} }
int flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0)); int flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
if (flags == -1) { if (flags == -1) {
PLOG_D("fcntl(fd, F_GETFD, 0)"); PLOG_D("fcntl(fd=%ld, F_GETFD, 0)", fd);
closedir(dir); closedir(dir);
return false; return false;
} }
if (containPassFd(nsjconf, fd)) { 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) { 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); closedir(dir);
return false; return false;
} }
} else { } 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) { 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); closedir(dir);
return false; return false;
} }