From b0a54f373fd457411355f98595e123e6878379b4 Mon Sep 17 00:00:00 2001 From: Li Bo Date: Fri, 27 Sep 2024 18:29:18 +0800 Subject: [PATCH] fix(openai): incorrect free json payload --- README.md | 2 +- README_CN.md | 2 +- components/openai/CHANGELOG.md | 6 +++++ components/openai/OpenAI.c | 24 ++++++++++--------- components/openai/idf_component.yml | 2 +- .../openai/test_apps/main/test_openai.c | 2 +- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c0c27f142..0ba37bf7c 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ The registered components in ESP-IoT-Solution are listed below: | [led_indicator](https://components.espressif.com/components/espressif/led_indicator/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/led_indicator/versions/1.0.0) | | [lightbulb_driver](https://components.espressif.com/components/espressif/lightbulb_driver/versions/1.3.3) | [![1.3.3](https://img.shields.io/badge/Stable-1.3.3-blue)](https://components.espressif.com/components/espressif/lightbulb_driver/versions/1.3.3) | | [ntc_driver](https://components.espressif.com/components/espressif/ntc_driver/versions/1.1.0) | [![1.1.0](https://img.shields.io/badge/Stable-1.1.0-blue)](https://components.espressif.com/components/espressif/ntc_driver/versions/1.1.0) | -| [openai](https://components.espressif.com/components/espressif/openai/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/openai/versions/1.0.0) | +| [openai](https://components.espressif.com/components/espressif/openai/versions/1.0.1) | [![1.0.1](https://img.shields.io/badge/Stable-1.0.1-blue)](https://components.espressif.com/components/espressif/openai/versions/1.0.1) | | [pwm_audio](https://components.espressif.com/components/espressif/pwm_audio/versions/1.1.2) | [![1.1.2](https://img.shields.io/badge/Stable-1.1.2-blue)](https://components.espressif.com/components/espressif/pwm_audio/versions/1.1.2) | | [spi_bus](https://components.espressif.com/components/espressif/spi_bus/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/spi_bus/versions/1.0.0) | | [touch_proximity_sensor](https://components.espressif.com/components/espressif/touch_proximity_sensor/versions/0.1.2) | [![0.1.2](https://img.shields.io/badge/Beta-0.1.2-yellow)](https://components.espressif.com/components/espressif/touch_proximity_sensor/versions/0.1.2) | diff --git a/README_CN.md b/README_CN.md index 317ca8143..2b47331c3 100644 --- a/README_CN.md +++ b/README_CN.md @@ -110,7 +110,7 @@ ESP-IoT-Solution 中注册的组件如下: | [led_indicator](https://components.espressif.com/components/espressif/led_indicator/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/led_indicator/versions/1.0.0) | | [lightbulb_driver](https://components.espressif.com/components/espressif/lightbulb_driver/versions/1.3.3) | [![1.3.3](https://img.shields.io/badge/Stable-1.3.3-blue)](https://components.espressif.com/components/espressif/lightbulb_driver/versions/1.3.3) | | [ntc_driver](https://components.espressif.com/components/espressif/ntc_driver/versions/1.1.0) | [![1.1.0](https://img.shields.io/badge/Stable-1.1.0-blue)](https://components.espressif.com/components/espressif/ntc_driver/versions/1.1.0) | -| [openai](https://components.espressif.com/components/espressif/openai/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/openai/versions/1.0.0) | +| [openai](https://components.espressif.com/components/espressif/openai/versions/1.0.1) | [![1.0.1](https://img.shields.io/badge/Stable-1.0.1-blue)](https://components.espressif.com/components/espressif/openai/versions/1.0.1) | | [pwm_audio](https://components.espressif.com/components/espressif/pwm_audio/versions/1.1.2) | [![1.1.2](https://img.shields.io/badge/Stable-1.1.2-blue)](https://components.espressif.com/components/espressif/pwm_audio/versions/1.1.2) | | [spi_bus](https://components.espressif.com/components/espressif/spi_bus/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/spi_bus/versions/1.0.0) | | [touch_proximity_sensor](https://components.espressif.com/components/espressif/touch_proximity_sensor/versions/0.1.2) | [![0.1.2](https://img.shields.io/badge/Beta-0.1.2-yellow)](https://components.espressif.com/components/espressif/touch_proximity_sensor/versions/0.1.2) | diff --git a/components/openai/CHANGELOG.md b/components/openai/CHANGELOG.md index 2a1a094af..fc3fe8e3c 100644 --- a/components/openai/CHANGELOG.md +++ b/components/openai/CHANGELOG.md @@ -1,5 +1,11 @@ # ChangeLog +## v1.0.1 - 2024-11-5 + +### Bug Fixes: + +* Fix incorrect free of cjson object's payload memory + ## v1.0.0 - 2024-5-15 ### Break Changes: diff --git a/components/openai/OpenAI.c b/components/openai/OpenAI.c index b6988e193..04cd9d735 100644 --- a/components/openai/OpenAI.c +++ b/components/openai/OpenAI.c @@ -1,4 +1,4 @@ -/* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +/* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -587,7 +587,6 @@ static OpenAI_StringResponse_t *OpenAI_StringResponseCreate(char *payload) // Parse payload cJSON *json = cJSON_Parse(payload); - free(payload); // Check for error char *error = getJsonError(json); @@ -653,6 +652,7 @@ static OpenAI_StringResponse_t *OpenAI_StringResponseCreate(char *payload) } cJSON_Delete(json); + free(payload); _stringResponse->parent.getUsage = &OpenAI_StringResponseGetUsage; _stringResponse->parent.getLen = &OpenAI_StringResponseGetLen; _stringResponse->parent.getData = &OpenAI_StringResponseGetDate; @@ -661,6 +661,7 @@ static OpenAI_StringResponse_t *OpenAI_StringResponseCreate(char *payload) return &_stringResponse->parent; end: cJSON_Delete(json); + free(payload); OpenAI_StringResponseDelete(&_stringResponse->parent); return NULL; } @@ -2079,24 +2080,24 @@ static char *OpenAI_AudioTranscriptionFile(OpenAI_AudioTranscription_t *audioTra free(data); OPENAI_ERROR_CHECK(result != NULL, "Empty result!", NULL); cJSON *json = cJSON_Parse(result); - free(result); - result = NULL; + char *result_ret = NULL; char *error = getJsonError(json); if (error != NULL) { if (strcmp(error, "cJSON_Parse failed!") == 0) { free(error); error = NULL; } - result = error; + result_ret = error; } else { if (cJSON_HasObjectItem(json, "text")) { cJSON *text = cJSON_GetObjectItem(json, "text"); - result = strdup(cJSON_GetStringValue(text)); + result_ret = strdup(cJSON_GetStringValue(text)); } } cJSON_Delete(json); - return result; + free(result); + return result_ret; } static OpenAI_AudioTranscription_t *OpenAI_AudioTranscriptionCreate(OpenAI_t *openai) @@ -2225,17 +2226,18 @@ static char *OpenAI_AudioTranslationFile(OpenAI_AudioTranslation_t *audioTransla OPENAI_ERROR_CHECK(result != NULL, "Empty result!", NULL); cJSON *json = cJSON_Parse(result); char *error = getJsonError(json); + char *result_ret = NULL; if (error != NULL) { ESP_LOGE(TAG, "%s", error); } else { - free(result); if (cJSON_HasObjectItem(json, "text")) { cJSON *text = cJSON_GetObjectItem(json, "text"); - result = strdup(cJSON_GetStringValue(text)); + result_ret = strdup(cJSON_GetStringValue(text)); } } cJSON_Delete(json); - return result; + free(result); + return result_ret; } static OpenAI_AudioTranslation_t *OpenAI_AudioTranslationCreate(OpenAI_t *openai) @@ -2397,7 +2399,7 @@ static char *OpenAI_Request(const char *base_url, const char *api_key, const cha esp_http_client_get_chunk_length(client, &content_length); } ESP_LOGD(TAG, "content_length=%d", content_length); - OPENAI_ERROR_CHECK_GOTO(content_length >= 0, "HTTP client fetch headers failed!", end); + OPENAI_ERROR_CHECK_GOTO(content_length > 0, "HTTP client fetch headers failed!", end); result = (char *)malloc(content_length + 1); int read = esp_http_client_read_response(client, result, content_length); if (read != content_length) { diff --git a/components/openai/idf_component.yml b/components/openai/idf_component.yml index 0c51ce366..855acb9f2 100644 --- a/components/openai/idf_component.yml +++ b/components/openai/idf_component.yml @@ -1,4 +1,4 @@ -version: "1.0.0" +version: "1.0.1" description: OpenAI library compatible with ESP-IDF url: https://github.com/espressif/esp-iot-solution repository: https://github.com/espressif/esp-iot-solution.git diff --git a/components/openai/test_apps/main/test_openai.c b/components/openai/test_apps/main/test_openai.c index 380e8761b..680fc4631 100644 --- a/components/openai/test_apps/main/test_openai.c +++ b/components/openai/test_apps/main/test_openai.c @@ -243,7 +243,7 @@ void app_main(void) ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); - // 比较 openai_key 是否是空字符串 + // Compare whether OpenAI_KEY is an empty string if (strlen(openai_key) == 0) { ESP_LOGE(TAG, "Please enter your openai_key"); return;