Changeset: 93db4ce6790a for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/93db4ce6790a
Modified Files:
        clients/mapilib/connect.c
        clients/mapilib/mapi.c
        common/stream/memio.c
        ctest/tools/monetdbe/example_copy.c
        gdk/gdk_bbp.c
        gdk/gdk_posix.c
        gdk/gdk_rsort.c
        geom/monetdb5/geod.c
        monetdb5/mal/mal_import.c
        monetdb5/mal/mal_interpreter.c
        monetdb5/mal/mal_linker.c
        monetdb5/mal/mal_module.c
        monetdb5/mal/mal_parser.c
        monetdb5/mal/mal_prelude.c
        monetdb5/modules/atoms/json.c
        monetdb5/modules/atoms/str.c
        monetdb5/modules/mal/calc.c
        monetdb5/modules/mal/groupby.c
        monetdb5/modules/mal/mal_mapi.c
        monetdb5/modules/mal/manifold.c
        monetdb5/optimizer/opt_remoteQueries.c
        tools/merovingian/client/monetdb.c
Branch: default
Log Message:

Cleanup.
- Use GDKmalloc instead of GDKzalloc when we're immediately initializing anyway;
- Use GDKzalloc instead of GDMmalloc when we're immediately calling memset;
- Avoid using strcat when it's easy to do so (often we can use stpcpy);
- Use compound literals for initialization in more places.


diffs (truncated from 645 to 300 lines):

diff --git a/clients/mapilib/connect.c b/clients/mapilib/connect.c
--- a/clients/mapilib/connect.c
+++ b/clients/mapilib/connect.c
@@ -797,10 +797,11 @@ mapi_handshake(Mapi mid)
                if (motdlen > 0) {
                        mid->motd = malloc(motdlen + 1);
                        *mid->motd = 0;
+                       char *p = mid->motd;
                        for (i = 0; i < result->cache.writer; i++)
                                if (result->cache.line[i].rows && 
result->cache.line[i].rows[0] == '#') {
-                                       strcat(mid->motd, 
result->cache.line[i].rows);
-                                       strcat(mid->motd, "\n");
+                                       p = stpcpy(p, 
result->cache.line[i].rows);
+                                       p = stpcpy(p, "\n");
                                }
                }
 
diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -1561,10 +1561,8 @@ add_error(struct MapiResultSet *result, 
        REALLOC(result->errorstr, size + strlen(error) + 2);
        if (result->errorstr == NULL)
                result->errorstr = mapi_nomem;
-       else {
-               strcpy(result->errorstr + size, error);
-               strcat(result->errorstr + size, "\n");
-       }
+       else
+               stpcpy(stpcpy(result->errorstr + size, error), "\n");
 }
 
 const char *
