Skip to content

Commit

Permalink
ws-chat-server: avoid SIGSEGV in case of evws_new_session() failure
Browse files Browse the repository at this point in the history
  • Loading branch information
azat committed Nov 12, 2022
1 parent dda05f4 commit 8482e22
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sample/ws-chat-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,19 @@ on_ws(struct evhttp_request *req, void *arg)
socklen_t len;

client = calloc(sizeof(*client), 1);
addr2str((struct sockaddr *)&addr, client->name, sizeof(client->name));
client->evws = evws_new_session(req, on_msg_cb, client, 0);
if (!client->evws) {
free(client);
log_d("Failed to create session (for %s)\n", client->name);
return;
}

fd = bufferevent_getfd(evws_connection_get_bufferevent(client->evws));

len = sizeof(addr);
getpeername(fd, (struct sockaddr *)&addr, &len);

addr2str((struct sockaddr *)&addr, client->name, sizeof(client->name));
log_d("New client joined from %s\n", client->name);

evws_connection_set_closecb(client->evws, on_close_cb, client);
Expand Down

0 comments on commit 8482e22

Please sign in to comment.