summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-16 21:32:50 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-16 21:32:50 +0200
commitcf57b3550aa1d0d3c5698f6bb1922ffb60242a7e (patch)
treee3371fcf4440c13f8db8f55ed7bac0ed9338bc5c
parent2e05a1748b67ca4c24289ca72963679284e2344c (diff)
downloadpsutil-cf57b3550aa1d0d3c5698f6bb1922ffb60242a7e.tar.gz
#1075 / win / net_if_addrs(): inet_ntop() return value isn't checked
-rw-r--r--HISTORY.rst1
-rw-r--r--psutil/_psutil_windows.c10
-rw-r--r--psutil/arch/windows/inet_ntop.c26
3 files changed, 24 insertions, 13 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index 080aaa86..8bf147f6 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -59,6 +59,7 @@
- 1069_: [FreeBSD] Process.cpu_num() may return 255 for certain kernel
processes.
- 1074_: [FreeBSD] sensors_battery() raises OSError in case of no battery.
+- 1075_: [Windows] net_if_addrs(): inet_ntop() return value is not checked.
**Porting notes**
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 657ff8b5..b4ba665f 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -3064,6 +3064,8 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
pUnicast->Address.lpSockaddr;
intRet = inet_ntop(AF_INET, &(sa_in->sin_addr), buff,
bufflen);
+ if (!intRet)
+ goto error;
#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above
netmask_bits = pUnicast->OnLinkPrefixLength;
dwRetVal = ConvertLengthToIpv4Mask(netmask_bits, &converted_netmask);
@@ -3071,6 +3073,8 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
in_netmask.s_addr = converted_netmask;
netmaskIntRet = inet_ntop(AF_INET, &in_netmask, netmask_buff,
netmask_bufflen);
+ if (!netmaskIntRet)
+ goto error;
}
#endif
}
@@ -3079,6 +3083,8 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
pUnicast->Address.lpSockaddr;
intRet = inet_ntop(AF_INET6, &(sa_in6->sin6_addr),
buff, bufflen);
+ if (!intRet)
+ goto error;
}
else {
// we should never get here
@@ -3086,10 +3092,6 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
continue;
}
- if (intRet == NULL) {
- PyErr_SetFromWindowsErr(GetLastError());
- goto error;
- }
#if PY_MAJOR_VERSION >= 3
py_address = PyUnicode_FromString(buff);
#else
diff --git a/psutil/arch/windows/inet_ntop.c b/psutil/arch/windows/inet_ntop.c
index 50dfb6ae..4b6c1dfe 100644
--- a/psutil/arch/windows/inet_ntop.c
+++ b/psutil/arch/windows/inet_ntop.c
@@ -1,9 +1,15 @@
+/*
+ * Copyright (c) 2009, Giampaolo Rodola', Jeff Tang. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <Python.h>
#include "inet_ntop.h"
// From: https://memset.wordpress.com/2010/10/09/inet_ntop-for-win32/
-PCSTR
-WSAAPI
-inet_ntop(__in INT Family,
+PCSTR WSAAPI
+inet_ntop(__in INT family,
__in PVOID pAddr,
__out_ecount(StringBufSize) PSTR pStringBuf,
__in size_t StringBufSize) {
@@ -13,17 +19,18 @@ inet_ntop(__in INT Family,
struct sockaddr_in6 *srcaddr6 = (struct sockaddr_in6*) &srcaddr;
memset(&srcaddr, 0, sizeof(struct sockaddr_storage));
- srcaddr.ss_family = Family;
+ srcaddr.ss_family = family;
- if (Family == AF_INET)
- {
+ if (family == AF_INET) {
dwAddressLength = sizeof(struct sockaddr_in);
memcpy(&(srcaddr4->sin_addr), pAddr, sizeof(struct in_addr));
- } else if (Family == AF_INET6)
- {
+ }
+ else if (family == AF_INET6) {
dwAddressLength = sizeof(struct sockaddr_in6);
memcpy(&(srcaddr6->sin6_addr), pAddr, sizeof(struct in6_addr));
- } else {
+ }
+ else {
+ PyErr_SetString(PyExc_ValueError, "invalid family");
return NULL;
}
@@ -32,6 +39,7 @@ inet_ntop(__in INT Family,
0,
pStringBuf,
(LPDWORD) &StringBufSize) != 0) {
+ PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
return NULL;
}
return pStringBuf;