Skip to content

Commit

Permalink
#4275 msvcrt malloc and free incorrect default definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Jul 3, 2024
1 parent a409b1b commit 57b88bc
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions xpra/platform/win32/win32_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# later version. See the file COPYING for details.

from ctypes import (
cdll, c_void_p, Structure, cast, c_char, c_int, pointer, POINTER,
cdll, c_size_t, c_void_p, Structure, cast, c_char, c_int, pointer, POINTER,
WinDLL, # @UnresolvedImport
)
from ctypes.wintypes import HDC, HANDLE, BOOL, BYTE, LPCSTR, DWORD, WORD
Expand All @@ -23,6 +23,12 @@
PSECURITY_DESCRIPTOR = HANDLE

msvcrt = cdll.msvcrt
malloc = malloc
malloc.argtypes = [c_size_t]
malloc.restype = c_void_p
free = free
free.argtypes = [c_void_p]
free.restype = None
winspool = WinDLL('winspool.drv', use_last_error=True)
OpenPrinterA = winspool.OpenPrinterA
OpenPrinterA.restype = BOOL
Expand Down Expand Up @@ -135,7 +141,7 @@ def __enter__(self):
if size.value==0:
raise RuntimeError("GetPrinterA PRINTER_INFO_1 failed for '%s'" % self.printer_name)
log("GetPrinter: PRINTER_INFO_1 size=%#x", size.value)
self.info1 = msvcrt.malloc(size.value)
self.info1 = malloc(size.value)
if not GetPrinterA(self.handle, 1, self.info1, size.value, pointer(size)):
raise RuntimeError("GetPrinterA PRINTER_INFO_1 failed for '%s'" % self.printer_name)
info = cast(self.info1, POINTER(PRINTER_INFO_1))
Expand All @@ -149,7 +155,7 @@ def __enter__(self):
if size.value==0:
raise RuntimeError("GetPrinterA PRINTER_INFO_2 failed for '%s'" % self.printer_name)
log("GetPrinter: PRINTER_INFO_2 size=%#x", size.value)
self.info2 = msvcrt.malloc(size.value)
self.info2 = malloc(size.value)
if GetPrinterA(self.handle, 2, self.info2, size.value, pointer(size)):
info = cast(self.info2, POINTER(PRINTER_INFO_2))
log(" driver=%#s" % info[0].pDriverName)
Expand All @@ -159,7 +165,7 @@ def __enter__(self):
GetPrinterA(self.handle, 8, None, 0, pointer(size))
if size.value==0:
raise RuntimeError("GetPrinter: PRINTER_INFO_8 failed for '%s'" % self.printer_name)
self.info8 = msvcrt.malloc(size.value)
self.info8 = malloc(size.value)
if GetPrinterA(self.handle, 8, self.info8, size.value, pointer(size)):
info = cast(self.info8, POINTER(PRINTER_INFO_8))
if info[0] and info[0].pDevMode:
Expand All @@ -172,7 +178,7 @@ def __enter__(self):
if size.value==0:
raise RuntimeError("GetPrinter: PRINTER_INFO_9 failed for '%s'" % self.printer_name)
log("GetPrinter: PRINTER_INFO_9 size=%#x" % size.value)
self.info9 = msvcrt.malloc(size.value)
self.info9 = malloc(size.value)
if GetPrinterA(self.handle, 9, self.info9, size.value, pointer(size)):
info = cast(self.info9, POINTER(PRINTER_INFO_9))
if info[0] and info[0].pDevMode:
Expand All @@ -191,16 +197,16 @@ def __exit__(self, exc_type, exc_val, exc_tb):
DeleteDC(self.hdc)
self.hdc = None
if self.info1:
msvcrt.free(self.info1)
free(self.info1)
self.info1 = None
if self.info2:
msvcrt.free(self.info2)
free(self.info2)
self.info2 = None
if self.info8:
msvcrt.free(self.info8)
free(self.info8)
self.info8 = None
if self.info9:
msvcrt.free(self.info9)
free(self.info9)
self.info9 = None
if self.handle:
ClosePrinter(self.handle)
Expand Down

0 comments on commit 57b88bc

Please sign in to comment.