Changeset: 3635b60bc753 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/3635b60bc753
Modified Files:
        clients/odbc/setup/drvcfg.c
        common/utils/msabaoth.c
        gdk/gdk_bbp.c
        gdk/gdk_posix.c
        monetdb5/mal/mal_linker.c
        monetdb5/mal/mal_listing.c
        monetdb5/modules/atoms/batxml.c
        monetdb5/modules/atoms/json.c
        monetdb5/modules/mal/mal_mapi.c
        monetdb5/modules/mal/tablet.c
        monetdb5/optimizer/opt_mergetable.c
        sql/backends/monet5/UDF/pyapi3/type_conversion3.c
        tools/merovingian/client/monetdb.c
Branch: default
Log Message:

Stop using strncpy and strncat.  Use strtcpy, stpecpy, strlcpy etc. instead.
strncpy is unsuitable for modern use: it doesn't close the string with a
NULL if the string is longer than the buffer, and it fills the buffer
with NULLs if the string is shorter.


diffs (truncated from 390 to 300 lines):

diff --git a/clients/odbc/setup/drvcfg.c b/clients/odbc/setup/drvcfg.c
--- a/clients/odbc/setup/drvcfg.c
+++ b/clients/odbc/setup/drvcfg.c
@@ -23,7 +23,6 @@
 #include "monetdb_config.h"
 
 #include "drvcfg.h"