diff --git a/common/stream/memio.c b/common/stream/memio.c
--- a/common/stream/memio.c
+++ b/common/stream/memio.c
@@ -20,9 +20,11 @@ buffer_init(buffer *restrict b, char *re
 {
        if (b == NULL || buf == NULL)
                return;
-       b->pos = 0;
-       b->buf = buf;
-       b->len = size;
+       *b = (buffer) {
+               .pos = 0,
+               .buf = buf,
+               .len = size,
+       };
 }
 
 buffer *
diff --git a/ctest/tools/monetdbe/example_copy.c 
b/ctest/tools/monetdbe/example_copy.c
--- a/ctest/tools/monetdbe/example_copy.c
+++ b/ctest/tools/monetdbe/example_copy.c
@@ -21,8 +21,8 @@
 int
 main(void)
 {
-       char sql[1000];
        char csv_path[PATH_MAX];
+       char sql[sizeof(csv_path) + 60];
        char* err = NULL;
        monetdbe_database mdbe;
        monetdbe_result* result = NULL;
@@ -44,22 +44,21 @@ main(void)
        }
        strcat(csv_path, "/test.csv");
 
-       strcpy(sql, "COPY SELECT * FROM test INTO '");
-       strcat(sql, csv_path);
-       strcat(sql, "' USING DELIMITERS ','");
+       snprintf(sql, sizeof(sql),
+                        "COPY SELECT * FROM test INTO '%s' USING DELIMITERS 
','",
+                        csv_path);
 
        if ((err = monetdbe_query(mdbe, sql, NULL, NULL)) != NULL)
                error(err)
-       
+
        if ((err = monetdbe_query(mdbe, "CREATE TABLE test_copy (x integer, y 
string, ts timestamp, dt date, t time, b blob)", NULL, NULL)) != NULL) {
                delete_file(csv_path)
                error(err)
        }
 
-       memset(sql, 0, 1000);
-       strcpy(sql, "COPY INTO test_copy FROM '");
-       strcat(sql, csv_path);
-       strcat(sql, "' DELIMITERS ','");
+       snprintf(sql, sizeof(sql),
+                        "COPY INTO test_copy FROM '%s' DELIMITERS ','",
+                        csv_path);
 
        if ((err = monetdbe_query(mdbe, sql, NULL, NULL)) != NULL) {
                delete_file(csv_path)
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -2493,7 +2493,6 @@ BBPallocbat(int tt)
        MT_Id pid = MT_getpid();
        bool lock = locked_by == 0 || locked_by != pid;
        bat i;
-       int len = 0;
        struct freebats *t = MT_thread_getfreebats();
 
        if (t->freebats == 0) {
@@ -2555,11 +2554,13 @@ BBPallocbat(int tt)
        BBP_pid(i) = pid;
        MT_lock_unset(&GDKswapLock(i));
 
-       if (*BBP_bak(i) == 0)
+       if (*BBP_bak(i) == 0) {
+               int len;
                len = snprintf(BBP_bak(i), sizeof(BBP_bak(i)), "tmp_%o", 
(unsigned) i);
-       if (len == -1 || len >= FILENAME_MAX) {
-               GDKerror("impossible error\n");
-               return 0;
+               if (len == -1 || (size_t) len >= sizeof(BBP_bak(i))) {
+                       GDKerror("impossible error\n");
+                       return 0;
+               }
        }
        BBP_logical(i) = BBP_bak(i);
 
diff --git a/gdk/gdk_posix.c b/gdk/gdk_posix.c
--- a/gdk/gdk_posix.c
+++ b/gdk/gdk_posix.c
@@ -563,7 +563,7 @@ MT_mremap(const char *path, int mode, vo
                                                return NULL;
                                        }
 
-                                       strcat(strcpy(p, path), ".tmp");
+                                       stpcpy(stpcpy(p, path), ".tmp");
                                        fd = open(p, O_RDWR | O_CREAT | 
O_CLOEXEC,
                                                  MONETDB_MODE);
                                        if (fd < 0) {
diff --git a/gdk/gdk_rsort.c b/gdk/gdk_rsort.c
--- a/gdk/gdk_rsort.c
+++ b/gdk/gdk_rsort.c
@@ -20,7 +20,7 @@
 gdk_return
 GDKrsort(void *restrict h, void *restrict t, size_t n, size_t hs, size_t ts, 
bool reverse, bool isuuid)
 {
-       size_t (*counts)[NBUCKETS] = GDKmalloc(hs * sizeof(counts[0]));
+       size_t (*counts)[NBUCKETS] = GDKzalloc(hs * sizeof(counts[0]));
        size_t pos[NBUCKETS];
        uint8_t *h1 = h;
        uint8_t *h2;
@@ -57,7 +57,6 @@ GDKrsort(void *restrict h, void *restric
                ts = 0;
        }
 
-       memset(counts, 0, hs * sizeof(counts[0]));
 #ifndef WORDS_BIGENDIAN
        if (isuuid /* UUID, treat like big-endian */) {
 #endif
diff --git a/geom/monetdb5/geod.c b/geom/monetdb5/geod.c
--- a/geom/monetdb5/geod.c
+++ b/geom/monetdb5/geod.c
@@ -355,16 +355,21 @@ boundingBoxLines(GeoLines lines)
        if (lines.pointCount == 0)
                return NULL;
 
-       bb = GDKzalloc(sizeof(BoundingBox));
+       bb = GDKmalloc(sizeof(BoundingBox));
        if (bb == NULL)
                return NULL;
 
        c = geo2cartFromDegrees(lines.points[0]);
 
        //Initialize the bounding box with the first point
-       bb->xmin = bb->xmax = c.x;
-       bb->ymin = bb->ymax = c.y;
-       bb->zmin = bb->zmax = c.z;
+       *bb = (BoundingBox) {
+               .xmin = c.x,
+               .xmax = c.x,
+               .ymin = c.y,
+               .ymax = c.y,
+               .zmin = c.z,
+               .zmax = c.z,
+       };
 
        for (int i = 1; i < lines.pointCount; i++) {
                c = geo2cartFromDegrees(lines.points[i]);
diff --git a/monetdb5/mal/mal_import.c b/monetdb5/mal/mal_import.c
--- a/monetdb5/mal/mal_import.c
+++ b/monetdb5/mal/mal_import.c
@@ -305,7 +305,7 @@ compileString(Symbol *fcn, Client cntxt,
        }
 
        mal_unquote(qry);
-       b = (buffer *) GDKzalloc(sizeof(buffer));
+       b = (buffer *) GDKmalloc(sizeof(buffer));
        if (b == NULL) {
                GDKfree(qry);
                throw(MAL, "mal.eval", SQLSTATE(HY013) MAL_MALLOC_FAIL);
diff --git a/monetdb5/mal/mal_interpreter.c b/monetdb5/mal/mal_interpreter.c
--- a/monetdb5/mal/mal_interpreter.c
+++ b/monetdb5/mal/mal_interpreter.c
@@ -1252,13 +1252,13 @@ runMALsequence(Client cntxt, MalBlkPtr m
                        str new, n;
                        n = createException(MAL, nme, "exception not caught");
                        if (n) {
-                               new = GDKzalloc(strlen(ret) + strlen(n) + 16);
+                               new = GDKmalloc(strlen(ret) + strlen(n) + 16);
                                if (new) {
-                                       strcpy(new, ret);
-                                       if (new[strlen(new) - 1] != '\n')
-                                               strcat(new, "\n");
-                                       strcat(new, "!");
-                                       strcat(new, n);
+                                       char *p = stpcpy(new, ret);
+                                       if (p[-1] != '\n')
+                                               *p++ = '\n';
+                                       *p++ = '!';
+                                       p = stpcpy(p, n);
                                        freeException(n);
                                        freeException(ret);
                                        ret = new;
diff --git a/monetdb5/mal/mal_linker.c b/monetdb5/mal/mal_linker.c
--- a/monetdb5/mal/mal_linker.c
+++ b/monetdb5/mal/mal_linker.c
@@ -432,7 +432,7 @@ locate_file(const char *basename, const 
                   extra checks */
                strncpy(fullname, mod_path, i);
                fullname[i] = DIR_SEP;
-               strcpy(fullname + i + 1, basename);
+               char *nameend = stpcpy(fullname + i + 1, basename);
                /* see if this is a directory, if so, recurse */
                if (recurse == 1 && (rdir = opendir(fullname)) != NULL) {
                        struct dirent *e;
@@ -468,7 +468,7 @@ locate_file(const char *basename, const 
                        }
                        (void) closedir(rdir);
                } else {
-                       strcat(fullname + i + 1, ext);
+                       strcpy(nameend, ext);
                        if ((fd = MT_open(fullname, O_RDONLY | O_CLOEXEC)) >= 
0) {
                                char *tmp;
                                close(fd);
diff --git a/monetdb5/mal/mal_module.c b/monetdb5/mal/mal_module.c
--- a/monetdb5/mal/mal_module.c
+++ b/monetdb5/mal/mal_module.c
@@ -244,15 +244,13 @@ userModule(void)
 {
        Module cur;
 
-       cur = (Module) GDKzalloc(sizeof(ModuleRecord));
+       cur = (Module) GDKmalloc(sizeof(ModuleRecord));
        if (cur == NULL)
                return NULL;
-       cur->name = userRef;
-       if (cur->name == NULL) {
-               GDKfree(cur);
-               return NULL;
-       }
-       cur->link = NULL;
+       *cur = (ModuleRecord) {
+               .name = userRef,
+               .link = NULL,
+       };
        return cur;
 }
 
diff --git a/monetdb5/mal/mal_parser.c b/monetdb5/mal/mal_parser.c
--- a/monetdb5/mal/mal_parser.c
+++ b/monetdb5/mal/mal_parser.c
@@ -111,7 +111,7 @@ parseError(Client cntxt, str msg)
        marker = createException(SYNTAX, "parseError", "%s%s", buf, msg);
 
        old = mb->errors;
-       new = GDKzalloc((old ? strlen(old) : 0) + strlen(line) + strlen(marker) 
+
+       new = GDKmalloc((old ? strlen(old) : 0) + strlen(line) + strlen(marker) 
+
                                        64);
        if (new == NULL) {
                freeException(line);
@@ -119,14 +119,14 @@ parseError(Client cntxt, str msg)
                skipToEnd(cntxt);
                return;                                 // just stick to old 
error message
        }
+       mb->errors = new;
        if (old) {
-               strcpy(new, old);
+               new = stpcpy(new, old);
                GDKfree(old);
        }
-       strcat(new, line);
-       strcat(new, marker);
+       new = stpcpy(new, line);
+       new = stpcpy(new, marker);
 
-       mb->errors = new;
        freeException(line);
        freeException(marker);
        skipToEnd(cntxt);
@@ -1912,11 +1912,11 @@ parseEnd(Client cntxt)
                        str new = GDKmalloc(strlen(errors) +
                                                                
strlen(cntxt->curprg->def->errors) + 16);
                        if (new) {
-                               strcpy(new, errors);
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to