From 370d43d1b33f31ff29e41193c564c2b4e45db329 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Tue, 25 Oct 2016 16:43:33 -0600 Subject: [PATCH] gp2y0a: test for NULL alloc and clear allocated memory Signed-off-by: Jon Trulson --- src/gp2y0a/gp2y0a.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/gp2y0a/gp2y0a.c b/src/gp2y0a/gp2y0a.c index 0ab080fb..318da1d9 100644 --- a/src/gp2y0a/gp2y0a.c +++ b/src/gp2y0a/gp2y0a.c @@ -33,8 +33,17 @@ gp2y0a_context gp2y0a_init(uint8_t pin, float a_ref){ return NULL; } - gp2y0a_context dev = (gp2y0a_context) malloc(sizeof(struct _gp2y0a_context)); + gp2y0a_context dev = + (gp2y0a_context) malloc(sizeof(struct _gp2y0a_context)); + if (!dev) + { + printf("%s: device context allocation failed (%d).\n", + __FUNCTION__, mraa_rv); + return NULL; + } + + memset((void *)dev, 0, sizeof(struct _gp2y0a_context)); dev->pin = pin; dev->aio = mraa_aio_init(dev->pin); if(dev->aio == NULL){