fixing memoryleak by introducing a nonehandle
In CreateProcess the non functional thread handle was leaked.
This commit is contained in:
parent
a26c89851a
commit
128458d380
@ -2,6 +2,7 @@
|
||||
# libwinpr-handle cmake build script
|
||||
#
|
||||
# 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");
|
||||
# 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
|
||||
# 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)
|
||||
winpr_library_add(rt)
|
||||
|
73
winpr/libwinpr/handle/nonehandle.c
Normal file
73
winpr/libwinpr/handle/nonehandle.c
Normal 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
|
40
winpr/libwinpr/handle/nonehandle.h
Normal file
40
winpr/libwinpr/handle/nonehandle.h
Normal 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 */
|
@ -3,6 +3,7 @@
|
||||
* Process Thread Functions
|
||||
*
|
||||
* 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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -22,6 +23,7 @@
|
||||
#endif
|
||||
|
||||
#include <winpr/handle.h>
|
||||
#include "../handle/nonehandle.h"
|
||||
|
||||
#include <winpr/thread.h>
|
||||
#include <fcntl.h>
|
||||
@ -183,7 +185,7 @@ BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags,
|
||||
LPSTR* pArgs = NULL;
|
||||
char** envp = NULL;
|
||||
char* filename = NULL;
|
||||
WINPR_THREAD* thread;
|
||||
HANDLE thread;
|
||||
WINPR_PROCESS* process;
|
||||
WINPR_ACCESS_TOKEN* token;
|
||||
LPTCH lpszEnvironmentBlock;
|
||||
@ -288,21 +290,15 @@ BOOL _CreateProcessExA(HANDLE hToken, DWORD dwLogonFlags,
|
||||
process->status = 0;
|
||||
process->dwExitCode = 0;
|
||||
|
||||
thread = (WINPR_THREAD*) malloc(sizeof(WINPR_THREAD));
|
||||
|
||||
ZeroMemory(thread, sizeof(WINPR_THREAD));
|
||||
thread = CreateNoneHandle();
|
||||
|
||||
if (!thread)
|
||||
{
|
||||
goto finish;
|
||||
}
|
||||
|
||||
WINPR_HANDLE_SET_TYPE(thread, HANDLE_TYPE_THREAD);
|
||||
|
||||
thread->mainProcess = TRUE;
|
||||
|
||||
lpProcessInformation->hProcess = (HANDLE) process;
|
||||
lpProcessInformation->hThread = (HANDLE) thread;
|
||||
lpProcessInformation->hThread = thread;
|
||||
lpProcessInformation->dwProcessId = (DWORD) pid;
|
||||
lpProcessInformation->dwThreadId = (DWORD) pid;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user