tcp_transport: Fix Websocket transport initialization

In case of a failure in alocation of WS transport handle, parent leaks.
This commit is contained in:
Euripedes Rocha
2021-12-06 13:15:55 -03:00
parent 120b0ba2af
commit ae8eeaade9
2 changed files with 39 additions and 41 deletions

View File

@@ -1,16 +1,8 @@
// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD /*
// * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
// Licensed under the Apache License, Version 2.0 (the "License"); *
// you may not use this file except in compliance with the License. * SPDX-License-Identifier: Apache-2.0
// You may obtain a copy of the License at */
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -109,7 +101,9 @@ static char *trimwhitespace(const char *str)
char *end; char *end;
// Trim leading space // Trim leading space
while (isspace((unsigned char)*str)) str++; while (isspace((unsigned char)*str)) {
str++;
}
if (*str == 0) { if (*str == 0) {
return (char *)str; return (char *)str;
@@ -117,7 +111,9 @@ static char *trimwhitespace(const char *str)
// Trim trailing space // Trim trailing space
end = (char *)(str + strlen(str) - 1); end = (char *)(str + strlen(str) - 1);
while (end > str && isspace((unsigned char)*end)) end--; while (end > str && isspace((unsigned char)*end)) {
end--;
}
// Write new null terminator // Write new null terminator
*(end + 1) = 0; *(end + 1) = 0;
@@ -598,7 +594,10 @@ esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handl
return NULL; return NULL;
} }
transport_ws_t *ws = calloc(1, sizeof(transport_ws_t)); transport_ws_t *ws = calloc(1, sizeof(transport_ws_t));
ESP_TRANSPORT_MEM_CHECK(TAG, ws, return NULL); ESP_TRANSPORT_MEM_CHECK(TAG, ws, {
esp_transport_destroy(t);
return NULL;
});
ws->parent = parent_handle; ws->parent = parent_handle;
ws->path = strdup("/"); ws->path = strdup("/");

View File

@@ -2294,7 +2294,6 @@ components/tcp_transport/test/test_transport_fixtures.c
components/tcp_transport/transport.c components/tcp_transport/transport.c
components/tcp_transport/transport_ssl.c components/tcp_transport/transport_ssl.c
components/tcp_transport/transport_utils.c components/tcp_transport/transport_utils.c
components/tcp_transport/transport_ws.c
components/tcpip_adapter/include/tcpip_adapter.h components/tcpip_adapter/include/tcpip_adapter.h
components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h
components/tcpip_adapter/include/tcpip_adapter_types.h components/tcpip_adapter/include/tcpip_adapter_types.h