Browse Source

Fix ASAN crashes and LSAN leaks (#7649)

Vincent Stumpf 2 years ago
parent
commit
819b7cb361
8 changed files with 15 additions and 6 deletions
  1. 2 2
      src/char/char_clif.cpp
  2. 1 1
      src/char/char_mapif.cpp
  3. 0 1
      src/map/clif.cpp
  4. 3 0
      src/map/mob.cpp
  5. 2 0
      src/map/npc.cpp
  6. 3 1
      src/map/pet.cpp
  7. 1 1
      src/map/script.cpp
  8. 3 0
      src/map/unit.cpp

+ 2 - 2
src/char/char_clif.cpp

@@ -920,8 +920,8 @@ int chclif_parse_select_accessible_map( int fd, struct char_session_data* sd, ui
 	// FIXME: is this case even possible? [ultramage]
 	// FIXME: is this case even possible? [ultramage]
 	if( ( map_fd = map_server[mapserver].fd ) < 1 || session[map_fd] == nullptr ){
 	if( ( map_fd = map_server[mapserver].fd ) < 1 || session[map_fd] == nullptr ){
 		ShowError( "parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, mapserver );
 		ShowError( "parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, mapserver );
+		map_server[mapserver] = {};
 		map_server[mapserver].fd = -1;
 		map_server[mapserver].fd = -1;
-		memset( &map_server[mapserver], 0, sizeof( struct mmo_map_server ) );
 		chclif_send_auth_result( fd, 1 ); // Send server closed.
 		chclif_send_auth_result( fd, 1 ); // Send server closed.
 		return 1;
 		return 1;
 	}
 	}
@@ -1090,8 +1090,8 @@ int chclif_parse_charselect(int fd, struct char_session_data* sd,uint32 ipl){
 		if ((map_fd = map_server[i].fd) < 1 || session[map_fd] == NULL)
 		if ((map_fd = map_server[i].fd) < 1 || session[map_fd] == NULL)
 		{
 		{
 			ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
 			ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
+			map_server[i] = {};
 			map_server[i].fd = -1;
 			map_server[i].fd = -1;
-			memset(&map_server[i], 0, sizeof(struct mmo_map_server));
 			chclif_send_auth_result(fd,1);  //Send server closed.
 			chclif_send_auth_result(fd,1);  //Send server closed.
 			return 1;
 			return 1;
 		}
 		}

+ 1 - 1
src/char/char_mapif.cpp

@@ -1480,7 +1480,7 @@ int chmapif_init(int fd){
  * @param id: id of map-serv (should be >0, FIXME)
  * @param id: id of map-serv (should be >0, FIXME)
  */
  */
 void chmapif_server_init(int id) {
 void chmapif_server_init(int id) {
-	memset(&map_server[id], 0, sizeof(map_server[id]));
+	map_server[id] = {};
 	map_server[id].fd = -1;
 	map_server[id].fd = -1;
 }
 }
 
 

+ 0 - 1
src/map/clif.cpp

@@ -10812,7 +10812,6 @@ void clif_parse_WantToConnection(int fd, map_session_data* sd)
 	}
 	}
 
 
 	CREATE(sd, TBL_PC, 1);
 	CREATE(sd, TBL_PC, 1);
-	// placement new
 	new(sd) map_session_data();
 	new(sd) map_session_data();
 	sd->fd = fd;
 	sd->fd = fd;
 #ifdef PACKET_OBFUSCATION
 #ifdef PACKET_OBFUSCATION

+ 3 - 0
src/map/mob.cpp

@@ -453,6 +453,7 @@ int mob_parse_dataset(struct spawn_data *data)
 struct mob_data* mob_spawn_dataset(struct spawn_data *data)
 struct mob_data* mob_spawn_dataset(struct spawn_data *data)
 {
 {
 	struct mob_data *md = (struct mob_data*)aCalloc(1, sizeof(struct mob_data));
 	struct mob_data *md = (struct mob_data*)aCalloc(1, sizeof(struct mob_data));
+	new(md) mob_data();
 	md->bl.id= npc_get_new_npc_id();
 	md->bl.id= npc_get_new_npc_id();
 	md->bl.type = BL_MOB;
 	md->bl.type = BL_MOB;
 	md->bl.m = data->m;
 	md->bl.m = data->m;
@@ -687,6 +688,7 @@ int mob_once_spawn(map_session_data* sd, int16 m, int16 x, int16 y, const char*
 			if (gc)
 			if (gc)
 			{
 			{
 				md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
 				md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
+				new(md->guardian_data) guardian_data();
 				md->guardian_data->castle = gc;
 				md->guardian_data->castle = gc;
 				md->guardian_data->number = MAX_GUARDIANS;
 				md->guardian_data->number = MAX_GUARDIANS;
 				md->guardian_data->guild_id = gc->guild_id;
 				md->guardian_data->guild_id = gc->guild_id;
@@ -890,6 +892,7 @@ int mob_spawn_guardian(const char* mapname, int16 x, int16 y, const char* mobnam
 
 
 	md = mob_spawn_dataset(&data);
 	md = mob_spawn_dataset(&data);
 	md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
 	md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
+	new (md->guardian_data) guardian_data();
 	md->guardian_data->number = guardian;
 	md->guardian_data->number = guardian;
 	md->guardian_data->guild_id = gc->guild_id;
 	md->guardian_data->guild_id = gc->guild_id;
 	md->guardian_data->castle = gc;
 	md->guardian_data->castle = gc;

+ 2 - 0
src/map/npc.cpp

@@ -3552,6 +3552,7 @@ int npc_unload(struct npc_data* nd, bool single) {
 		}
 		}
 	}
 	}
 
 
+	nd->~npc_data();
 	aFree(nd);
 	aFree(nd);
 
 
 	return 0;
 	return 0;
@@ -4136,6 +4137,7 @@ static const char* npc_parse_shop(char* w1, char* w2, char* w3, char* w4, const
 	}
 	}
 	if( nd->u.shop.count == 0 ) {
 	if( nd->u.shop.count == 0 ) {
 		ShowWarning("npc_parse_shop: Ignoring empty shop in file '%s', line '%d'.\n", filepath, strline(buffer,start-buffer));
 		ShowWarning("npc_parse_shop: Ignoring empty shop in file '%s', line '%d'.\n", filepath, strline(buffer,start-buffer));
+		nd->~npc_data();
 		aFree(nd);
 		aFree(nd);
 		return strchr(start,'\n');// continue
 		return strchr(start,'\n');// continue
 	}
 	}

+ 3 - 1
src/map/pet.cpp

@@ -1022,7 +1022,9 @@ bool pet_data_init(map_session_data *sd, struct s_pet *pet)
 		return false;
 		return false;
 	}
 	}
 
 
-	sd->pd = pd = (struct pet_data *)aCalloc(1,sizeof(struct pet_data));
+	pd = (struct pet_data *)aCalloc(1,sizeof(struct pet_data));
+	new(pd) pet_data();
+	sd->pd = pd;
 	pd->bl.type = BL_PET;
 	pd->bl.type = BL_PET;
 	pd->bl.id = npc_get_new_npc_id();
 	pd->bl.id = npc_get_new_npc_id();
 
 

+ 1 - 1
src/map/script.cpp

@@ -18020,7 +18020,7 @@ BUILDIN_FUNC(npcshopdelitem)
 		ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
 		ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
 		if( n < size ) {
 		if( n < size ) {
 			if (n+1 != size)
 			if (n+1 != size)
-				memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
+				memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-(n + 1)));
 #if PACKETVER >= 20131223
 #if PACKETVER >= 20131223
 			if (nd->subtype == NPCTYPE_MARKETSHOP)
 			if (nd->subtype == NPCTYPE_MARKETSHOP)
 				npc_market_delfromsql_(nd->exname, nameid, false);
 				npc_market_delfromsql_(nd->exname, nameid, false);

+ 3 - 0
src/map/unit.cpp

@@ -3501,6 +3501,7 @@ int unit_free(struct block_list *bl, clr_type clrtype)
 
 
 			skill_clear_unitgroup(bl);
 			skill_clear_unitgroup(bl);
 			status_change_clear(bl,1);
 			status_change_clear(bl,1);
+			pd->~pet_data();
 			break;
 			break;
 		}
 		}
 		case BL_MOB: {
 		case BL_MOB: {
@@ -3536,6 +3537,7 @@ int unit_free(struct block_list *bl, clr_type clrtype)
 						gc->temp_guardians[i] = 0;
 						gc->temp_guardians[i] = 0;
 				}
 				}
 
 
+				md->guardian_data->~guardian_data();
 				aFree(md->guardian_data);
 				aFree(md->guardian_data);
 				md->guardian_data = NULL;
 				md->guardian_data = NULL;
 			}
 			}
@@ -3564,6 +3566,7 @@ int unit_free(struct block_list *bl, clr_type clrtype)
 
 
 			if( md->tomb_nid )
 			if( md->tomb_nid )
 				mvptomb_destroy(md);
 				mvptomb_destroy(md);
+			md->~mob_data();
 			break;
 			break;
 		}
 		}
 		case BL_HOM:
 		case BL_HOM: