Skip to content

Commit

Permalink
Fix a memory leak and missing error check in _get_windows_argv
Browse files Browse the repository at this point in the history
Fixes #495
  • Loading branch information
segevfiner committed Oct 7, 2018
1 parent 930ad81 commit f014a47
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions click/_winconsole.py
Expand Up @@ -17,7 +17,8 @@
import msvcrt
from ._compat import _NonClosingTextIOWrapper, text_type, PY2
from ctypes import byref, POINTER, c_int, c_char, c_char_p, \
c_void_p, py_object, c_ssize_t, c_ulong, windll, WINFUNCTYPE
c_void_p, py_object, c_ssize_t, c_ulong, windll, WINFUNCTYPE, \
WinError
try:
from ctypes import pythonapi
PyObject_GetBuffer = pythonapi.PyObject_GetBuffer
Expand All @@ -39,6 +40,8 @@
CommandLineToArgvW = WINFUNCTYPE(
POINTER(LPWSTR), LPCWSTR, POINTER(c_int))(
('CommandLineToArgvW', windll.shell32))
LocalFree = WINFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p)(
('LocalFree', windll.kernel32))


STDIN_HANDLE = GetStdHandle(-10)
Expand Down Expand Up @@ -265,7 +268,13 @@ def _hash_py_argv():
def _get_windows_argv():
argc = c_int(0)
argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))
argv = [argv_unicode[i] for i in range(0, argc.value)]
if not argv_unicode:
raise WinError()
try:
argv = [argv_unicode[i] for i in range(0, argc.value)]
finally:
LocalFree(argv_unicode)
del argv_unicode

if not hasattr(sys, 'frozen'):
argv = argv[1:]
Expand Down

0 comments on commit f014a47

Please sign in to comment.