Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linux] Process.rlimit() does not handle LONG LONG properly #1760

Merged
merged 3 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ XXXX-XX-XX

- 1726_: [Linux] cpu_freq() parsing should use spaces instead of tabs on ia64.
(patch by Michał Górny)
- 1760_: [Linux] Process.rlimit() does not handle long long type properly.

5.7.0
=====
Expand Down
2 changes: 1 addition & 1 deletion psutil/_psutil_aix.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ psutil_net_if_stats(PyObject* self, PyObject* args) {
if (sock == -1)
goto error;

strncpy(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));

// is up?
ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
Expand Down
5 changes: 5 additions & 0 deletions psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ extern int PSUTIL_DEBUG;
// a signaler for connections without an actual status
static const int PSUTIL_CONN_NONE = 128;

// strncpy() variant which appends a null terminator.
#define PSUTIL_STRNCPY(dst, src, n) \
strncpy(dst, src, n - 1); \
dst[n - 1] = '\0'

// ====================================================================
// --- Backward compatibility with missing Python.h APIs
// ====================================================================
Expand Down
6 changes: 2 additions & 4 deletions psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,17 @@ psutil_linux_prlimit(PyObject *self, PyObject *args) {
ret = prlimit(pid, resource, NULL, &old);
if (ret == -1)
return PyErr_SetFromErrno(PyExc_OSError);
#if defined(PSUTIL_HAVE_LONG_LONG)
if (sizeof(old.rlim_cur) > sizeof(long)) {
return Py_BuildValue("LL",
(PY_LONG_LONG)old.rlim_cur,
(PY_LONG_LONG)old.rlim_max);
}
#endif
return Py_BuildValue("ll", (long)old.rlim_cur, (long)old.rlim_max);
}

// set
else {
#if defined(PSUTIL_HAVE_LARGEFILE_SUPPORT)
#if defined(HAVE_LONG_LONG)
new.rlim_cur = PyLong_AsLongLong(py_soft);
if (new.rlim_cur == (rlim_t) - 1 && PyErr_Occurred())
return NULL;
Expand Down Expand Up @@ -500,7 +498,7 @@ psutil_net_if_duplex_speed(PyObject* self, PyObject* args) {
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
return PyErr_SetFromOSErrnoWithSyscall("socket()");
strncpy(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));

// duplex and speed
memset(&ethcmd, 0, sizeof ethcmd);
Expand Down
8 changes: 4 additions & 4 deletions psutil/_psutil_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,10 @@ psutil_net_if_mtu(PyObject *self, PyObject *args) {
goto error;

#ifdef PSUTIL_SUNOS10
strncpy(lifr.lifr_name, nic_name, sizeof(lifr.lifr_name));
PSUTIL_STRNCPY(lifr.lifr_name, nic_name, sizeof(lifr.lifr_name));
ret = ioctl(sock, SIOCGIFMTU, &lifr);
#else
strncpy(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));
ret = ioctl(sock, SIOCGIFMTU, &ifr);
#endif
if (ret == -1)
Expand Down Expand Up @@ -398,7 +398,7 @@ psutil_net_if_flags(PyObject *self, PyObject *args) {
if (sock == -1)
goto error;

strncpy(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));
ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
if (ret == -1)
goto error;
Expand Down Expand Up @@ -578,7 +578,7 @@ psutil_net_if_duplex_speed(PyObject *self, PyObject *args) {
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
return PyErr_SetFromErrno(PyExc_OSError);
strncpy(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name));

// speed / duplex
memset(&ifmed, 0, sizeof(struct ifmediareq));
Expand Down
4 changes: 2 additions & 2 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ psutil_net_io_counters(PyObject *self, PyObject *args) {
goto next;

// check if this is a network interface by sending a ioctl
strncpy(ifr.lifr_name, ksp->ks_name, sizeof(ifr.lifr_name));
PSUTIL_STRNCPY(ifr.lifr_name, ksp->ks_name, sizeof(ifr.lifr_name));
ret = ioctl(sock, SIOCGLIFFLAGS, &ifr);
if (ret == -1)
goto next;
Expand Down Expand Up @@ -1515,7 +1515,7 @@ psutil_net_if_stats(PyObject* self, PyObject* args) {
if (strcmp(ksp->ks_class, "net") != 0)
continue;

strncpy(ifr.lifr_name, ksp->ks_name, sizeof(ifr.lifr_name));
PSUTIL_STRNCPY(ifr.lifr_name, ksp->ks_name, sizeof(ifr.lifr_name));
ret = ioctl(sock, SIOCGLIFFLAGS, &ifr);
if (ret == -1)
continue; // not a network interface
Expand Down