[C] "invalid next size (fast)" error
Hello all.
I don't consider myself a newbie programmer but this has me stumped. I've written code to generate randomized Vigenere ciphers, just for fun.
However I'm having some trouble with a free() call after a malloc (line 8 in main() ). The code is here. Whenever I run it, it generates and prints the cipher fine, but when it gets to the free( array ); call I get an error:
*** glibc detected *** ./test: free(): invalid next size (fast): 0x0000000000be2010 ***
...followed by a stack trace. It's the only free() call anywhere in my code. It's simple, non-threaded stuff but I can't figure out why there's an error here. If I comment out the free() call it compiles and runs fine... but doesn't this constitute a memory leak? I know once the process is done Linux probably scraps the memory anyway but still... it bugs me that I can't free my own malloc.
I should point out I'm compiling with gcc 4.7.2 on a Linux 3.5.0 kernel and compiled against glibc 2.15 (I think, not sure how to verify glibc version).
gcc --std=gnu99 -o test vigenere.c
Any help is appreciated, thanks all!
Posted May 7, 2013, 1:31 AM UTC
That error means you have corrupted the internal bookkeeping data structures that
malloc()andfree()maintain behind the scenes to track available regions of memory. A common cause for that is writing outside the bounds of the memory block you requested, since those data structures are typically stored in the parts surrounding the returned allocated block. And indeed that's the case here due to this:You are missing a
sizeof(u_int16_t)term in the size calculation, which means you're allocating only 7*7 bytes but you're treating the result as if you'd allocated 7*7*2 bytes, and the result is a severe buffer overflow.That makes perfect sense! Thank you very much!