I have three lines (version) of a linux product. V1 works fine in the customer. V2 and V3 crashed and the fix seems to be a memset call afte开发者_JS百科r a malloc call.
What is the deeper explanation on this topic? Why memset resolved the issue?
My guess without a code example is that you were operating on the buffer or struct you malloc'd with assumptions that its contents would be initialized with certain default values. Malloc doesn't initialize the memory it hands back, so unless you memset or use some other initialization, the values in that memory could be anything, and therefore, if you're trying to check a pointer assuming it'd be NULL or that an int will be zero, you can't make that assumption without initializing the memory first.
Maybe because there is a wrong assumtion that the allocated buffer is zeroed. So for example if the buffer contains a string and is printed somewhere before initialize, it can result in an access violation. Zeroing the buffer would fix such an issue.
精彩评论