Changeset: 718d6667305d for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/718d6667305d
Modified Files:
        clients/mapiclient/dotmonetdb.c
        clients/mapiclient/dotmonetdb.h
        clients/mapiclient/mclient.c
        clients/mapiclient/msqldump.c
Branch: Jun2023
Log Message:

Do fewer mallocs, and make sure everything that is allocated gets freed.
When dealing with the .monetdb file, that is.


diffs (162 lines):

diff --git a/clients/mapiclient/dotmonetdb.c b/clients/mapiclient/dotmonetdb.c
--- a/clients/mapiclient/dotmonetdb.c
+++ b/clients/mapiclient/dotmonetdb.c
@@ -158,3 +158,14 @@ parse_dotmonetdb(DotMonetdb *dotfile)
                fclose(config);
        }
 }
+
+void
+destroy_dotmonetdb(DotMonetdb *dotfile)
+{
+       free(dotfile->user);
+       free(dotfile->passwd);
+       free(dotfile->dbname);
+       free(dotfile->host);
+       free(dotfile->output);
+       free(dotfile->language);
+}
diff --git a/clients/mapiclient/dotmonetdb.h b/clients/mapiclient/dotmonetdb.h
--- a/clients/mapiclient/dotmonetdb.h
+++ b/clients/mapiclient/dotmonetdb.h
@@ -21,3 +21,4 @@ typedef struct DotMonetdb {
 } DotMonetdb;
 
 extern void parse_dotmonetdb(DotMonetdb *dotfile);
+extern void destroy_dotmonetdb(DotMonetdb *dotfile);
diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -3471,11 +3471,7 @@ main(int argc, char **argv)
 #endif
                        mnstr_printf(toConsole, "using mapi library %s\n",
                                                 mapi_get_mapi_version());
-                       free(dotfile.user);
-                       free(dotfile.passwd);
-                       free(dotfile.dbname);
-                       free(dotfile.host);
-                       free(dotfile.output);
+                       destroy_dotmonetdb(&dotfile);
                        return 0;
                }
                case 'w':
@@ -3771,11 +3767,7 @@ main(int argc, char **argv)
        if (priv.buf != NULL)
                free(priv.buf);
 
-       free(dotfile.user);
-       free(dotfile.passwd);
-       free(dotfile.dbname);
-       free(dotfile.host);
-       free(dotfile.output);
+       destroy_dotmonetdb(&dotfile);
 
        return c;
 }
diff --git a/clients/mapiclient/msqldump.c b/clients/mapiclient/msqldump.c
--- a/clients/mapiclient/msqldump.c
+++ b/clients/mapiclient/msqldump.c
@@ -60,10 +60,10 @@ main(int argc, char **argv)
 #endif
 {
        int port = 0;
-       char *user = NULL;
-       char *passwd = NULL;
-       char *host = NULL;
-       char *dbname = NULL;
+       const char *user = NULL;
+       const char *passwd = NULL;
+       const char *host = NULL;
+       const char *dbname = NULL;
        DotMonetdb dotfile = {0};
        bool trace = false;
        bool describe = false;
@@ -117,9 +117,7 @@ main(int argc, char **argv)
        while ((c = getopt_long(argc, argv, "h:p:d:Dft:NeXu:qv?", long_options, 
NULL)) != -1) {
                switch (c) {
                case 'u':
-                       if (user)
-                               free(user);
-                       user = strdup(optarg);
+                       user = optarg;
                        user_set_as_flag = true;
                        break;
                case 'h':
@@ -130,9 +128,7 @@ main(int argc, char **argv)
                        port = atoi(optarg);
                        break;
                case 'd':
-                       if (dbname)
-                               free(dbname);
-                       dbname = strdup(optarg);
+                       dbname = optarg;
                        break;
                case 'D':
                        describe = true;
@@ -170,6 +166,7 @@ main(int argc, char **argv)
                                printf(" (hg id: %s)", rev);
 #endif
                        printf("\n");
+                       destroy_dotmonetdb(&dotfile);
                        return 0;
                }
                case '?':
@@ -183,7 +180,7 @@ main(int argc, char **argv)
        }
 
        if (optind == argc - 1)
-               dbname = strdup(argv[optind]);
+               dbname = argv[optind];
        else if (optind != argc)
                usage(argv[0], -1);
 
@@ -195,18 +192,25 @@ main(int argc, char **argv)
                printf("msqldump, please specify a database\n");
                usage(argv[0], -1);
        }
-       if (user == NULL)
-               user = simple_prompt("user", BUFSIZ, 1, prompt_getlogin());
-       if (passwd == NULL)
-               passwd = simple_prompt("password", BUFSIZ, 0, NULL);
+       char *user_allocated = NULL;
+       if (user == NULL) {
+               user_allocated = simple_prompt("user", BUFSIZ, 1, 
prompt_getlogin());
+               user = user_allocated;
+       }
+       char *passwd_allocated = NULL;
+       if (passwd == NULL) {
+               passwd_allocated = simple_prompt("password", BUFSIZ, 0, NULL);
+               passwd = passwd_allocated;
+       }
 
        mid = mapi_mapi(host, port, user, passwd, "sql", dbname);
-       if (user)
-               free(user);
-       if (passwd)
-               free(passwd);
-       if (dbname)
-               free(dbname);
+       free(user_allocated);
+       user_allocated = NULL;
+       free(passwd_allocated);
+       passwd_allocated = NULL;
+       user = NULL;
+       passwd = NULL;
+       dbname = NULL;
        if (mid == NULL) {
                fprintf(stderr, "failed to allocate Mapi structure\n");
                exit(2);
@@ -279,9 +283,12 @@ main(int argc, char **argv)
        mapi_destroy(mid);
        if (mnstr_errnr(out) != MNSTR_NO__ERROR) {
                fprintf(stderr, "%s: %s\n", argv[0], mnstr_peek_error(out));
-               return 1;
+               c = 1;
        }
 
        mnstr_destroy(out);
+
+       destroy_dotmonetdb(&dotfile);
+
        return c;
 }
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to