[PATCH 2/5] clients: Use x*alloc routines for memory allocation
Eoff, Ullysses A
ullysses.a.eoff at intel.com
Wed May 7 13:03:34 PDT 2014
This fixes a few possible NULL deref's, too... not that it matters much
once you're in an OOM situation.
LGTM
-- U. Artie
> -----Original Message-----
> From: wayland-devel [mailto:wayland-devel-bounces at lists.freedesktop.org] On Behalf Of Bryce W. Harrington
> Sent: Tuesday, May 06, 2014 7:13 PM
> To: wayland-devel at lists.freedesktop.org
> Cc: Bryce W. Harrington
> Subject: [PATCH 2/5] clients: Use x*alloc routines for memory allocation
>
> Since these are all demo client programs, program termination is an
> appropriate response to an out-of-memory situation. Using these
> routines keeps the client code more concise.
>
> Signed-off-by: Bryce Harrington <b.harrington at samsung.com>
> ---
> clients/calibrator.c | 5 +----
> clients/desktop-shell.c | 5 +----
> clients/dnd.c | 8 ++------
> clients/editor.c | 4 ++--
> clients/eventdemo.c | 5 +----
> clients/fullscreen.c | 2 +-
> clients/gears.c | 2 +-
> clients/image.c | 4 +---
> clients/smoke.c | 12 ++++++------
> clients/subsurfaces.c | 4 +---
> clients/wscreensaver-glue.c | 8 ++------
> clients/wscreensaver.c | 14 +++-----------
> 12 files changed, 22 insertions(+), 51 deletions(-)
>
> diff --git a/clients/calibrator.c b/clients/calibrator.c
> index 1eb117f..67ee70e 100644
> --- a/clients/calibrator.c
> +++ b/clients/calibrator.c
> @@ -222,10 +222,7 @@ calibrator_create(struct display *display)
> {
> struct calibrator *calibrator;
>
> - calibrator = malloc(sizeof *calibrator);
> - if (calibrator == NULL)
> - return NULL;
> -
> + calibrator = xmalloc(sizeof *calibrator);
> calibrator->window = window_create(display);
> calibrator->widget = window_add_widget(calibrator->window, calibrator);
> window_set_title(calibrator->window, "Wayland calibrator");
> diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
> index 4880888..a3b2534 100644
> --- a/clients/desktop-shell.c
> +++ b/clients/desktop-shell.c
> @@ -1217,10 +1217,7 @@ create_output(struct desktop *desktop, uint32_t id)
> {
> struct output *output;
>
> - output = calloc(1, sizeof *output);
> - if (!output)
> - return;
> -
> + output = xcalloc(1, sizeof *output);
> output->output =
> display_bind(desktop->display, id, &wl_output_interface, 2);
> output->server_output_id = id;
> diff --git a/clients/dnd.c b/clients/dnd.c
> index a463d6f..989e5ab 100644
> --- a/clients/dnd.c
> +++ b/clients/dnd.c
> @@ -92,15 +92,11 @@ item_create(struct display *display, int x, int y, int seed)
> struct item *item;
> struct timeval tv;
>
> - item = malloc(sizeof *item);
> - if (item == NULL)
> - return NULL;
> -
> -
> + item = xmalloc(sizeof *item);
> gettimeofday(&tv, NULL);
> item->seed = seed ? seed : tv.tv_usec;
> srandom(item->seed);
> -
> +
> const int petal_count = 3 + random() % 5;
> const double r1 = 20 + random() % 10;
> const double r2 = 5 + random() % 12;
> diff --git a/clients/editor.c b/clients/editor.c
> index 3b00833..6ed76d4 100644
> --- a/clients/editor.c
> +++ b/clients/editor.c
> @@ -682,7 +682,7 @@ text_entry_update_layout(struct text_entry *entry)
> (entry->preedit.text ? strlen(entry->preedit.text) : 0)));
>
> if (entry->preedit.text) {
> - text = malloc(strlen(entry->text) + strlen(entry->preedit.text) + 1);
> + text = xmalloc(strlen(entry->text) + strlen(entry->preedit.text) + 1);
> strncpy(text, entry->text, entry->cursor);
> strcpy(text + entry->cursor, entry->preedit.text);
> strcpy(text + entry->cursor + strlen(entry->preedit.text),
> @@ -764,7 +764,7 @@ static void
> text_entry_insert_at_cursor(struct text_entry *entry, const char *text,
> int32_t cursor, int32_t anchor)
> {
> - char *new_text = malloc(strlen(entry->text) + strlen(text) + 1);
> + char *new_text = xmalloc(strlen(entry->text) + strlen(text) + 1);
>
> strncpy(new_text, entry->text, entry->cursor);
> strcpy(new_text + entry->cursor, text);
> diff --git a/clients/eventdemo.c b/clients/eventdemo.c
> index 5ec6829..d12ec4b 100644
> --- a/clients/eventdemo.c
> +++ b/clients/eventdemo.c
> @@ -297,10 +297,7 @@ eventdemo_create(struct display *d)
> {
> struct eventdemo *e;
>
> - e = malloc(sizeof (struct eventdemo));
> - if(e == NULL)
> - return NULL;
> -
> + e = xmalloc(sizeof (struct eventdemo));
> e->window = window_create(d);
>
> if (noborder) {
> diff --git a/clients/fullscreen.c b/clients/fullscreen.c
> index fa8028a..ad7c703 100644
> --- a/clients/fullscreen.c
> +++ b/clients/fullscreen.c
> @@ -480,7 +480,7 @@ output_handler(struct output *output, void *data)
> if (fsout->output == output)
> return;
>
> - fsout = calloc(1, sizeof *fsout);
> + fsout = xcalloc(1, sizeof *fsout);
> fsout->output = output;
> wl_list_insert(&fullscreen->output_list, &fsout->link);
> }
> diff --git a/clients/gears.c b/clients/gears.c
> index 93a86b4..cfba30a 100644
> --- a/clients/gears.c
> +++ b/clients/gears.c
> @@ -401,7 +401,7 @@ gears_create(struct display *display)
> struct timeval tv;
> int i;
>
> - gears = zalloc(sizeof *gears);
> + gears = xzalloc(sizeof *gears);
> gears->d = display;
> gears->window = window_create(display);
> gears->widget = window_frame_create(gears->window, gears);
> diff --git a/clients/image.c b/clients/image.c
> index 054979d..cba68c5 100644
> --- a/clients/image.c
> +++ b/clients/image.c
> @@ -360,9 +360,7 @@ image_create(struct display *display, const char *filename,
> struct image *image;
> char *b, *copy, title[512];;
>
> - image = zalloc(sizeof *image);
> - if (image == NULL)
> - return image;
> + image = xzalloc(sizeof *image);
>
> copy = strdup(filename);
> b = basename(copy);
> diff --git a/clients/smoke.c b/clients/smoke.c
> index 5d976af..f03a6fa 100644
> --- a/clients/smoke.c
> +++ b/clients/smoke.c
> @@ -311,12 +311,12 @@ int main(int argc, char *argv[])
>
> smoke.current = 0;
> size = smoke.height * smoke.width;
> - smoke.b[0].d = calloc(size, sizeof(float));
> - smoke.b[0].u = calloc(size, sizeof(float));
> - smoke.b[0].v = calloc(size, sizeof(float));
> - smoke.b[1].d = calloc(size, sizeof(float));
> - smoke.b[1].u = calloc(size, sizeof(float));
> - smoke.b[1].v = calloc(size, sizeof(float));
> + smoke.b[0].d = xcalloc(size, sizeof(float));
> + smoke.b[0].u = xcalloc(size, sizeof(float));
> + smoke.b[0].v = xcalloc(size, sizeof(float));
> + smoke.b[1].d = xcalloc(size, sizeof(float));
> + smoke.b[1].u = xcalloc(size, sizeof(float));
> + smoke.b[1].v = xcalloc(size, sizeof(float));
>
> widget_set_motion_handler(smoke.widget, mouse_motion_handler);
> widget_set_touch_motion_handler(smoke.widget, touch_motion_handler);
> diff --git a/clients/subsurfaces.c b/clients/subsurfaces.c
> index 66a10f2..15af9aa 100644
> --- a/clients/subsurfaces.c
> +++ b/clients/subsurfaces.c
> @@ -212,9 +212,7 @@ egl_state_create(struct wl_display *display)
> EGLint major, minor, n;
> EGLBoolean ret;
>
> - egl = calloc(1, sizeof *egl);
> - assert(egl);
> -
> + egl = xcalloc(1, sizeof *egl);
> egl->dpy = eglGetDisplay(display);
> assert(egl->dpy);
>
> diff --git a/clients/wscreensaver-glue.c b/clients/wscreensaver-glue.c
> index 55d0a8c..f08d377 100644
> --- a/clients/wscreensaver-glue.c
> +++ b/clients/wscreensaver-glue.c
> @@ -109,9 +109,7 @@ XImage *xpm_to_ximage(char **xpm_data)
> int i;
> uint32_t ctable[256] = { 0 };
>
> - xi = malloc(sizeof *xi);
> - if (!xi)
> - return NULL;
> + xi = xmalloc(sizeof *xi);
> xi->data = NULL;
>
> if (sscanf(xpm_data[0], "%d %d %d %d", &xi->width,
> @@ -122,9 +120,7 @@ XImage *xpm_to_ximage(char **xpm_data)
> goto errout;
>
> xi->bytes_per_line = xi->width * sizeof(uint32_t);
> - xi->data = malloc(xi->height * xi->bytes_per_line);
> - if (!xi->data)
> - goto errout;
> + xi->data = xmalloc(xi->height * xi->bytes_per_line);
>
> for (i = 0; i < colors; ++i)
> read_xpm_color(ctable, xpm_data[i + 1]);
> diff --git a/clients/wscreensaver.c b/clients/wscreensaver.c
> index 47f6c8a..1070c07 100644
> --- a/clients/wscreensaver.c
> +++ b/clients/wscreensaver.c
> @@ -135,9 +135,7 @@ init_GL(struct ModeInfo *mi)
> struct wscreensaver *wscr = mi->priv;
> EGLContext *pctx;
>
> - pctx = malloc(sizeof *pctx);
> - if (!pctx)
> - return NULL;
> + pctx = xmalloc(sizeof *pctx);
>
> if (mi->eglctx != EGL_NO_CONTEXT) {
> fprintf(stderr, "%s: multiple GL contexts are not supported",
> @@ -177,9 +175,7 @@ create_wscreensaver_instance(struct wscreensaver *screensaver,
> struct ModeInfo *mi;
> struct rectangle drawarea;
>
> - mi = calloc(1, sizeof *mi);
> - if (!mi)
> - return NULL;
> + mi = xcalloc(1, sizeof *mi);
>
> if (demo_mode)
> mi->window = window_create(screensaver->display);
> @@ -256,11 +252,7 @@ init_wscreensaver(struct wscreensaver *wscr, struct display *display)
> wscr->plugin = plugins[0];
>
> size = sizeof(prefix) + strlen(wscr->plugin->name);
> - str = malloc(size);
> - if (!str) {
> - fprintf(stderr, "init: out of memory\n");
> - return -1;
> - }
> + str = xmalloc(size);
> snprintf(str, size, "%s%s", prefix, wscr->plugin->name);
> progname = str;
>
> --
> 1.7.9.5
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel
More information about the wayland-devel
mailing list