Jump to content
  • 0
Sign in to follow this  
bWolfie

Help with map server leak

Question

I made this command to retrieve all of an item from storage.

 

This line char *item_name =(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char)); causes memory leak. How to fix it?

 


	struct item_data *i_data = itemdb->exists(nameid);

	if (i_data == NULL) {
		clif->message(fd, "Invalid Item ID.");
		return false;
	}

	char *item_name =(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
	memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);

	char output[100];
	sprintf(output, "Retrieved %d '%s' from storage.", i, item_name);

 

 

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

after sprintf, add

aFree(item_name);

and why memcpy? server may crash if name too short. probably better strncpy or safestrncpy

Share this post


Link to post
Share on other sites
  • 0

Thanks for the responses. I don't know why I use memcpy, not so advanced on coding. I just copied an existing structure I found in source code.

 

I used safestrncpy in new version and no leaks.

	char item_name[ITEM_NAME_LENGTH], output[100];
	safestrncpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
	sprintf(output, "Retrieved %d '%s' from storage.", i, item_name);

 

Edited by bWolfie

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.