Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memchk.c - sometimes malloc doesn't return aligned pointer #4

Open
GoogleCodeExporter opened this issue Mar 23, 2015 · 1 comment
Open

Comments

@GoogleCodeExporter
Copy link

I'm trying to use the memchk.c library to check some of my code, but I was 
getting an assertion error on FREE'ing because this test was failing:

    ((unsigned long)ptr) % (sizeof (union align)) != 0

Upon review, it looks like it is a result of malloc not returning a pointer 
that is aligned according to `sizeof(union align)`.  For example, I added the 
following check to the `dalloc` function:


    size_t alignment = sizeof(union align);
    assert((alignment & (alignment - 1)) == 0); // make sure alignment is a power of 2
    void *nptr = (void *)(((unsigned long)ptr + (alignment-1)) & ~ (size_t)(alignment-1));
    assert(ptr == nptr);

And my code would fail, but I would expect that nptr should equal ptr?  If the 
code was modified to change:

    avail->ptr  = ptr;

to:
    avail->ptr  = (void *)(((unsigned long)ptr + (alignment-1)) & ~ (size_t)(alignment-1));

I don't think it would have any affect other than to assure that the memory is 
aligned along the boundaries we believe it is.

Original issue reported on code.google.com by kevin.t....@gmail.com on 26 Jul 2011 at 4:22

@GoogleCodeExporter
Copy link
Author

Sorry, also realized you must change avail->size:

    avail->size = size - ((ptr == nptr) ? 0 : alignment);

Original comment by kevin.t....@gmail.com on 26 Jul 2011 at 5:32

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

No branches or pull requests

1 participant