From 9e4042c149ed5d7ce023e5efa5ef1dc45a942dd3 Mon Sep 17 00:00:00 2001 From: Pavol Sloboda Date: Fri, 11 Apr 2025 11:45:53 +0200 Subject: [PATCH] fix: fixed a possible leak when one of the files (fp1 or fp2) fails to open by adding a simple condition that checks whether one of them opens while the other fails and closing it and added the extra prentheses for the assignment inside the if statement for easier debugging and to avoid static analysis flagging it as a wrong statement inside the if clause --- unittest/libmariadb/connection.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/unittest/libmariadb/connection.c b/unittest/libmariadb/connection.c index d6e73485..04172f05 100644 --- a/unittest/libmariadb/connection.c +++ b/unittest/libmariadb/connection.c @@ -1521,6 +1521,7 @@ static int test_conc327(MYSQL *unused __attribute__((unused))) const char *env= getenv("MYSQL_TMP_DIR"); char cnf_file1[FN_REFLEN + 1]; char cnf_file2[FN_REFLEN + 1]; + my_bool failed_opening_files; SKIP_SKYSQL; @@ -1539,7 +1540,19 @@ static int test_conc327(MYSQL *unused __attribute__((unused))) fp1= fopen(cnf_file1, "w"); fp2= fopen(cnf_file2, "w"); - FAIL_IF(!fp1 || !fp2, "fopen failed"); + if((failed_opening_files = !fp1 || !fp2)) + { + if(fp1) + { + fclose(fp1); + } + if(fp2) + { + fclose(fp2); + } + } + + FAIL_IF(failed_opening_files, "fopen failed"); fprintf(fp1, "!include %s\n", cnf_file2); @@ -1564,7 +1577,18 @@ static int test_conc327(MYSQL *unused __attribute__((unused))) snprintf(cnf_file1, FN_REFLEN, "%s%cmy.cnf", env, FN_LIBCHAR); fp1= fopen(cnf_file1, "w"); fp2= fopen(cnf_file2, "w"); - FAIL_IF(!fp1 || !fp2, "fopen failed"); + if((failed_opening_files = !fp1 || !fp2)) + { + if(fp1) + { + fclose(fp1); + } + if(fp2) + { + fclose(fp2); + } + } + FAIL_IF(failed_opening_files, "fopen failed"); fprintf(fp2, "!includedir %s\n", env);