kamby
kamby copied to clipboard
Buffer overflow on string allocation
Hi!
There are many places (e.g. here) where the code copies a string by computing strlen() of a string, mallocs that size, and copies the contents with strcpy. However, this does not account for the size of the \0
at the end of the string. The code should malloc one more than the string length.
Since this is a recurring operation, it would probably be best to write a dedicated function to allocate and copy a string, to use wherever needed. On Unix, there is strdup()
, but if you want to keep the code more portable, you can easily make a small function to do that.
Recommendation for Henrique: run your program through valgrind. You will get hundreds of warnings, and crashes also (in linux anyhow).
Thank you for tips. I ran valgrind and improved some issues. Need more work, but the specific "string overflow" must have been fixed.