1- /* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
1+ /* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
22 *
33 * SPDX-License-Identifier: Apache-2.0
44 */
@@ -587,7 +587,6 @@ static OpenAI_StringResponse_t *OpenAI_StringResponseCreate(char *payload)
587587
588588 // Parse payload
589589 cJSON * json = cJSON_Parse (payload );
590- free (payload );
591590
592591 // Check for error
593592 char * error = getJsonError (json );
@@ -653,6 +652,7 @@ static OpenAI_StringResponse_t *OpenAI_StringResponseCreate(char *payload)
653652 }
654653
655654 cJSON_Delete (json );
655+ free (payload );
656656 _stringResponse -> parent .getUsage = & OpenAI_StringResponseGetUsage ;
657657 _stringResponse -> parent .getLen = & OpenAI_StringResponseGetLen ;
658658 _stringResponse -> parent .getData = & OpenAI_StringResponseGetDate ;
@@ -661,6 +661,7 @@ static OpenAI_StringResponse_t *OpenAI_StringResponseCreate(char *payload)
661661 return & _stringResponse -> parent ;
662662end :
663663 cJSON_Delete (json );
664+ free (payload );
664665 OpenAI_StringResponseDelete (& _stringResponse -> parent );
665666 return NULL ;
666667}
@@ -2079,24 +2080,24 @@ static char *OpenAI_AudioTranscriptionFile(OpenAI_AudioTranscription_t *audioTra
20792080 free (data );
20802081 OPENAI_ERROR_CHECK (result != NULL , "Empty result!" , NULL );
20812082 cJSON * json = cJSON_Parse (result );
2082- free (result );
2083- result = NULL ;
2083+ char * result_ret = NULL ;
20842084 char * error = getJsonError (json );
20852085 if (error != NULL ) {
20862086 if (strcmp (error , "cJSON_Parse failed!" ) == 0 ) {
20872087 free (error );
20882088 error = NULL ;
20892089 }
2090- result = error ;
2090+ result_ret = error ;
20912091 } else {
20922092 if (cJSON_HasObjectItem (json , "text" )) {
20932093 cJSON * text = cJSON_GetObjectItem (json , "text" );
2094- result = strdup (cJSON_GetStringValue (text ));
2094+ result_ret = strdup (cJSON_GetStringValue (text ));
20952095 }
20962096 }
20972097
20982098 cJSON_Delete (json );
2099- return result ;
2099+ free (result );
2100+ return result_ret ;
21002101}
21012102
21022103static OpenAI_AudioTranscription_t * OpenAI_AudioTranscriptionCreate (OpenAI_t * openai )
@@ -2225,17 +2226,18 @@ static char *OpenAI_AudioTranslationFile(OpenAI_AudioTranslation_t *audioTransla
22252226 OPENAI_ERROR_CHECK (result != NULL , "Empty result!" , NULL );
22262227 cJSON * json = cJSON_Parse (result );
22272228 char * error = getJsonError (json );
2229+ char * result_ret = NULL ;
22282230 if (error != NULL ) {
22292231 ESP_LOGE (TAG , "%s" , error );
22302232 } else {
2231- free (result );
22322233 if (cJSON_HasObjectItem (json , "text" )) {
22332234 cJSON * text = cJSON_GetObjectItem (json , "text" );
2234- result = strdup (cJSON_GetStringValue (text ));
2235+ result_ret = strdup (cJSON_GetStringValue (text ));
22352236 }
22362237 }
22372238 cJSON_Delete (json );
2238- return result ;
2239+ free (result );
2240+ return result_ret ;
22392241}
22402242
22412243static 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
23972399 esp_http_client_get_chunk_length (client , & content_length );
23982400 }
23992401 ESP_LOGD (TAG , "content_length=%d" , content_length );
2400- OPENAI_ERROR_CHECK_GOTO (content_length >= 0 , "HTTP client fetch headers failed!" , end );
2402+ OPENAI_ERROR_CHECK_GOTO (content_length > 0 , "HTTP client fetch headers failed!" , end );
24012403 result = (char * )malloc (content_length + 1 );
24022404 int read = esp_http_client_read_response (client , result , content_length );
24032405 if (read != content_length ) {
0 commit comments