summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-03 16:43:29 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-03 16:43:29 +0200
commit061c15513415b932218b35cbf8ddbe4ce6cece34 (patch)
tree116c6be5eb80447cccdcdb93efc8dbd417124016
parentf0c0373ea67d5e42a7553fb27fac3df823e62655 (diff)
downloadpsutil-061c15513415b932218b35cbf8ddbe4ce6cece34.tar.gz
LookupAccountSid() retry only in case of INSUFFICIENT_BUFFER
-rw-r--r--psutil/_psutil_windows.c36
1 files changed, 14 insertions, 22 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 47376499..3836f9e4 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -1331,6 +1331,7 @@ psutil_proc_username(PyObject *self, PyObject *args) {
}
CloseHandle(processHandle);
+ processHandle = NULL;
// Get the user SID.
@@ -1359,28 +1360,12 @@ psutil_proc_username(PyObject *self, PyObject *args) {
}
CloseHandle(tokenHandle);
+ tokenHandle = NULL;
// resolve the SID to a name
nameSize = 0x100;
domainNameSize = 0x100;
-
- name = malloc(nameSize * sizeof(TCHAR));
- if (name == NULL) {
- PyErr_NoMemory();
- goto error;
- }
-
- domainName = malloc(domainNameSize * sizeof(TCHAR));
- if (domainName == NULL) {
- PyErr_NoMemory();
- goto error;
- }
-
- if (!LookupAccountSid(NULL, user->User.Sid, name, &nameSize, domainName,
- &domainNameSize, &nameUse))
- {
- free(name);
- free(domainName);
+ while (1) {
name = malloc(nameSize * sizeof(TCHAR));
if (name == NULL) {
PyErr_NoMemory();
@@ -1394,15 +1379,22 @@ psutil_proc_username(PyObject *self, PyObject *args) {
if (!LookupAccountSid(NULL, user->User.Sid, name, &nameSize,
domainName, &domainNameSize, &nameUse))
{
- PyErr_SetFromWindowsErr(0);
- goto error;
+ if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
+ free(name);
+ free(domainName);
+ continue;
+ }
+ else {
+ PyErr_SetFromWindowsErr(0);
+ goto error;
+ }
}
+ break;
}
+ // build the "domain\\username" username string
nameSize = _tcslen(name);
domainNameSize = _tcslen(domainName);
-
- // build the full username string
fullName = malloc((domainNameSize + 1 + nameSize + 1) * sizeof(TCHAR));
if (fullName == NULL) {
PyErr_NoMemory();