Skip to content

Commit

Permalink
#1075 / win / net_if_addrs(): inet_ntop() return value isn't checked
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed May 16, 2017
1 parent 2e05a17 commit cf57b35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
10 changes: 6 additions & 4 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3064,13 +3064,17 @@ 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);
if (dwRetVal == NO_ERROR) {
in_netmask.s_addr = converted_netmask;
netmaskIntRet = inet_ntop(AF_INET, &in_netmask, netmask_buff,
netmask_bufflen);
if (!netmaskIntRet)
goto error;
}
#endif
}
Expand All @@ -3079,17 +3083,15 @@ 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
pUnicast = pUnicast->Next;
continue;
}

if (intRet == NULL) {
PyErr_SetFromWindowsErr(GetLastError());
goto error;
}
#if PY_MAJOR_VERSION >= 3
py_address = PyUnicode_FromString(buff);
#else
Expand Down
26 changes: 17 additions & 9 deletions psutil/arch/windows/inet_ntop.c
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
}

Expand All @@ -32,6 +39,7 @@ inet_ntop(__in INT Family,
0,
pStringBuf,
(LPDWORD) &StringBufSize) != 0) {
PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
return NULL;
}
return pStringBuf;
Expand Down

0 comments on commit cf57b35

Please sign in to comment.