Reply to post: Re: This bug is everywhere else too

After reportedly dragging its feet, BlackBerry admits, yes, QNX in cars, equipment suffers from BadAlloc bug

MacroRodent Silver badge

Re: This bug is everywhere else too

A calloc that tries to make assumptions about alignment based on the inputs is just an invalid implementation. The C standard says "The calloc function allocates space for an array of nmemb objects, each of whose size is size. The space is initialized to all bits zero." It cannot assume the size is not really size, but something larger. Typically the second parameter is sizeof(something), and if "something" has alignment requirements in an array, sizeof already takes care of rounding up.

All implementations I have seen just multiply the numbers together and call malloc with them (or call the same internal function malloc calls). The only gotcha is if the implementer does not realize the multiplication can overflow and wrap to something less than either size parameter. Sadly, that is how many callocs are...

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon

Biting the hand that feeds IT © 1998–2021