FlatLaf icon indicating copy to clipboard operation
FlatLaf copied to clipboard

Memory allocation fixes in flatlaf-natives/flatlaf-natives-windows

Open johnplatts opened this issue 3 years ago • 1 comments

I noticed that the operator delete[] overload is missing (and should be added) to the flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp file as follows:

 void __cdecl operator delete[]( void* pv, size_t cb ) { 
 	if( pv != NULL ) 
 		::HeapFree( ::GetProcessHeap(), 0, pv ); 
 } 

The following line in flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp needs to be changed to delete[] table: https://github.com/JFormDesigner/FlatLaf/blob/596ff3382dc068c3765ffe91c99cad8b31d12f4c/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp#L137

There is also undefined behavior in FlatWndProc::install, HWNDMap::HWNDMap, and HWNDMap::ensureCapacity if memory allocation fails.

johnplatts avatar Sep 19 '22 17:09 johnplatts

I have created a branch at https://github.com/johnplatts/FlatLaf_Win32_Native_Fix/tree/flatlaf_win32_native_fix that includes fixes to flatlaf-natives/flatlaf-natives-windows/src/main/cpp/ to fix the issues surrounding memory allocation failures and overflow in HWNDMap::ensureCapacity.

johnplatts avatar Sep 19 '22 19:09 johnplatts

@johnplatts Thanks. I'll have a look at this in the next weeks.

DevCharly avatar Sep 30 '22 22:09 DevCharly

@johnplatts thanks for the code. I've changed it a little bit 😉 Sorry, but didn't like the extra methods in AllocRoutines.h. So, removed it.

I've also removed the integer overflow handling in HWNDMap because this will never occur. The map is designed for a small amount of entries. (uses sorted table) It is used only for shown Swing windows/frames/dialogs. Usually there is only one main window and some temporary dialogs. But there will be never 2147483647 windows. 😉

DevCharly avatar Nov 26 '22 18:11 DevCharly