From 064e65ede4c6fe0cc35887c4593de99d0243b7b0 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 26 Aug 2015 03:03:01 -0700 Subject: [PATCH] #655: fix net_if_stats unicode error --- HISTORY.rst | 4 +--- psutil/_psutil_windows.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index def4bb3eb..1d1755948 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -12,11 +12,9 @@ Bug tracker at https://github.com/giampaolo/psutil/issues - #513: [Linux] fixed integer overflow for RLIM_INFINITY. - #641: [Windows] fixed many compilation warnings. (patch by Jeff Tang) +- #655: [Windows] net_if_stats unicode error in in case of non-ASCII NIC names. - #659: [Linux] compilation error on Suse 10. - #664: [Linux] compilation error on Alpine Linux. (patch by Bart van Kleef) - -**Bug fixes** - - #670: [Windows] segfgault of net_if_addrs() in case of non-ASCII NIC names. (patch by sk6249) - #672: [Windows] compilation fails if using Windows SDK v8.0. (patch by diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 504f8cccb..07345f0fc 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -3004,10 +3004,10 @@ psutil_net_if_stats(PyObject *self, PyObject *args) { MIB_IFROW *pIfRow; PIP_ADAPTER_ADDRESSES pAddresses = NULL; PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; - char friendly_name[MAX_PATH]; char descr[MAX_PATH]; int ifname_found; + PyObject *py_nic_name = NULL; PyObject *py_retdict = PyDict_New(); PyObject *py_ifc_info = NULL; PyObject *py_is_up = NULL; @@ -3052,7 +3052,11 @@ psutil_net_if_stats(PyObject *self, PyObject *args) { while (pCurrAddresses) { sprintf_s(descr, MAX_PATH, "%wS", pCurrAddresses->Description); if (lstrcmp(descr, pIfRow->bDescr) == 0) { - sprintf_s(friendly_name, MAX_PATH, "%wS", pCurrAddresses->FriendlyName); + py_nic_name = PyUnicode_FromWideChar( + pCurrAddresses->FriendlyName, + wcslen(pCurrAddresses->FriendlyName)); + if (py_nic_name == NULL) + goto error; ifname_found = 1; break; } @@ -3085,8 +3089,9 @@ psutil_net_if_stats(PyObject *self, PyObject *args) { ); if (!py_ifc_info) goto error; - if (PyDict_SetItemString(py_retdict, friendly_name, py_ifc_info)) + if (PyDict_SetItemString(py_retdict, py_nic_name, py_ifc_info)) goto error; + Py_DECREF(py_nic_name); Py_DECREF(py_ifc_info); } @@ -3097,6 +3102,7 @@ psutil_net_if_stats(PyObject *self, PyObject *args) { error: Py_XDECREF(py_is_up); Py_XDECREF(py_ifc_info); + Py_XDECREF(py_nic_name); Py_DECREF(py_retdict); if (pIfTable != NULL) free(pIfTable);