Browse Source

imc: fix a chat room related crash and DB reload problems (GH #1855)

- Avoid crash in case a chat room has no members.
  The imc module may encounter chat rooms that, for one reason or another,
  have no members. In that case, we cannot use the URI of the first member
  as the owner URI. This happens, for example, when the destroy function
  fails to save chat room members into the database.
- When storing data in database, use replace instead of insert.
  The insert statement would fail with an index violation if the
  database already contains a matching record. That would happen, for
  example, if some of the records being saved in mod_destroy were
  re-loaded from the database on server start.
Jan Janak 6 years ago
parent
commit
669bb9a077
1 changed files with 6 additions and 5 deletions
  1. 6 5
      src/modules/imc/imc.c

+ 6 - 5
src/modules/imc/imc.c

@@ -674,9 +674,9 @@ static void destroy(void)
 				return;
 				return;
 			}
 			}
 
 
-			if(imc_dbf.insert(imc_db, rq_cols, rq_vals, 3)<0)
+			if(imc_dbf.replace(imc_db, rq_cols, rq_vals, 3, 2, 0)<0)
 			{
 			{
-				LM_ERR("failed to insert into table imc_rooms\n");
+				LM_ERR("failed to replace into table imc_rooms\n");
 				return;
 				return;
 			}
 			}
 			LM_DBG("room %d %.*s\n", i, irp->name.len, irp->name.s);
 			LM_DBG("room %d %.*s\n", i, irp->name.len, irp->name.s);
@@ -694,9 +694,9 @@ static void destroy(void)
 					return;
 					return;
 				}
 				}
 
 
-				if(imc_dbf.insert(imc_db, mq_cols, mq_vals, 4)<0)
+				if(imc_dbf.replace(imc_db, mq_cols, mq_vals, 4, 2, 0)<0)
 				{
 				{
-					LM_ERR("failed to insert  into table imc_rooms\n");
+					LM_ERR("failed to replace  into table imc_rooms\n");
 					return;
 					return;
 				}
 				}
 				member = member->next;
 				member = member->next;
@@ -716,6 +716,7 @@ static void  imc_rpc_list_rooms(rpc_t* rpc, void* ctx)
 	int i;
 	int i;
 	imc_room_p irp = NULL;
 	imc_room_p irp = NULL;
 	void *vh;
 	void *vh;
+	static str unknown = STR_STATIC_INIT("");
 
 
 	for(i=0; i<imc_hash_size; i++)
 	for(i=0; i<imc_hash_size; i++)
 	{
 	{
@@ -730,7 +731,7 @@ static void  imc_rpc_list_rooms(rpc_t* rpc, void* ctx)
 			rpc->struct_add(vh, "SdS",
 			rpc->struct_add(vh, "SdS",
 					"room", &irp->uri,
 					"room", &irp->uri,
 					"members", irp->nr_of_members,
 					"members", irp->nr_of_members,
-					"owner", &irp->members->uri);
+					"owner", (irp->nr_of_members > 0) ? &irp->members->uri : &unknown);
 
 
 			irp = irp->next;
 			irp = irp->next;
 		}
 		}