Fixed gsql Memory Leakage

Offering: openGaussDev

More detail: Fixed gsql Memory Leakage

Match-id-7690c6e6d5a316182fbaa55f89e90e00762fa13d
This commit is contained in:
openGaussDev 2022-03-04 16:00:27 +08:00 committed by yanghao
parent a2e54efa3f
commit 4cb0596eda
1 changed files with 26 additions and 25 deletions

View File

@ -46,8 +46,6 @@ PsqlSettings pset;
/* Used for change child process name in gsql parallel execute mode. */ /* Used for change child process name in gsql parallel execute mode. */
char* argv_para; char* argv_para;
int argv_num; int argv_num;
static int PASSWORD_STR_LEN = 9;
static bool dbname_alloced = false;
static bool is_pipeline = false; static bool is_pipeline = false;
static bool is_interactive = true; static bool is_interactive = true;
#ifndef ENABLE_MULTIPLE_NODES #ifndef ENABLE_MULTIPLE_NODES
@ -630,6 +628,10 @@ int main(int argc, char* argv[])
/* Stored connection and guc info for new connections in gsql parallel mode. */ /* Stored connection and guc info for new connections in gsql parallel mode. */
values_free[3] = true; values_free[3] = true;
if (options.dbname != NULL) {
/* When we use a new dbname or exit the program, we need to free the value. */
values_free[4] = true; /* The dbname index is 4 */
}
pset.connInfo.keywords = keywords; pset.connInfo.keywords = keywords;
pset.connInfo.values = values; pset.connInfo.values = values;
@ -828,6 +830,12 @@ int main(int argc, char* argv[])
for (int i = 0; i < PARAMS_ARRAY_SIZE; i++) { for (int i = 0; i < PARAMS_ARRAY_SIZE; i++) {
if (pset.connInfo.values_free[i] && NULL != pset.connInfo.values[i]) { if (pset.connInfo.values_free[i] && NULL != pset.connInfo.values[i]) {
if (strlen(pset.connInfo.values[i]) != 0) { if (strlen(pset.connInfo.values[i]) != 0) {
/* Erase the connection information in the memory. */
rc = memset_s(pset.connInfo.values[i],
strlen(pset.connInfo.values[i]),
0,
strlen(pset.connInfo.values[i]));
securec_check_c(rc, "\0", "\0");
free(pset.connInfo.values[i]); free(pset.connInfo.values[i]);
pset.connInfo.values[i] = NULL; pset.connInfo.values[i] = NULL;
} }
@ -859,13 +867,6 @@ int main(int argc, char* argv[])
ResetQueryRetryController(); ResetQueryRetryController();
EmptyRetryErrcodesList(pset.errcodes_list); EmptyRetryErrcodesList(pset.errcodes_list);
if (dbname_alloced) {
rc = memset_s(options.dbname, strlen(options.dbname), 0, strlen(options.dbname));
securec_check_c(rc, "\0", "\0");
free(options.dbname);
options.dbname = NULL;
}
return successResult; return successResult;
} }
@ -1014,7 +1015,7 @@ static void parse_psql_options(int argc, char* const argv[], struct adhoc_opts*
bool is_action_file = false; bool is_action_file = false;
/* Database Security: Data importing/dumping support AES128. */ /* Database Security: Data importing/dumping support AES128. */
char* dencrypt_key = NULL; char* dencrypt_key = NULL;
char* needmask = NULL; char* dbname = NULL;
errno_t rc = EOK; errno_t rc = EOK;
#ifdef USE_READLINE #ifdef USE_READLINE
useReadline = false; useReadline = false;
@ -1054,9 +1055,7 @@ static void parse_psql_options(int argc, char* const argv[], struct adhoc_opts*
} }
break; break;
case 'd': case 'd':
options->dbname = pg_strdup(optarg); dbname = optarg;
dbname_alloced = true;
needmask = optarg;
break; break;
case 'e': case 'e':
if (!SetVariable(pset.vars, "ECHO", "queries")) { if (!SetVariable(pset.vars, "ECHO", "queries")) {
@ -1263,10 +1262,8 @@ static void parse_psql_options(int argc, char* const argv[], struct adhoc_opts*
* if we still have arguments, use it as the database name and username * if we still have arguments, use it as the database name and username
*/ */
while (argc - optind >= 1) { while (argc - optind >= 1) {
if (options->dbname == NULL) { if (dbname == NULL) {
options->dbname = pg_strdup(argv[optind]); dbname = argv[optind];
dbname_alloced = true;
needmask = argv[optind];
} else if (options->username == NULL) { } else if (options->username == NULL) {
options->username = argv[optind]; options->username = argv[optind];
} else if (!pset.quiet) { } else if (!pset.quiet) {
@ -1276,21 +1273,25 @@ static void parse_psql_options(int argc, char* const argv[], struct adhoc_opts*
optind++; optind++;
} }
if (needmask != NULL) { /* mask Password information stored in dbname */
if (dbname != NULL) {
/* Save a copy for connection before masking the password. */
options->dbname = pg_strdup(dbname);
/* mask informations in URI string. */ /* mask informations in URI string. */
if (strncmp(options->dbname, "postgresql://", strlen("postgresql://")) == 0) { if (strncmp(dbname, "postgresql://", strlen("postgresql://")) == 0) {
char *off_argv = needmask + strlen("postgresql://"); char *off_argv = dbname + strlen("postgresql://");
rc = memset_s(off_argv, strlen(off_argv), '*', strlen(off_argv)); rc = memset_s(off_argv, strlen(off_argv), '*', strlen(off_argv));
check_memset_s(rc); check_memset_s(rc);
} else if (strncmp(options->dbname, "postgres://", strlen("postgres://")) == 0) { } else if (strncmp(dbname, "postgres://", strlen("postgres://")) == 0) {
char *off_argv = needmask + strlen("postgres://"); char *off_argv = dbname + strlen("postgres://");
rc = memset_s(off_argv, strlen(off_argv), '*', strlen(off_argv)); rc = memset_s(off_argv, strlen(off_argv), '*', strlen(off_argv));
check_memset_s(rc); check_memset_s(rc);
} }
/* mask password */ /* mask password in key/value string. */
char *temp = NULL; char *temp = NULL;
if ((temp = strstr(needmask, "password")) != NULL) { if ((temp = strstr(dbname, "password")) != NULL) {
char *off_argv = temp + PASSWORD_STR_LEN; char *off_argv = temp + strlen("password");
rc = memset_s(off_argv, strlen(off_argv), '*', strlen(off_argv)); rc = memset_s(off_argv, strlen(off_argv), '*', strlen(off_argv));
check_memset_s(rc); check_memset_s(rc);
} }