Skip to content

Conversation

@aklomp
Copy link

@aklomp aklomp commented May 3, 2014

The proper idiom for calculating an image's memory size from a pointer using sizeof() is:

ptr = malloc(width * height * sizeof(*ptr));

The sizeof() dereferences the pointer's type, and allocates enough memory to store an image's worth of elements of that type. However, Motion was occasionally using this pattern:

ptr = malloc(width * height * sizeof(ptr));

Not dereferencing the pointer, this uses the storage size of the pointer itself, not its target type, for the calculation. While this is probably not immediately harmful since pointers tend to be large, it is incorrect. This pull request fixes a number of these issues.

aklomp added 2 commits May 3, 2014 20:38
The proper idiom is:

  memset(ptr, val, sizeof(*ptr));

These files were using:

  memset(ptr, val, sizeof(ptr));
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.
@Mr-Dave
Copy link

Mr-Dave commented Sep 7, 2014

Rather than rebase, I have rolled back the affected files and removed your changes from my fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants