@@ -281,7 +281,8 @@ ut_kvp_status_t ut_kvp_openMemory(ut_kvp_instance_t *pInstance, char *pData, uin
281281 }
282282 }
283283
284- node = process_node_copy (fy_document_root (srcDoc ), pInternal -> fy_handle , 0 );
284+ struct fy_node * srcNode = fy_document_root (srcDoc );
285+ node = process_node_copy (srcNode , pInternal -> fy_handle , 0 );
285286
286287 if (node == NULL )
287288 {
@@ -931,20 +932,30 @@ static void convert_dot_to_slash(const char *key, char *output)
931932static size_t write_memory_callback (void * contents , size_t size , size_t nmemb , void * userp )
932933{
933934 size_t realsize = size * nmemb ;
935+
936+ if (userp == NULL || contents == NULL )
937+ {
938+ UT_LOG_ERROR ("User pointer or contents are NULL in write_memory_callback" );
939+ return 0 ;
940+ }
941+
934942 ut_kvp_download_memory_internal_t * downloadMemory = (ut_kvp_download_memory_internal_t * )userp ;
935943
936944 char * ptr = realloc (downloadMemory -> memory , downloadMemory -> size + realsize + 1 );
937945 if (ptr == NULL )
938946 {
939947 // Out of memory
940948 UT_LOG_ERROR ("Not enough memory (realloc returned NULL)\n" );
949+ free (downloadMemory -> memory );
950+ downloadMemory -> memory = NULL ;
951+ downloadMemory -> size = 0 ;
941952 return 0 ;
942953 }
943954
944955 downloadMemory -> memory = ptr ;
945956 memcpy (& (downloadMemory -> memory [downloadMemory -> size ]), contents , realsize );
946957 downloadMemory -> size += realsize ;
947- downloadMemory -> memory [downloadMemory -> size ] = 0 ;
958+ downloadMemory -> memory [downloadMemory -> size ] = '\0' ;
948959
949960 return realsize ;
950961}
@@ -1191,7 +1202,30 @@ static struct fy_node* process_include(const char *filename, int depth, struct f
11911202 return NULL ;
11921203 }
11931204
1194- struct fy_document * srcDoc = fy_document_build_from_malloc_string (NULL , mChunk .memory , mChunk .size );
1205+ // Write mChunk.memory to a temporary file
1206+ FILE * tmp = tmpfile ();
1207+ if (!tmp )
1208+ {
1209+ UT_LOG_ERROR ("Failed to create temporary file" );
1210+ free (mChunk .memory );
1211+ curl_easy_cleanup (curl );
1212+ return NULL ;
1213+ }
1214+
1215+ size_t written = fwrite (mChunk .memory , 1 , mChunk .size , tmp );
1216+ if (written != mChunk .size )
1217+ {
1218+ UT_LOG_ERROR ("Failed to write all data to temporary file" );
1219+ fclose (tmp );
1220+ free (mChunk .memory );
1221+ curl_easy_cleanup (curl );
1222+ return NULL ;
1223+ }
1224+ fflush (tmp );
1225+ rewind (tmp );
1226+
1227+ struct fy_document * srcDoc = fy_document_build_from_fp (NULL , tmp );
1228+ //using this instead of fy_document_build_from_malloc_string(), as the malloced string was not getting freed at fy_document_destroy()
11951229 if (srcDoc == NULL )
11961230 {
11971231 UT_LOG_ERROR ("Error: Cannot parse included content\n" );
@@ -1200,13 +1234,13 @@ static struct fy_node* process_include(const char *filename, int depth, struct f
12001234 return NULL ;
12011235 }
12021236
1203- struct fy_node * root = process_node_copy (fy_document_root (srcDoc ), doc , depth + 1 );
1204-
1205- // UT_LOG_DEBUG("%s memory chunk = \n%s\n", __FUNCTION__, mChunk.memory);
1237+ struct fy_node * srcNode = fy_document_root (srcDoc );
1238+ struct fy_node * root = process_node_copy (srcNode , doc , depth + 1 );
12061239
1207- // free(mChunk.memory); // fy_document_build_from_malloc_string(): The string is expected to have been allocated by malloc(3) and when the document is destroyed it will be automatically freed.
1208- curl_easy_cleanup (curl );
12091240 fy_document_destroy (srcDoc );
1241+ fclose (tmp );
1242+ free (mChunk .memory );
1243+ curl_easy_cleanup (curl );
12101244 return root ;
12111245 }
12121246 else
@@ -1229,7 +1263,8 @@ static struct fy_node* process_include(const char *filename, int depth, struct f
12291263 }
12301264
12311265 struct fy_node * root ;
1232- root = process_node_copy (fy_document_root (srcDoc ), doc , depth + 1 );
1266+ struct fy_node * srcNode = fy_document_root (srcDoc );
1267+ root = process_node_copy (srcNode , doc , depth + 1 );
12331268 fclose (file );
12341269 fy_document_destroy (srcDoc );
12351270 return root ;
0 commit comments