From 8e486bc068fc03d6bc28a95373c47f159e3b258e Mon Sep 17 00:00:00 2001 From: Alfred Klomp Date: Fri, 2 May 2014 12:31:36 +0200 Subject: [PATCH 1/2] Bugfix: calculate proper size for memset() The proper idiom is: memset(ptr, val, sizeof(*ptr)); These files were using: memset(ptr, val, sizeof(ptr)); --- alg.c | 4 ++-- motion.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/alg.c b/alg.c index 93c260f..343ff47 100644 --- a/alg.c +++ b/alg.c @@ -534,7 +534,7 @@ static int alg_labeling(struct context *cnt) imgs->labels_above = 0; /* Init: 0 means no label set / not checked. */ - memset(labels, 0, width * height * sizeof(labels)); + memset(labels, 0, width * height * sizeof(*labels)); pixelpos = 0; for (iy = 0; iy < height - 1; iy++) { @@ -1363,6 +1363,6 @@ void alg_update_reference_frame(struct context *cnt, int action) /* Copy fresh image */ memcpy(cnt->imgs.ref, cnt->imgs.image_virgin, cnt->imgs.size); /* Reset static objects */ - memset(cnt->imgs.ref_dyn, 0, cnt->imgs.motionsize * sizeof(cnt->imgs.ref_dyn)); + memset(cnt->imgs.ref_dyn, 0, cnt->imgs.motionsize * sizeof(*cnt->imgs.ref_dyn)); } } diff --git a/motion.c b/motion.c index 5666770..66aff3f 100644 --- a/motion.c +++ b/motion.c @@ -917,7 +917,7 @@ static int motion_init(struct context *cnt) /* Always initialize smart_mask - someone could turn it on later... */ memset(cnt->imgs.smartmask, 0, cnt->imgs.motionsize); memset(cnt->imgs.smartmask_final, 255, cnt->imgs.motionsize); - memset(cnt->imgs.smartmask_buffer, 0, cnt->imgs.motionsize*sizeof(cnt->imgs.smartmask_buffer)); + memset(cnt->imgs.smartmask_buffer, 0, cnt->imgs.motionsize * sizeof(*cnt->imgs.smartmask_buffer)); /* Set noise level */ cnt->noise = cnt->conf.noise; From 145201a1c2b24bff88ecd775bd79d44f8b813e92 Mon Sep 17 00:00:00 2001 From: Alfred Klomp Date: Fri, 2 May 2014 10:49:50 +0200 Subject: [PATCH 2/2] bugfix: motion.c: calculate proper allocation size The proper idiom for calculating the size for memory allocation is: ptr = malloc(sizeof(*ptr)); The sizeof() dereferences the pointer's type, and allocates enough memory to store an instance of that type. motion.c was using this idiom: ptr = malloc(sizeof(ptr)); This is incorrect, but thankfully fairly harmless in practice since the pointer type is usually quite large. Change this to the proper idiom. --- motion.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/motion.c b/motion.c index 66aff3f..dd4ac2a 100644 --- a/motion.c +++ b/motion.c @@ -723,13 +723,13 @@ static int motion_init(struct context *cnt) memset(cnt->imgs.out, 0, cnt->imgs.size); /* contains the moving objects of ref. frame */ - cnt->imgs.ref_dyn = mymalloc(cnt->imgs.motionsize * sizeof(cnt->imgs.ref_dyn)); + cnt->imgs.ref_dyn = mymalloc(cnt->imgs.motionsize * sizeof(*cnt->imgs.ref_dyn)); cnt->imgs.image_virgin = mymalloc(cnt->imgs.size); cnt->imgs.smartmask = mymalloc(cnt->imgs.motionsize); cnt->imgs.smartmask_final = mymalloc(cnt->imgs.motionsize); - cnt->imgs.smartmask_buffer = mymalloc(cnt->imgs.motionsize * sizeof(cnt->imgs.smartmask_buffer)); - cnt->imgs.labels = mymalloc(cnt->imgs.motionsize * sizeof(cnt->imgs.labels)); - cnt->imgs.labelsize = mymalloc((cnt->imgs.motionsize/2+1) * sizeof(cnt->imgs.labelsize)); + cnt->imgs.smartmask_buffer = mymalloc(cnt->imgs.motionsize * sizeof(*cnt->imgs.smartmask_buffer)); + cnt->imgs.labels = mymalloc(cnt->imgs.motionsize * sizeof(*cnt->imgs.labels)); + cnt->imgs.labelsize = mymalloc((cnt->imgs.motionsize/2+1) * sizeof(*cnt->imgs.labelsize)); /* Set output picture type */ if (!strcmp(cnt->conf.picture_type, "ppm"))