Skip to content

Commit

Permalink
Merge branch 'fix/openai_json_parse' into 'master'
Browse files Browse the repository at this point in the history
fix(openai): incorrect free JSON payload

See merge request ae_group/esp-iot-solution!1098
  • Loading branch information
leeebo committed Nov 6, 2024
2 parents 0633084 + 476a24b commit 17c072b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 6 additions & 0 deletions components/openai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
24 changes: 13 additions & 11 deletions components/openai/OpenAI.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion components/openai/idf_component.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/openai/test_apps/main/test_openai.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 17c072b

Please sign in to comment.