bWolfie 138 Posted August 6, 2022 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); Quote Share this post Link to post Share on other sites
0 Asheraf 123 Posted August 6, 2022 Either add a aFree(item_name); or better use a stack allocation instead of malloc char item_name[ITEM_NAME_LENGTH]; 1 bWolfie reacted to this Quote Share this post Link to post Share on other sites
0 4144 364 Posted August 6, 2022 after sprintf, add aFree(item_name); and why memcpy? server may crash if name too short. probably better strncpy or safestrncpy 1 bWolfie reacted to this Quote Share this post Link to post Share on other sites
0 bWolfie 138 Posted August 8, 2022 (edited) 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 August 9, 2022 by bWolfie Quote Share this post Link to post Share on other sites
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?
Share this post
Link to post
Share on other sites