Skip to content

Commit

Permalink
bugfix: motion.c: calculate proper allocation size
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aklomp committed Oct 6, 2014
1 parent 95c6a9f commit d0cc56c
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions motion.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit d0cc56c

Please sign in to comment.