Skip to content

Commit f9ecffb

Browse files
committed
flatlaf-natives-windows: fixed memory allocation error handling (issue #591)
2 parents d209d47 + c9b5274 commit f9ecffb

4 files changed

Lines changed: 50 additions & 12 deletions

File tree

flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,29 @@ HWND FlatWndProc::install( JNIEnv *env, jobject obj, jobject window ) {
9999
return 0;
100100

101101
// create HWND map
102-
if( hwndMap == NULL )
102+
if( hwndMap == NULL ) {
103103
hwndMap = new HWNDMap();
104+
if( hwndMap == NULL )
105+
return 0;
106+
}
104107

105108
// get window handle
106109
HWND hwnd = getWindowHandle( env, window );
107110
if( hwnd == NULL || hwndMap->get( hwnd ) != NULL )
108111
return 0;
109112

110113
FlatWndProc* fwp = new FlatWndProc();
114+
if( fwp == NULL )
115+
return 0;
116+
117+
if( !hwndMap->put( hwnd, fwp ) ) {
118+
delete fwp;
119+
return 0;
120+
}
121+
111122
env->GetJavaVM( &fwp->jvm );
112123
fwp->obj = env->NewGlobalRef( obj );
113124
fwp->hwnd = hwnd;
114-
hwndMap->put( hwnd, fwp );
115125

116126
// replace window procedure
117127
fwp->defaultWndProc = reinterpret_cast<WNDPROC>(

flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class LOCK {
4343

4444
HWNDMap::HWNDMap() {
4545
size = 0;
46-
capacity = DEFAULT_CAPACITY;
47-
table = new Entry[capacity];
46+
capacity = 0;
47+
table = NULL;
4848

4949
::InitializeCriticalSection( &criticalSection );
5050

@@ -58,7 +58,7 @@ LPVOID HWNDMap::get( HWND key ) {
5858
return (index >= 0) ? table[index].value : NULL;
5959
}
6060

61-
void HWNDMap::put( HWND key, LPVOID value ) {
61+
bool HWNDMap::put( HWND key, LPVOID value ) {
6262
LOCK lock( &criticalSection );
6363

6464
int index = binarySearch( key );
@@ -68,9 +68,10 @@ void HWNDMap::put( HWND key, LPVOID value ) {
6868
table[index].value = value;
6969
} else {
7070
// insert new key
71-
ensureCapacity( size + 1 );
71+
if( !ensureCapacity() )
72+
return false;
7273

73-
// make roor for new entry
74+
// make room for new entry
7475
index = -(index + 1);
7576
for( int i = size - 1; i >= index; i-- )
7677
table[i + 1] = table[i];
@@ -82,6 +83,7 @@ void HWNDMap::put( HWND key, LPVOID value ) {
8283
}
8384

8485
// dump( "put" );
86+
return true;
8587
}
8688

8789
void HWNDMap::remove( HWND key ) {
@@ -102,6 +104,9 @@ void HWNDMap::remove( HWND key ) {
102104
}
103105

104106
int HWNDMap::binarySearch( HWND key ) {
107+
if( table == NULL )
108+
return -1;
109+
105110
__int64 ikey = reinterpret_cast<__int64>( key );
106111
int low = 0;
107112
int high = size - 1;
@@ -121,23 +126,37 @@ int HWNDMap::binarySearch( HWND key ) {
121126
return -(low + 1);
122127
}
123128

124-
void HWNDMap::ensureCapacity( int minCapacity ) {
129+
bool HWNDMap::ensureCapacity() {
130+
if( table == NULL ) {
131+
table = new Entry[DEFAULT_CAPACITY];
132+
if( table == NULL )
133+
return false;
134+
135+
capacity = DEFAULT_CAPACITY;
136+
return true;
137+
}
138+
139+
// check capacity
140+
int minCapacity = size + 1;
125141
if( minCapacity <= capacity )
126-
return;
142+
return true;
127143

128144
// allocate new table
129145
int newCapacity = minCapacity + INCREASE_CAPACITY;
130146
Entry* newTable = new Entry[newCapacity];
147+
if( newTable == NULL )
148+
return false;
131149

132150
// copy old table to new table
133151
for( int i = 0; i < capacity; i++ )
134152
newTable[i] = table[i];
135153

136154
// delete old table
137-
delete table;
155+
delete[] table;
138156

139157
table = newTable;
140158
capacity = newCapacity;
159+
return true;
141160
}
142161

143162
/*

flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ class HWNDMap
4242
HWNDMap();
4343

4444
LPVOID get( HWND key );
45-
void put( HWND key, LPVOID value );
45+
bool put( HWND key, LPVOID value );
4646
void remove( HWND key );
4747

4848
private:
4949
int binarySearch( HWND key );
50-
void ensureCapacity( int newCapacity );
50+
bool ensureCapacity();
5151

5252
// void dump( char* msg );
5353
};

flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,23 @@ BOOL WINAPI _DllMainCRTStartup( HINSTANCE instance, DWORD reason, LPVOID reserve
4242
}
4343

4444
void* __cdecl operator new( size_t cb ) {
45+
// printf( "new %d\n", cb );
4546
return ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, cb );
4647
}
4748

4849
void* __cdecl operator new[]( size_t cb ) {
50+
// printf( "new[] %d\n", cb );
4951
return ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, cb );
5052
}
5153

5254
void __cdecl operator delete( void* pv, size_t cb ) {
55+
// printf( "delete %p %d\n", pv, cb );
56+
if( pv != NULL )
57+
::HeapFree( ::GetProcessHeap(), 0, pv );
58+
}
59+
60+
void __cdecl operator delete[]( void* pv ) {
61+
// printf( "delete[] %p\n", pv );
5362
if( pv != NULL )
5463
::HeapFree( ::GetProcessHeap(), 0, pv );
5564
}

0 commit comments

Comments
 (0)