Skip to content

Commit 5ea8838

Browse files
Merge branch 'develop' into feature/issue98_support_url
2 parents 889d5d5 + b800ce2 commit 5ea8838

3 files changed

Lines changed: 55 additions & 11 deletions

File tree

src/ut_kvp.c

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
931932
static 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;

tests/lsan.supp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Suppress all leaks originating from libfyaml
2+
leak:libfyaml
3+
leak:fyaml
4+
leak:../framework/linux/libfyaml-master/libfyaml-master
5+
leak:libfyaml-master/src
6+
7+
# Suppress all leaks originating from asprintf
8+
leak:asprintf
9+
leak:../framework/linux/asprintf
10+
leak:asprintf.c-master

tests/src/ut_control_test.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,4 @@ trap '
4242
UT_STATUS=$?
4343

4444
# Exit with same status as UT test
45-
exit $UT_STATUS
46-
45+
exit $UT_STATUS

0 commit comments

Comments
 (0)