fixing memoryleak by introducing a nonehandle

In CreateProcess the non functional thread
handle was leaked.
This commit is contained in:
Martin Haimberger 2014-12-01 23:53:58 -08:00
parent a26c89851a
commit 128458d380
4 changed files with 120 additions and 10 deletions

View File

@ -2,6 +2,7 @@
# libwinpr-handle cmake build script # libwinpr-handle cmake build script
# #
# Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> # Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
# Copyright 2014 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -15,7 +16,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
winpr_module_add(handle.c handle.h) winpr_module_add(handle.c handle.h nonehandle.c nonehandle.h)
if(${CMAKE_SYSTEM_NAME} MATCHES SunOS) if(${CMAKE_SYSTEM_NAME} MATCHES SunOS)
winpr_library_add(rt) winpr_library_add(rt)

View File

@ -0,0 +1,73 @@
/**
* WinPR: Windows Portable Runtime
* NoneHandle a.k.a. brathandle should be used where a handle is needed, but
* functionality is not implemented yet or not implementable.
*
* Copyright 2014 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "nonehandle.h"
#include <pthread.h>
#ifndef _WIN32
static HANDLE_CLOSE_CB _NoneHandleCloseCb;
static pthread_once_t none_initialized = PTHREAD_ONCE_INIT;
static BOOL NoneHandleCloseHandle(HANDLE handle)
{
WINPR_NONE_HANDLE* none = (WINPR_NONE_HANDLE*) handle;
free(none);
return TRUE;
}
static BOOL NoneHandleIsHandle(HANDLE handle)
{
WINPR_NONE_HANDLE* none = (WINPR_NONE_HANDLE*) handle;
if (!none || none->Type != HANDLE_TYPE_NONE)
{
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
return TRUE;
}
static void NoneHandleInitialize(void)
{
_NoneHandleCloseCb.IsHandled = NoneHandleIsHandle;
_NoneHandleCloseCb.CloseHandle = NoneHandleCloseHandle;
RegisterHandleCloseCb(&_NoneHandleCloseCb);
}
HANDLE CreateNoneHandle()
{
WINPR_NONE_HANDLE* none;
none = (WINPR_NONE_HANDLE*) calloc(1, sizeof(WINPR_NONE_HANDLE));
if (!none)
return NULL;
pthread_once(&none_initialized, NoneHandleInitialize);
return (HANDLE)none;
}
#endif

View File

@ -0,0 +1,40 @@
/**
* WinPR: Windows Portable Runtime
* NoneHandle a.k.a. brathandle should be used where a handle is needed, but
* functionality is not implemented yet or not implementable.
*
* Copyright 2014 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef WINPR_NONE_HANDLE_PRIVATE_H
#define WINPR_NONE_HANDLE_PRIVATE_H
#ifndef _WIN32
#include <winpr/handle.h>
#include "handle.h"
struct winpr_none_handle
{
WINPR_HANDLE_DEF();
};
typedef struct winpr_none_handle WINPR_NONE_HANDLE;
HANDLE CreateNoneHandle();
#endif /*_WIN32*/
#endif /* WINPR_NONE_HANDLE_PRIVATE_H */

View File

@ -3,6 +3,7 @@
* Process Thread Functions * Process Thread Functions
* *
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2014 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +23,7 @@
#endif #endif
#include <winpr/handle.h> #include <winpr/handle.h>
#include "../handle/nonehandle.h"
#include <winpr/thread.h> #include <winpr/thread.h>
#include <fcntl.h> #include <fcntl.h>
@ -183,7 +185,7 @@ BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags,
LPSTR* pArgs = NULL; LPSTR* pArgs = NULL;
char** envp = NULL; char** envp = NULL;
char* filename = NULL; char* filename = NULL;
WINPR_THREAD* thread; HANDLE thread;
WINPR_PROCESS* process; WINPR_PROCESS* process;
WINPR_ACCESS_TOKEN* token; WINPR_ACCESS_TOKEN* token;
LPTCH lpszEnvironmentBlock; LPTCH lpszEnvironmentBlock;
@ -288,21 +290,15 @@ BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags,
process->status = 0; process->status = 0;
process->dwExitCode = 0; process->dwExitCode = 0;
thread = (WINPR_THREAD*) malloc(sizeof(WINPR_THREAD)); thread = CreateNoneHandle();
ZeroMemory(thread, sizeof(WINPR_THREAD));
if (!thread) if (!thread)
{ {
goto finish; goto finish;
} }
WINPR_HANDLE_SET_TYPE(thread, HANDLE_TYPE_THREAD);
thread->mainProcess = TRUE;
lpProcessInformation->hProcess = (HANDLE) process; lpProcessInformation->hProcess = (HANDLE) process;
lpProcessInformation->hThread = (HANDLE) thread; lpProcessInformation->hThread = thread;
lpProcessInformation->dwProcessId = (DWORD) pid; lpProcessInformation->dwProcessId = (DWORD) pid;
lpProcessInformation->dwThreadId = (DWORD) pid; lpProcessInformation->dwThreadId = (DWORD) pid;