summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-03 17:02:12 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-03 17:02:12 +0200
commit5580428550896c66a9972682be0997035382d468 (patch)
treec6293ccb92419ade04aeda25f7512e290d2bff66
parent061c15513415b932218b35cbf8ddbe4ce6cece34 (diff)
downloadpsutil-5580428550896c66a9972682be0997035382d468.tar.gz
have GetTokenInformation() retry only in case of INSUFFICIENT_BUFFER
-rw-r--r--psutil/_psutil_windows.c23
1 files changed, 10 insertions, 13 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 3836f9e4..69d1fdfd 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -1334,18 +1334,8 @@ psutil_proc_username(PyObject *self, PyObject *args) {
processHandle = NULL;
// Get the user SID.
-
bufferSize = 0x100;
- user = malloc(bufferSize);
- if (user == NULL) {
- PyErr_NoMemory();
- goto error;
- }
-
- if (!GetTokenInformation(tokenHandle, TokenUser, user, bufferSize,
- &bufferSize))
- {
- free(user);
+ while (1) {
user = malloc(bufferSize);
if (user == NULL) {
PyErr_NoMemory();
@@ -1354,9 +1344,16 @@ psutil_proc_username(PyObject *self, PyObject *args) {
if (!GetTokenInformation(tokenHandle, TokenUser, user, bufferSize,
&bufferSize))
{
- PyErr_SetFromWindowsErr(0);
- goto error;
+ if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
+ free(user);
+ continue;
+ }
+ else {
+ PyErr_SetFromWindowsErr(0);
+ goto error;
+ }
}
+ break;
}
CloseHandle(tokenHandle);