Menu

#8 memory allocation bugs...

open
nobody
program (2)
5
2000-11-27
2000-11-27
No

I found a bug trying to install cvsd on my computer. I can't log in because of this message :

"can't stat /usr/local/cvshome/cvsd-root^Y"

The "^Y" surprised me, and the reason is below.

At some places, you allocated memory to copy string parameter in memory, using strlen(parameter), like this :

 cvsd\_root = \(char \*\)malloc\(strlen\(optarg\) \* sizeof\(char\)\);

But, you need strlen(optarg) bytes + 1, you have to allocate place to store the '\0' in C strings.

Here is a diff between cvsd.c version 0.8b3 and my corrections :

359c359
< cvsd_root = (char *)malloc(strlen(optarg) * sizeof(char));
---
> cvsd_root = (char *)malloc((strlen(optarg)+1) * sizeof(char));
363c363
< config_file = (char *)malloc(strlen(optarg) * sizeof(char));
---
> config_file = (char *)malloc((strlen(optarg)+1) * sizeof(char));
390c390
< config_file = (char *)malloc(strlen(CONFIG_FILE) * sizeof(char));
---
> config_file = (char *)malloc((strlen(CONFIG_FILE)+1) * sizeof(char));
470c470
< ctemp = (char *)malloc(strlen(buffer)+1 * sizeof(char));
---
> ctemp = (char *)malloc((strlen(buffer)+1) * sizeof(char));

Hope this helps.

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.