Elliott C. Back: Technology FTW!

A Desperate Tale of Malloc

Posted in Code by Elliott Back on November 28th, 2004.

Actually, I was using calloc, but that doesn’t matter. I still managed to corrupt the heap, leading to an access-violation exception on the next malloc. Gee, I wish VS tell me there was heap damage at least. Here is the offending c code–let it be a lesson to you:

I was using a constant length string for serialization, so I memcpy 64 characters into the file->name field every time. But, I don’t malloc 64 characters–sometimes, if the filename is smaller, I’d only malloc space for 3 or 4 characters. The memcpy would then write into heap memory it shouldn’t, corrupt the heap, and cause the next malloc call to fail. C is tricky–be careful!

This entry was posted on Sunday, November 28th, 2004 at 3:05 pm and is tagged with heap memory, violation exception, malloc, access violation, c code, serialization, memcpy. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

 

Trackbacks

(Trackback URL)

close Reblog this comment
blog comments powered by Disqus