Skip to content

Commit ee849ec

Browse files
authored
Merge pull request #252 from thomas-brunel/fix/VLA_unsupported_C++
[LibWebsocketServer] Clang - Fix memory management for URI in eventCallback
2 parents f217547 + ed95425 commit ee849ec

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/websockets/libwebsockets/LibWebsocketServer.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,9 @@ int LibWebsocketServer::eventCallback(struct lws* wsi, enum lws_callback_reasons
312312
if (strcmp("websocket", static_cast<char*>(in)) == 0)
313313
{
314314
// Check URI
315-
#ifdef _MSC_VER
316-
char uri[512u];
317-
#else // _MSC_VER
318-
char uri[lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) + 1];
319-
#endif // _MSC_VER
320-
int uri_len = lws_hdr_copy(wsi, uri, sizeof(uri), WSI_TOKEN_GET_URI);
315+
const size_t uri_size = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) + 1;
316+
char* uri = new char[uri_size];
317+
int uri_len = lws_hdr_copy(wsi, uri, uri_size, WSI_TOKEN_GET_URI);
321318
if ((uri_len >= static_cast<int>(server->m_url.path().size())) &&
322319
(strncmp(uri, server->m_url.path().c_str(), server->m_url.path().size()) == 0))
323320
{
@@ -423,6 +420,7 @@ int LibWebsocketServer::eventCallback(struct lws* wsi, enum lws_callback_reasons
423420
lwsl_err("invalid URI\n");
424421
ret = -1;
425422
}
423+
delete[] uri;
426424
}
427425
else
428426
{
@@ -442,16 +440,14 @@ int LibWebsocketServer::eventCallback(struct lws* wsi, enum lws_callback_reasons
442440
server->m_clients[wsi] = client;
443441

444442
// Notify connection
445-
#ifdef _MSC_VER
446-
char uri[512u];
447-
#else // _MSC_VER
448-
char uri[lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) + 1];
449-
#endif // _MSC_VER
450-
if (lws_hdr_copy(wsi, uri, sizeof(uri), WSI_TOKEN_GET_URI) <= 0)
443+
const size_t uri_size = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) + 1;
444+
char* uri = new char[uri_size];
445+
if (lws_hdr_copy(wsi, uri, uri_size, WSI_TOKEN_GET_URI) <= 0)
451446
{
452447
uri[0] = 0;
453448
}
454449
server->m_listener->wsClientConnected(uri, client);
450+
delete[] uri;
455451
}
456452
break;
457453

0 commit comments

Comments
 (0)