Skip to content

Commit

Permalink
imc: fix a chat room related crash and DB reload problems (GH #1855)
Browse files Browse the repository at this point in the history
- 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.

(cherry picked from commit 669bb9a)
  • Loading branch information
janakj authored and henningw committed Feb 17, 2019
1 parent 878c23c commit c4549a0
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/modules/imc/imc.c
Expand Up @@ -680,9 +680,9 @@ static void destroy(void)
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;
}
LM_DBG("room %d %.*s\n", i, irp->name.len, irp->name.s);
Expand All @@ -700,9 +700,9 @@ static void destroy(void)
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;
}
member = member->next;
Expand All @@ -722,6 +722,7 @@ static void imc_rpc_list_rooms(rpc_t* rpc, void* ctx)
int i;
imc_room_p irp = NULL;
void *vh;
static str unknown = STR_STATIC_INIT("");

for(i=0; i<imc_hash_size; i++)
{
Expand All @@ -736,7 +737,7 @@ static void imc_rpc_list_rooms(rpc_t* rpc, void* ctx)
rpc->struct_add(vh, "SdS",
"room", &irp->uri,
"members", irp->nr_of_members,
"owner", &irp->members->uri);
"owner", (irp->nr_of_members > 0) ? &irp->members->uri : &unknown);

irp = irp->next;
}
Expand Down

0 comments on commit c4549a0

Please sign in to comment.