shape: More robust handling of failure cases in CreateShaper.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
diff --git a/src/video/cocoa/SDL_cocoashape.m b/src/video/cocoa/SDL_cocoashape.m
index 6dbd64b..ac3459c 100644
--- a/src/video/cocoa/SDL_cocoashape.m
+++ b/src/video/cocoa/SDL_cocoashape.m
@@ -47,11 +47,17 @@ Cocoa_CreateShaper(SDL_Window* window)
SDL_ShapeData* data;
int resized_properly;
SDL_WindowData* windata = (__bridge SDL_WindowData*)window->driverdata;
+
+ result = (SDL_WindowShaper *)SDL_malloc(sizeof(SDL_WindowShaper));
+ if (!result) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
+
[windata.nswindow setOpaque:NO];
[windata.nswindow setStyleMask:NSWindowStyleMaskBorderless];
- result = (SDL_WindowShaper *)SDL_malloc(sizeof(SDL_WindowShaper));
result->window = window;
result->mode.mode = ShapeModeDefault;
result->mode.parameters.binarizationCutoff = 1;
diff --git a/src/video/directfb/SDL_DirectFB_shape.c b/src/video/directfb/SDL_DirectFB_shape.c
index ff334c8..b6f5d08 100644
--- a/src/video/directfb/SDL_DirectFB_shape.c
+++ b/src/video/directfb/SDL_DirectFB_shape.c
@@ -35,11 +35,20 @@ DirectFB_CreateShaper(SDL_Window* window) {
int resized_properly;
result = SDL_malloc(sizeof(SDL_WindowShaper));
+ if (!result) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
result->window = window;
result->mode.mode = ShapeModeDefault;
result->mode.parameters.binarizationCutoff = 1;
result->userx = result->usery = 0;
data = SDL_malloc(sizeof(SDL_ShapeData));
+ if (!data) {
+ SDL_free(result);
+ SDL_OutOfMemory();
+ return NULL;
+ }
result->driverdata = data;
data->surface = NULL;
window->shaper = result;
diff --git a/src/video/windows/SDL_windowsshape.c b/src/video/windows/SDL_windowsshape.c
index 919076f..484566d 100644
--- a/src/video/windows/SDL_windowsshape.c
+++ b/src/video/windows/SDL_windowsshape.c
@@ -29,13 +29,21 @@ SDL_WindowShaper*
Win32_CreateShaper(SDL_Window * window) {
int resized_properly;
SDL_WindowShaper* result = (SDL_WindowShaper *)SDL_malloc(sizeof(SDL_WindowShaper));
+ if (!result) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
result->window = window;
result->mode.mode = ShapeModeDefault;
result->mode.parameters.binarizationCutoff = 1;
result->userx = result->usery = 0;
result->hasshape = SDL_FALSE;
- result->driverdata = (SDL_ShapeData*)SDL_malloc(sizeof(SDL_ShapeData));
- ((SDL_ShapeData*)result->driverdata)->mask_tree = NULL;
+ result->driverdata = (SDL_ShapeData*)SDL_calloc(1, sizeof(SDL_ShapeData));
+ if (!result->driverdata) {
+ SDL_free(result);
+ SDL_OutOfMemory();
+ return NULL;
+ }
window->shaper = result;
/* Put some driver-data here. */
resized_properly = Win32_ResizeWindowShape(window);
diff --git a/src/video/x11/SDL_x11shape.c b/src/video/x11/SDL_x11shape.c
index 7d78f11..7d4e32c 100644
--- a/src/video/x11/SDL_x11shape.c
+++ b/src/video/x11/SDL_x11shape.c
@@ -31,22 +31,34 @@ SDL_WindowShaper*
X11_CreateShaper(SDL_Window* window) {
SDL_WindowShaper* result = NULL;
SDL_ShapeData* data = NULL;
- int resized_properly;
#if SDL_VIDEO_DRIVER_X11_XSHAPE
if (SDL_X11_HAVE_XSHAPE) { /* Make sure X server supports it. */
result = SDL_malloc(sizeof(SDL_WindowShaper));
+ if (!result) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
result->window = window;
result->mode.mode = ShapeModeDefault;
result->mode.parameters.binarizationCutoff = 1;
result->userx = result->usery = 0;
data = SDL_malloc(sizeof(SDL_ShapeData));
+ if (!data) {
+ SDL_free(result);
+ SDL_OutOfMemory();
+ return NULL;
+ }
result->driverdata = data;
data->bitmapsize = 0;
data->bitmap = NULL;
window->shaper = result;
- resized_properly = X11_ResizeWindowShape(window);
- SDL_assert(resized_properly == 0);
+ if (X11_ResizeWindowShape(window) != 0) {
+ SDL_free(result);
+ SDL_free(data);
+ window->shaper = NULL;
+ return NULL;
+ }
}
#endif
@@ -64,11 +76,10 @@ X11_ResizeWindowShape(SDL_Window* window) {
bitmapsize *= window->h;
if(data->bitmapsize != bitmapsize || data->bitmap == NULL) {
data->bitmapsize = bitmapsize;
- if(data->bitmap != NULL)
- SDL_free(data->bitmap);
+ SDL_free(data->bitmap);
data->bitmap = SDL_malloc(data->bitmapsize);
if(data->bitmap == NULL) {
- return SDL_SetError("Could not allocate memory for shaped-window bitmap.");
+ return SDL_OutOfMemory();
}
}
SDL_memset(data->bitmap,0,data->bitmapsize);