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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...

Important Information

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