-#include <string.h>            /* for memset(), memcpy(), strncpy() */
 #include "mstring.h"
 
 static const char *aHost[] = {
diff --git a/common/utils/msabaoth.c b/common/utils/msabaoth.c
--- a/common/utils/msabaoth.c
+++ b/common/utils/msabaoth.c
@@ -227,8 +227,7 @@ msab_dbpathinit(const char *dbpath)
 
        p = strrchr(dbpath, DIR_SEP);
        assert(p != NULL);
-       strncpy(dbfarm, dbpath, p - dbpath);
-       dbfarm[p - dbpath] = 0;
+       strtcpy(dbfarm, dbpath, p - dbpath + 1);
        msab_init(dbfarm, p + 1);
 }
 void
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -4055,11 +4055,14 @@ force_move(int farmid, const char *srcdi
 
        if ((p = strrchr(name, '.')) != NULL && strcmp(p, ".kill") == 0) {
                /* Found a X.new.kill file, ie remove the X.new file */
-               ptrdiff_t len = p - name;
+               size_t len = p - name;
                long_str srcpath;
 
-               strncpy(srcpath, name, len);
-               srcpath[len] = '\0';
+               if (len >= sizeof(srcpath)) {
+                       GDKerror("force_move: file name %s too long\n", name);
+                       return GDK_FAIL;
+               }
+               strtcpy(srcpath, name, len + 1);
                if (GDKfilepath(dstpath, sizeof(dstpath), farmid, dstdir, 
srcpath, NULL) != GDK_SUCCEED) {
                        return GDK_FAIL;
                }
@@ -4163,12 +4166,11 @@ BBPrecover(int farmid)
                }
                if (q == NULL)
                        q = dent->d_name + strlen(dent->d_name);
-               if ((j = q - dent->d_name) + 1 > sizeof(path)) {
+               if ((j = q - dent->d_name) >= sizeof(path)) {
                        /* name too long: ignore */
                        continue;
                }
-               strncpy(path, dent->d_name, j);
-               path[j] = 0;
+               strtcpy(path, dent->d_name, j + 1);
                if (GDKisdigit(*path)) {
                        i = strtol(path, NULL, 8);
                } else {
@@ -4311,8 +4313,7 @@ BBPdiskscan(const char *parent, size_t b
        DIR *dirp = opendir(parent);
        struct dirent *dent;
        char fullname[FILENAME_MAX];
-       str dst;
-       size_t dstlen;
+       char *dst;
        const char *src = parent;
 
        if (dirp == NULL) {
@@ -4321,10 +4322,9 @@ BBPdiskscan(const char *parent, size_t b
                return true;    /* nothing to do */
        }
 
-       dst = stpcpy(fullname, src);
-       if (dst > fullname && dst[-1] != DIR_SEP)
-               *dst++ = DIR_SEP;
-       dstlen = sizeof(fullname) - (dst - fullname);
+       dst = stpecpy(fullname, &fullname[sizeof(fullname)], src);
+       if (dst != NULL && dst > fullname && dst[-1] != DIR_SEP)
+               dst = stpecpy(dst, &fullname[sizeof(fullname)], DIR_SEP_STR);
 
        while ((dent = readdir(dirp)) != NULL) {
                const char *p;
@@ -4347,16 +4347,13 @@ BBPdiskscan(const char *parent, size_t b
                        continue;
 
                p = strchr(dent->d_name, '.');
-
-               if (strlen(dent->d_name) >= dstlen) {
+               if (stpecpy(dst, &fullname[sizeof(fullname)], dent->d_name) == 
NULL) {
                        /* found a file with too long a name
-                          (i.e. unknown); stop pruning in this
-                          subdir */
+                        * (i.e. unknown); stop pruning in this
+                        * subdir */
                        fprintf(stderr, "unexpected file %s, leaving %s.\n", 
dent->d_name, parent);
                        break;
                }
-               strncpy(dst, dent->d_name, dstlen);
-               fullname[sizeof(fullname) - 1] = 0;
 
                if (p == NULL && !BBPdiskscan(fullname, baseoff)) {
                        /* it was a directory */
diff --git a/gdk/gdk_posix.c b/gdk/gdk_posix.c
--- a/gdk/gdk_posix.c
+++ b/gdk/gdk_posix.c
@@ -22,7 +22,6 @@
 #include "gdk_private.h"
 #include "mutils.h"
 #include <unistd.h>
-#include <string.h>     /* strncpy */
 
 #ifdef HAVE_FCNTL_H
 # include <fcntl.h>
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
@@ -415,11 +415,10 @@ locate_file(allocator *ma, const char *b
                }
                if (i + filelen + 2 > PATH_MAX)
                        return NULL;
-               /* we are now sure the directory name, file
-                  base name, extension, and separator fit
-                  into fullname, so we don't need to do any
-                  extra checks */
-               strncpy(fullname, mod_path, i);
+               /* we are now sure the directory name, file base name,
+                * extension, and separator fit into fullname, so we don't need
+                * to do any extra checks */
+               strtcpy(fullname, mod_path, i + 1);
                fullname[i] = DIR_SEP;
                strcpy(stpcpy(fullname + i + 1, basename), ext);
                if ((fd = MT_open(fullname, O_RDONLY | O_CLOEXEC)) >= 0) {
diff --git a/monetdb5/mal/mal_listing.c b/monetdb5/mal/mal_listing.c
--- a/monetdb5/mal/mal_listing.c
+++ b/monetdb5/mal/mal_listing.c
@@ -652,14 +652,14 @@ mal2str(MalBlkPtr mb, int first, int las
        }
 
        totlen = 0;
+       char *p = ps;
        for (i = first; i < last; i++) {
                if (txt[i]) {
-                       strncpy(ps + totlen, txt[i], len[i]);
-                       ps[totlen + len[i]] = '\n';
-                       ps[totlen + len[i] + 1] = 0;
-                       totlen += len[i] + 1;
+                       p = stpcpy(p, txt[i]);
+                       *p++ = '\n';
                }
        }
+       *p = 0;
        return ps;
 }
 
diff --git a/monetdb5/modules/atoms/batxml.c b/monetdb5/modules/atoms/batxml.c
--- a/monetdb5/modules/atoms/batxml.c
+++ b/monetdb5/modules/atoms/batxml.c
@@ -1353,7 +1353,7 @@ BATxmlaggr(BAT **bnp, BAT *b, BAT *g, BA
                                if (bunfastapp_nocheckVAR(bn, buf) != 
GDK_SUCCEED)
                                        goto bunins_failed;
                                nils += strNil(buf);
-                               strncpy(buf, str_nil, maxlen);
+                               strtcpy(buf, str_nil, maxlen);
                                buflen = 0;
                                if (p == q)
                                        break;
@@ -1366,7 +1366,7 @@ BATxmlaggr(BAT **bnp, BAT *b, BAT *g, BA
                        if (strNil(v)) {
                                if (skip_nils)
                                        continue;
-                               strncpy(buf, str_nil, buflen);
+                               strtcpy(buf, str_nil, maxlen);
                                isnil = 1;
                        } else {
                                len = strlen(v);
@@ -1380,7 +1380,7 @@ BATxmlaggr(BAT **bnp, BAT *b, BAT *g, BA
                                        buf = tmp;
                                }
                                if (buflen == 0) {
-                                       strncpy(buf, v, maxlen);
+                                       strtcpy(buf, v, maxlen);
                                        buflen += len;
                                } else if (buf[0] != v[0]) {
                                        err = "incompatible values in group";
@@ -1405,7 +1405,7 @@ BATxmlaggr(BAT **bnp, BAT *b, BAT *g, BA
                        if (strNil(v)) {
                                if (skip_nils)
                                        continue;
-                               strncpy(buf, str_nil, buflen);
+                               strtcpy(buf, str_nil, maxlen);
                                nils++;
                                break;
                        }
@@ -1420,7 +1420,7 @@ BATxmlaggr(BAT **bnp, BAT *b, BAT *g, BA
                                buf = tmp;
                        }
                        if (buflen == 0) {
-                               strncpy(buf, v, maxlen);
+                               strtcpy(buf, v, maxlen);
                                buflen += len;
                        } else if (buf[0] != v[0]) {
                                err = "incompatible values in group";
diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -172,7 +172,7 @@ JSONtoStorageString(JSON *jt, int idx, j
                break;
        case JSON_ELEMENT:
                *p++ = '"';
-               strncpy(p, jt->elm[idx].value, jt->elm[idx].valuelen);
+               memcpy(p, jt->elm[idx].value, jt->elm[idx].valuelen);
                p += jt->elm[idx].valuelen;
                *p++ = '"';
                *p++ = ':';
@@ -187,7 +187,7 @@ JSONtoStorageString(JSON *jt, int idx, j
        case JSON_NUMBER:
                /* fall through */
        case JSON_STRING:
-               strncpy(p, jt->elm[idx].value, jt->elm[idx].valuelen);
+               memcpy(p, jt->elm[idx].value, jt->elm[idx].valuelen);
                *out_size += jt->elm[idx].valuelen;
                p += *out_size;
                break;
@@ -200,7 +200,7 @@ JSONtoStorageString(JSON *jt, int idx, j
                p += sz;
                break;
        case JSON_NULL:
-               strncpy(p, "null", 5);
+               strcpy(p, "null");
                *out_size += 4;
                p += *out_size;
                break;
@@ -755,7 +755,7 @@ JSONcompile(Client ctx, const char *expr
                        if (terms[t].name == NULL)
                                throw(MAL, "json.compile", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        terms[t].namelen = s - beg;
-                       strncpy(terms[t].name, beg, s - beg);
+                       strtcpy(terms[t].name, beg, s - beg + 1);
                        if (*s == '.')
                                s--;
                        if (*s == 0) {
@@ -2587,10 +2587,10 @@ JSONfoldKeyValue(Client ctx, str *ret, c
                                goto memfail;
                        }
                        if (strcmp(val, "nil") == 0) {
-                               val = NULL;
+                               val = "null";
                        }
                }
-               l = val ? strlen(val) : 4;
+               l = strlen(val);
                size_t osz = lim;
                while (l > lim - len)
                        lim = (lim / (i + 1)) * cnt + BUFSIZ + l + 3;
@@ -2600,7 +2600,7 @@ JSONfoldKeyValue(Client ctx, str *ret, c
                        bat_iterator_end(&bvi);
                        goto memfail;
                }
-               strncpy(row + len, val ? val : "null", l);
+               strcpy(row + len, val);
                len += l;
                row[len++] = ',';
                row[len] = 0;
diff --git a/monetdb5/modules/mal/mal_mapi.c b/monetdb5/modules/mal/mal_mapi.c
--- a/monetdb5/modules/mal/mal_mapi.c
+++ b/monetdb5/modules/mal/mal_mapi.c
@@ -463,7 +463,7 @@ doChallenge(void *data)
                        p += strlen(p);
                        *p++ = ']';
                        *p++ = ':';
-                       strncpy(p, service, peer_end - p);
+                       strtcpy(p, service, peer_end - p);
                        peer = peerbuf;
                } else {
                        peer = NULL;
diff --git a/monetdb5/modules/mal/tablet.c b/monetdb5/modules/mal/tablet.c
--- a/monetdb5/modules/mal/tablet.c
+++ b/monetdb5/modules/mal/tablet.c
@@ -257,10 +257,10 @@ output_line(allocator *ma, char **buf, s
                                                return -1;      /* *buf freed 
by caller */
                                        *len = fill + l + f->seplen + BUFSIZ;
                                }
-                               strncpy(*buf + fill, p, l);
+                               strtcpy(*buf + fill, p, l + 1);
                                fill += l;
                        }
-                       strncpy(*buf + fill, f->sep, f->seplen);
+                       strtcpy(*buf + fill, f->sep, f->seplen + 1);
                        fill += f->seplen;
                }
        }
@@ -300,11 +300,11 @@ output_line_dense(allocator *ma, char **
                                        return -1;      /* *buf freed by caller 
*/
                                *len = fill + l + f->seplen + BUFSIZ;
                        }
-                       strncpy(*buf + fill, p, l);
+                       strtcpy(*buf + fill, p, l + 1);
                        fill += l;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to