Prevent alloc errors from crashing

Resolves #76
This commit is contained in:
Calvin Lee 2017-08-15 07:56:47 +02:00
parent 5ca88af557
commit 5cc7342606
13 changed files with 109 additions and 13 deletions

View file

@ -39,8 +39,10 @@ static struct wlr_input_device *allocate_device(
int vendor = libinput_device_get_id_vendor(libinput_dev); int vendor = libinput_device_get_id_vendor(libinput_dev);
int product = libinput_device_get_id_product(libinput_dev); int product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev); const char *name = libinput_device_get_name(libinput_dev);
struct wlr_libinput_input_device *wlr_libinput_dev = struct wlr_libinput_input_device *wlr_libinput_dev;
calloc(1, sizeof(struct wlr_libinput_input_device)); if (!(wlr_libinput_dev = calloc(1, sizeof(struct wlr_libinput_input_device)))) {
return NULL;
}
struct wlr_input_device *wlr_dev = &wlr_libinput_dev->wlr_input_device; struct wlr_input_device *wlr_dev = &wlr_libinput_dev->wlr_input_device;
wlr_libinput_dev->handle = libinput_dev; wlr_libinput_dev->handle = libinput_dev;
libinput_device_ref(libinput_dev); libinput_device_ref(libinput_dev);
@ -63,36 +65,74 @@ static void handle_device_added(struct wlr_libinput_backend *backend,
int product = libinput_device_get_id_product(libinput_dev); int product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev); const char *name = libinput_device_get_name(libinput_dev);
list_t *wlr_devices = list_create(); list_t *wlr_devices = list_create();
if (!wlr_devices) {
goto fail;
}
wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product);
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) { if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
struct wlr_input_device *wlr_dev = allocate_device(backend, struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_KEYBOARD); libinput_dev, wlr_devices, WLR_INPUT_DEVICE_KEYBOARD);
if (!wlr_dev) {
goto fail;
}
wlr_dev->keyboard = wlr_libinput_keyboard_create(libinput_dev); wlr_dev->keyboard = wlr_libinput_keyboard_create(libinput_dev);
if (!wlr_dev->keyboard) {
free(wlr_dev);
goto fail;
}
wl_signal_emit(&backend->backend.events.input_add, wlr_dev); wl_signal_emit(&backend->backend.events.input_add, wlr_dev);
} }
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_POINTER)) { if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_POINTER)) {
struct wlr_input_device *wlr_dev = allocate_device(backend, struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_POINTER); libinput_dev, wlr_devices, WLR_INPUT_DEVICE_POINTER);
if (!wlr_dev) {
goto fail;
}
wlr_dev->pointer = wlr_libinput_pointer_create(libinput_dev); wlr_dev->pointer = wlr_libinput_pointer_create(libinput_dev);
if (!wlr_dev->pointer) {
free(wlr_dev);
goto fail;
}
wl_signal_emit(&backend->backend.events.input_add, wlr_dev); wl_signal_emit(&backend->backend.events.input_add, wlr_dev);
} }
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TOUCH)) { if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TOUCH)) {
struct wlr_input_device *wlr_dev = allocate_device(backend, struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TOUCH); libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TOUCH);
if (!wlr_dev) {
goto fail;
}
wlr_dev->touch = wlr_libinput_touch_create(libinput_dev); wlr_dev->touch = wlr_libinput_touch_create(libinput_dev);
if (!wlr_dev->touch) {
free(wlr_dev);
goto fail;
}
wl_signal_emit(&backend->backend.events.input_add, wlr_dev); wl_signal_emit(&backend->backend.events.input_add, wlr_dev);
} }
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) {
struct wlr_input_device *wlr_dev = allocate_device(backend, struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_TOOL); libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_TOOL);
if (!wlr_dev) {
goto fail;
}
wlr_dev->tablet_tool = wlr_libinput_tablet_tool_create(libinput_dev); wlr_dev->tablet_tool = wlr_libinput_tablet_tool_create(libinput_dev);
if (!wlr_dev->tablet_tool) {
free(wlr_dev);
goto fail;
}
wl_signal_emit(&backend->backend.events.input_add, wlr_dev); wl_signal_emit(&backend->backend.events.input_add, wlr_dev);
} }
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_PAD)) { if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_PAD)) {
struct wlr_input_device *wlr_dev = allocate_device(backend, struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_PAD); libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_PAD);
if (!wlr_dev) {
goto fail;
}
wlr_dev->tablet_pad = wlr_libinput_tablet_pad_create(libinput_dev); wlr_dev->tablet_pad = wlr_libinput_tablet_pad_create(libinput_dev);
if (!wlr_dev->tablet_pad) {
free(wlr_dev);
goto fail;
}
wl_signal_emit(&backend->backend.events.input_add, wlr_dev); wl_signal_emit(&backend->backend.events.input_add, wlr_dev);
} }
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_GESTURE)) { if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_GESTURE)) {
@ -108,6 +148,12 @@ static void handle_device_added(struct wlr_libinput_backend *backend,
} else { } else {
list_free(wlr_devices); list_free(wlr_devices);
} }
return;
fail:
wlr_log(L_ERROR, "Could not allocate new device");
list_foreach(wlr_devices, free);
list_free(wlr_devices);
} }
static void handle_device_removed(struct wlr_libinput_backend *backend, static void handle_device_removed(struct wlr_libinput_backend *backend,

View file

@ -32,8 +32,10 @@ struct wlr_keyboard_impl impl = {
struct wlr_keyboard *wlr_libinput_keyboard_create( struct wlr_keyboard *wlr_libinput_keyboard_create(
struct libinput_device *libinput_dev) { struct libinput_device *libinput_dev) {
assert(libinput_dev); assert(libinput_dev);
struct wlr_libinput_keyboard *wlr_libinput_kb = struct wlr_libinput_keyboard *wlr_libinput_kb;
calloc(1, sizeof(struct wlr_libinput_keyboard)); if (!(wlr_libinput_kb= calloc(1, sizeof(struct wlr_libinput_keyboard)))) {
return NULL;
}
wlr_libinput_kb->libinput_dev = libinput_dev; wlr_libinput_kb->libinput_dev = libinput_dev;
libinput_device_ref(libinput_dev); libinput_device_ref(libinput_dev);
libinput_device_led_update(libinput_dev, 0); libinput_device_led_update(libinput_dev, 0);

View file

@ -115,7 +115,11 @@ void wlr_multi_backend_add(struct wlr_backend *_multi,
assert(wlr_backend_is_multi(_multi)); assert(wlr_backend_is_multi(_multi));
struct wlr_multi_backend *multi = (struct wlr_multi_backend *)_multi; struct wlr_multi_backend *multi = (struct wlr_multi_backend *)_multi;
struct subbackend_state *sub = calloc(1, sizeof(struct subbackend_state)); struct subbackend_state *sub;
if (!(sub = calloc(1, sizeof(struct subbackend_state)))) {
wlr_log(L_ERROR, "Could not add backend: allocation failed");
return;
}
sub->backend = backend; sub->backend = backend;
sub->container = &multi->backend; sub->container = &multi->backend;

View file

@ -239,7 +239,7 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
struct wlr_input_device *wlr_device = allocate_device(backend, struct wlr_input_device *wlr_device = allocate_device(backend,
WLR_INPUT_DEVICE_KEYBOARD); WLR_INPUT_DEVICE_KEYBOARD);
if (!wlr_device) { if (!wlr_device) {
wlr_log(L_ERROR, "Unable to allocate wl_pointer device"); wlr_log(L_ERROR, "Unable to allocate wl_keyboard device");
return; return;
} }
wlr_device->keyboard = calloc(1, sizeof(struct wlr_keyboard)); wlr_device->keyboard = calloc(1, sizeof(struct wlr_keyboard));

View file

@ -70,6 +70,10 @@ int main() {
compositor_init(&compositor); compositor_init(&compositor);
state.renderer = wlr_gles2_renderer_init(compositor.backend); state.renderer = wlr_gles2_renderer_init(compositor.backend);
if (!state.renderer) {
wlr_log(L_ERROR, "Could not start compositor, OOM");
exit(EXIT_FAILURE);
}
wl_display_init_shm(compositor.display); wl_display_init_shm(compositor.display);
wl_compositor_init(compositor.display, &state.compositor, state.renderer); wl_compositor_init(compositor.display, &state.compositor, state.renderer);
wl_shell_init(compositor.display, &state.shell); wl_shell_init(compositor.display, &state.shell);

View file

@ -16,6 +16,7 @@
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/backend/session.h> #include <wlr/backend/session.h>
#include <wlr/types/wlr_keyboard.h> #include <wlr/types/wlr_keyboard.h>
#include <wlr/util/log.h>
#include <math.h> #include <math.h>
#include "shared.h" #include "shared.h"
#include "cat.h" #include "cat.h"
@ -205,7 +206,15 @@ int main(int argc, char *argv[]) {
compositor_init(&compositor); compositor_init(&compositor);
state.renderer = wlr_gles2_renderer_init(compositor.backend); state.renderer = wlr_gles2_renderer_init(compositor.backend);
if (!state.renderer) {
wlr_log(L_ERROR, "Could not start compositor, OOM");
exit(EXIT_FAILURE);
}
state.cat_texture = wlr_render_texture_init(state.renderer); state.cat_texture = wlr_render_texture_init(state.renderer);
if (!state.cat_texture) {
wlr_log(L_ERROR, "Could not start compositor, OOM");
exit(EXIT_FAILURE);
}
wlr_texture_upload_pixels(state.cat_texture, WL_SHM_FORMAT_ABGR8888, wlr_texture_upload_pixels(state.cat_texture, WL_SHM_FORMAT_ABGR8888,
cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data); cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data);

View file

@ -17,6 +17,7 @@
#include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_tablet_tool.h> #include <wlr/types/wlr_tablet_tool.h>
#include <wlr/types/wlr_tablet_pad.h> #include <wlr/types/wlr_tablet_pad.h>
#include <wlr/util/log.h>
#include <math.h> #include <math.h>
#include "shared.h" #include "shared.h"
#include "cat.h" #include "cat.h"
@ -153,6 +154,10 @@ int main(int argc, char *argv[]) {
compositor_init(&compositor); compositor_init(&compositor);
state.renderer = wlr_gles2_renderer_init(compositor.backend); state.renderer = wlr_gles2_renderer_init(compositor.backend);
if (!state.renderer) {
wlr_log(L_ERROR, "Could not start compositor, OOM");
exit(EXIT_FAILURE);
}
compositor_run(&compositor); compositor_run(&compositor);
wlr_renderer_destroy(state.renderer); wlr_renderer_destroy(state.renderer);

View file

@ -17,6 +17,7 @@
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/backend/session.h> #include <wlr/backend/session.h>
#include <wlr/util/list.h> #include <wlr/util/list.h>
#include <wlr/util/log.h>
#include "shared.h" #include "shared.h"
#include "cat.h" #include "cat.h"
@ -105,7 +106,15 @@ int main(int argc, char *argv[]) {
compositor_init(&compositor); compositor_init(&compositor);
state.renderer = wlr_gles2_renderer_init(compositor.backend); state.renderer = wlr_gles2_renderer_init(compositor.backend);
if (!state.renderer) {
wlr_log(L_ERROR, "Could not start compositor, OOM");
exit(EXIT_FAILURE);
}
state.cat_texture = wlr_render_texture_init(state.renderer); state.cat_texture = wlr_render_texture_init(state.renderer);
if (!state.cat_texture) {
wlr_log(L_ERROR, "Could not start compositor, OOM");
exit(EXIT_FAILURE);
}
wlr_texture_upload_pixels(state.cat_texture, WL_SHM_FORMAT_ARGB8888, wlr_texture_upload_pixels(state.cat_texture, WL_SHM_FORMAT_ARGB8888,
cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data); cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data);

View file

@ -240,8 +240,10 @@ static struct wlr_renderer_impl wlr_renderer_impl = {
struct wlr_renderer *wlr_gles2_renderer_init(struct wlr_backend *backend) { struct wlr_renderer *wlr_gles2_renderer_init(struct wlr_backend *backend) {
init_globals(); init_globals();
struct wlr_gles2_renderer *renderer = struct wlr_gles2_renderer *renderer;
calloc(1, sizeof(struct wlr_gles2_renderer)); if (!(renderer = calloc(1, sizeof(struct wlr_gles2_renderer)))) {
return NULL;
}
wlr_renderer_init(&renderer->wlr_renderer, &wlr_renderer_impl); wlr_renderer_init(&renderer->wlr_renderer, &wlr_renderer_impl);
if (backend) { if (backend) {
struct wlr_egl *egl = wlr_backend_get_egl(backend); struct wlr_egl *egl = wlr_backend_get_egl(backend);

View file

@ -252,8 +252,10 @@ static struct wlr_texture_impl wlr_texture_impl = {
}; };
struct wlr_texture *gles2_texture_init(struct wlr_egl *egl) { struct wlr_texture *gles2_texture_init(struct wlr_egl *egl) {
struct wlr_gles2_texture *texture = struct wlr_gles2_texture *texture;
calloc(1, sizeof(struct wlr_gles2_texture)); if (!(texture = calloc(1, sizeof(struct wlr_gles2_texture)))) {
return NULL;
}
wlr_texture_init(&texture->wlr_texture, &wlr_texture_impl); wlr_texture_init(&texture->wlr_texture, &wlr_texture_impl);
texture->egl = egl; texture->egl = egl;
return &texture->wlr_texture; return &texture->wlr_texture;

View file

@ -200,7 +200,11 @@ static void destroy_surface(struct wl_resource *resource) {
struct wlr_surface *wlr_surface_create(struct wl_resource *res, struct wlr_surface *wlr_surface_create(struct wl_resource *res,
struct wlr_renderer *renderer) { struct wlr_renderer *renderer) {
struct wlr_surface *surface = calloc(1, sizeof(struct wlr_surface)); struct wlr_surface *surface;
if (!(surface = calloc(1, sizeof(struct wlr_surface)))) {
wl_resource_post_no_memory(res);
return NULL;
}
surface->renderer = renderer; surface->renderer = renderer;
surface->texture = wlr_render_texture_init(renderer); surface->texture = wlr_render_texture_init(renderer);
surface->resource = res; surface->resource = res;

View file

@ -141,8 +141,10 @@ static void xdg_shell_get_xdg_surface(struct wl_client *client,
struct wl_resource *_xdg_shell, uint32_t id, struct wl_resource *_xdg_shell, uint32_t id,
struct wl_resource *_surface) { struct wl_resource *_surface) {
struct wlr_xdg_shell_v6 *xdg_shell = wl_resource_get_user_data(_xdg_shell); struct wlr_xdg_shell_v6 *xdg_shell = wl_resource_get_user_data(_xdg_shell);
struct wlr_xdg_surface_v6 *surface = struct wlr_xdg_surface_v6 *surface;
calloc(1, sizeof(struct wlr_xdg_surface_v6)); if (!(surface = calloc(1, sizeof(struct wlr_xdg_surface_v6)))) {
return;
}
surface->surface = _surface; surface->surface = _surface;
surface->resource = wl_resource_create(client, surface->resource = wl_resource_create(client,
&zxdg_surface_v6_interface, wl_resource_get_version(_xdg_shell), id); &zxdg_surface_v6_interface, wl_resource_get_version(_xdg_shell), id);

View file

@ -6,9 +6,16 @@
list_t *list_create(void) { list_t *list_create(void) {
list_t *list = malloc(sizeof(list_t)); list_t *list = malloc(sizeof(list_t));
if (!list) {
return NULL;
}
list->capacity = 10; list->capacity = 10;
list->length = 0; list->length = 0;
list->items = malloc(sizeof(void*) * list->capacity); list->items = malloc(sizeof(void*) * list->capacity);
if (!list->items) {
free(list);
return NULL;
}
return list; return list;
} }