Skip to content

Commit f7b21e5

Browse files
jayathirthraokevinrushforth
authored andcommitted
8315074: Possible null pointer access in native glass
Reviewed-by: jvos, kcr
1 parent 7c8dd1e commit f7b21e5

File tree

8 files changed

+98
-35
lines changed

8 files changed

+98
-35
lines changed

modules/javafx.graphics/src/main/native-glass/gtk/GlassApplication.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,13 @@ JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1submitForLater
262262
(void)obj;
263263

264264
RunnableContext* context = (RunnableContext*)malloc(sizeof(RunnableContext));
265-
context->runnable = env->NewGlobalRef(runnable);
266-
gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE + 30, call_runnable, context, NULL);
265+
if (context != NULL) {
266+
context->runnable = env->NewGlobalRef(runnable);
267+
gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE + 30, call_runnable, context, NULL);
268+
// we release this context in call_runnable
269+
} else {
270+
fprintf(stderr, "malloc failed in GtkApplication__1submitForLaterInvocation\n");
271+
}
267272
}
268273

269274
/*

modules/javafx.graphics/src/main/native-glass/gtk/GlassTimer.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@ JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_gtk_GtkTimer__1start
4545
(void)obj;
4646

4747
RunnableContext* context = (RunnableContext*) malloc(sizeof(RunnableContext));
48-
context->runnable = env->NewGlobalRef(runnable);
49-
context->flag = 0;
50-
gdk_threads_add_timeout_full(G_PRIORITY_HIGH_IDLE, period, call_runnable_in_timer, context, NULL);
51-
return PTR_TO_JLONG(context);
48+
if (context != NULL) {
49+
context->runnable = env->NewGlobalRef(runnable);
50+
context->flag = 0;
51+
gdk_threads_add_timeout_full(G_PRIORITY_HIGH_IDLE, period, call_runnable_in_timer, context, NULL);
52+
return PTR_TO_JLONG(context);
53+
} else {
54+
// we throw RuntimeException on Java side when we can't
55+
// start the timer
56+
return 0L;
57+
}
5258
}
5359

5460
/*

modules/javafx.graphics/src/main/native-glass/gtk/glass_general.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ struct jni_exception: public std::exception {
9696
jstring jmessage;
9797
};
9898

99+
#define SAFE_FREE(PTR) \
100+
if ((PTR) != NULL) { \
101+
free(PTR); \
102+
(PTR) = NULL; \
103+
}
104+
99105
#define EXCEPTION_OCCURED(env) (check_and_clear_exception(env))
100106

101107
#define CHECK_JNI_EXCEPTION(env) \

modules/javafx.graphics/src/main/native-glass/gtk/glass_window_ime.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ bool WindowContextBase::im_filter_keypress(GdkEventKey* event) {
5959
buffer = (char*)malloc(buf_len * sizeof (char));
6060
}
6161

62+
if (buffer == NULL) {
63+
// dont process the key event
64+
fprintf(stderr, "malloc failed in im_filter_keypress\n");
65+
return false;
66+
}
67+
6268
KeySym keysym;
6369
Status status;
6470
XKeyPressedEvent xevent = convert_event(event);
@@ -74,7 +80,14 @@ bool WindowContextBase::im_filter_keypress(GdkEventKey* event) {
7480
int len = Xutf8LookupString(xim.ic, &xevent, buffer, buf_len - 1, &keysym, &status);
7581
if (status == XBufferOverflow) {
7682
buf_len = len + 1;
77-
buffer = (char*)realloc(buffer, buf_len * sizeof (char));
83+
char *tmpBuffer = (char*)realloc(buffer, buf_len * sizeof (char));
84+
if (tmpBuffer == NULL) {
85+
SAFE_FREE(buffer);
86+
// dont process the key event
87+
fprintf(stderr, "realloc failed in im_filter_keypress\n");
88+
return false;
89+
}
90+
buffer = tmpBuffer;
7891
len = Xutf8LookupString(xim.ic, &xevent, buffer, buf_len - 1,
7992
&keysym, &status);
8093
}

modules/javafx.graphics/src/main/native-glass/ios/GlassMacros.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,34 @@ do {
7373
GlassThreadData *_GlassThreadData = (GlassThreadData*)pthread_getspecific(GlassThreadDataKey); \
7474
if (_GlassThreadData == NULL) \
7575
{ \
76-
_GlassThreadData = malloc(sizeof(GlassThreadData)); \
77-
memset(_GlassThreadData, 0x00, sizeof(GlassThreadData)); \
78-
pthread_setspecific(GlassThreadDataKey, _GlassThreadData); \
76+
_GlassThreadData = calloc(1, sizeof(GlassThreadData)); \
77+
if (_GlassThreadData != NULL) \
78+
{ \
79+
pthread_setspecific(GlassThreadDataKey, _GlassThreadData); \
80+
} \
81+
else \
82+
{ \
83+
fprintf(stderr, "malloc failed in GLASS_POOL_ENTER\n"); \
84+
} \
7985
} \
80-
assert(_GlassThreadData->counter >= 0); \
81-
if (_GlassThreadData->counter++ == 0) \
86+
if (_GlassThreadData != NULL) \
8287
{ \
83-
_GlassThreadData->pool = [[NSAutoreleasePool alloc] init]; \
88+
assert(_GlassThreadData->counter >= 0); \
89+
if (_GlassThreadData->counter++ == 0) \
90+
{ \
91+
_GlassThreadData->pool = [[NSAutoreleasePool alloc] init]; \
92+
} \
8493
}
8594
#define GLASS_POOL_EXIT \
86-
if (--_GlassThreadData->counter == 0) \
95+
if (_GlassThreadData != NULL) \
8796
{ \
88-
[_GlassThreadData->pool drain]; \
89-
_GlassThreadData->pool = nil; \
97+
if (--_GlassThreadData->counter == 0) \
98+
{ \
99+
[_GlassThreadData->pool drain]; \
100+
_GlassThreadData->pool = nil; \
101+
} \
102+
assert(_GlassThreadData->counter >= 0); \
90103
} \
91-
assert(_GlassThreadData->counter >= 0); \
92104
}
93105

94106
// variations of GLASS_POOL_ENTER/GLASS_POOL_EXIT allowing them to be used accross different calls

modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ - (void)application:(NSApplication *)theApplication openFiles:(NSArray *)filenam
362362
LOG("GlassApplication:application:openFiles");
363363

364364
GET_MAIN_JENV;
365+
NSApplicationDelegateReply reply = NSApplicationDelegateReplySuccess;
365366
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
366367
{
367368
NSUInteger count = [filenames count];
@@ -371,21 +372,26 @@ - (void)application:(NSApplication *)theApplication openFiles:(NSArray *)filenam
371372
}
372373
jobjectArray files = (*env)->NewObjectArray(env, (jsize)count, stringClass, NULL);
373374
GLASS_CHECK_EXCEPTION(env);
374-
for (NSUInteger i=0; i<count; i++)
375-
{
376-
NSString *file = [filenames objectAtIndex:i];
377-
if (file != nil)
375+
if (files != NULL) {
376+
for (NSUInteger i=0; i<count; i++)
378377
{
379-
(*env)->SetObjectArrayElement(env, files, (jsize)i, (*env)->NewStringUTF(env, [file UTF8String]));
380-
GLASS_CHECK_EXCEPTION(env);
378+
NSString *file = [filenames objectAtIndex:i];
379+
if (file != nil)
380+
{
381+
(*env)->SetObjectArrayElement(env, files, (jsize)i, (*env)->NewStringUTF(env, [file UTF8String]));
382+
GLASS_CHECK_EXCEPTION(env);
383+
}
381384
}
385+
(*env)->CallVoidMethod(env, self->jApplication, [GlassHelper ApplicationNotifyOpenFilesMethod], files);
386+
} else {
387+
fprintf(stderr, "NewObjectArray failed in GlassApplication_application\n");
388+
reply = NSApplicationDelegateReplyFailure;
382389
}
383-
(*env)->CallVoidMethod(env, self->jApplication, [GlassHelper ApplicationNotifyOpenFilesMethod], files);
384390
}
385391
[pool drain];
386392
GLASS_CHECK_EXCEPTION(env);
387393

388-
[theApplication replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
394+
[theApplication replyToOpenOrPrint:reply];
389395
}
390396

391397
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename

modules/javafx.graphics/src/main/native-glass/mac/GlassKey.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ jcharArray GetJavaKeyChars(JNIEnv *env, NSEvent *event)
461461
jchar jc[16];
462462
[chars getCharacters:jc range:NSMakeRange(0, [chars length])];
463463
jcharArray jChars = (*env)->NewCharArray(env, (jsize)[chars length]);
464+
if (jChars == NULL) {
465+
return NULL;
466+
}
464467
(*env)->SetCharArrayRegion(env, jChars, 0, (jsize)[chars length], jc);
465468
GLASS_CHECK_EXCEPTION(env);
466469
return jChars;

modules/javafx.graphics/src/main/native-glass/mac/GlassMacros.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,34 @@ do {
177177
GlassThreadData *_GlassThreadData = (GlassThreadData*)pthread_getspecific(GlassThreadDataKey); \
178178
if (_GlassThreadData == NULL) \
179179
{ \
180-
_GlassThreadData = malloc(sizeof(GlassThreadData)); \
181-
memset(_GlassThreadData, 0x00, sizeof(GlassThreadData)); \
182-
pthread_setspecific(GlassThreadDataKey, _GlassThreadData); \
180+
_GlassThreadData = calloc(1, sizeof(GlassThreadData)); \
181+
if (_GlassThreadData != NULL) \
182+
{ \
183+
pthread_setspecific(GlassThreadDataKey, _GlassThreadData); \
184+
} \
185+
else \
186+
{ \
187+
fprintf(stderr, "malloc failed in GLASS_POOL_ENTER\n"); \
188+
} \
183189
} \
184-
assert(_GlassThreadData->counter >= 0); \
185-
if (_GlassThreadData->counter++ == 0) \
190+
if (_GlassThreadData != NULL) \
186191
{ \
187-
_GlassThreadData->pool = [[NSAutoreleasePool alloc] init]; \
192+
assert(_GlassThreadData->counter >= 0); \
193+
if (_GlassThreadData->counter++ == 0) \
194+
{ \
195+
_GlassThreadData->pool = [[NSAutoreleasePool alloc] init]; \
196+
} \
188197
}
189198
#define GLASS_POOL_EXIT \
190-
if (--_GlassThreadData->counter == 0) \
199+
if (_GlassThreadData != NULL) \
191200
{ \
192-
[_GlassThreadData->pool drain]; \
193-
_GlassThreadData->pool = nil; \
201+
if (--_GlassThreadData->counter == 0) \
202+
{ \
203+
[_GlassThreadData->pool drain]; \
204+
_GlassThreadData->pool = nil; \
205+
} \
206+
assert(_GlassThreadData->counter >= 0); \
194207
} \
195-
assert(_GlassThreadData->counter >= 0); \
196208
}
197209

198210
// variations of GLASS_POOL_ENTER/GLASS_POOL_EXIT allowing them to be used accross different calls

0 commit comments

Comments
 (0)