Changeset: c797c68421d8 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/c797c68421d8
Modified Files:
        clients/mapiclient/dump.c
        clients/mapilib/connect_unix.c
        clients/mapilib/mapi.c
        clients/odbc/driver/ODBCUtil.c
        clients/odbc/driver/ODBCUtil.h
        clients/odbc/driver/SQLDriverConnect.c
        clients/odbc/driver/SQLGetDiagField.c
        cmake/monetdb-defines.cmake
        common/utils/msabaoth.c
        common/utils/mstring.h
        common/utils/mutils.c
        gdk/gdk_utils.c
        geom/monetdb5/geom.c
        monetdb5/mal/mal_exception.c
        monetdb5/modules/atoms/json.c
        monetdb5/modules/atoms/url.c
        monetdb_config.h.in
        tools/merovingian/daemon/snapshot.c
Branch: default
Log Message:

Use (ma_)strndup where it makes sense.
Also, provide an implementation of strndup (and strnlen) for systems
that don't provide their own.


diffs (truncated from 585 to 300 lines):

diff --git a/clients/mapiclient/dump.c b/clients/mapiclient/dump.c
--- a/clients/mapiclient/dump.c
+++ b/clients/mapiclient/dump.c
@@ -1542,13 +1542,11 @@ describe_sequence(Mapi mid, const char *
 
        if (schema == NULL) {
                if ((sname = strchr(tname, '.')) != NULL) {
-                       size_t len = sname - tname + 1;
-
-                       sname = malloc(len);
+                       size_t len = sname - tname;
+                       sname = strndup(tname, len);
                        if (sname == NULL)
                                goto bailout;
-                       strtcpy(sname, tname, len);
-                       tname += len;
+                       tname += len + 1;
                } else if ((sname = get_schema(mid)) == NULL) {
                        return 1;
                }
@@ -2103,15 +2101,14 @@ dump_table(Mapi mid, const char *schema,
 
        if (schema == NULL) {
                if ((sname = strchr(tname, '.')) != NULL) {
-                       size_t len = sname - tname + 1;
+                       size_t len = sname - tname;
 
-                       sname = malloc(len);
+                       sname = strndup(tname, len);
                        if (sname == NULL) {
                                fprintf(stderr, "malloc failure\n");
                                return 1;
                        }
-                       strtcpy(sname, tname, len);
-                       tname += len;
+                       tname += len + 1;
                } else if ((sname = get_schema(mid)) == NULL) {
                        return 1;
                }
@@ -2534,13 +2531,12 @@ dump_functions(Mapi mid, stream *sqlf, c
                        /* no schema given, so figure it out */
                        const char *dot = strchr(fname, '.');
                        if (dot != NULL) {
-                               size_t len = dot - fname + 1;
+                               size_t len = dot - fname;
 
-                               to_free = malloc(len);
+                               to_free = strndup(fname, len);
                                if (to_free == NULL)
                                        goto bailout;
-                               strtcpy(to_free, fname, len);
-                               fname += len;
+                               fname += len + 1;
                        } else if ((to_free = get_schema(mid)) == NULL) {
                                return 1;
                        }
diff --git a/clients/mapilib/connect_unix.c b/clients/mapilib/connect_unix.c
--- a/clients/mapilib/connect_unix.c
+++ b/clients/mapilib/connect_unix.c
@@ -139,8 +139,10 @@ connect_socket_unix(Mapi mid)
 
        mapi_log_record(mid, "CONN", "Connecting to Unix domain socket %s with 
timeout %ld", sockname, timeout);
 
-       struct sockaddr_un userver;
-       if (strlen(sockname) >= sizeof(userver.sun_path)) {
+       struct sockaddr_un userver = {
+               .sun_family = AF_UNIX,
+       };
+       if (strtcpy(userver.sun_path, sockname, sizeof(userver.sun_path)) == 
-1) {
                return mapi_printError(mid, __func__, MERROR, "path name '%s' 
too long", sockname);
        }
 
@@ -178,11 +180,6 @@ connect_socket_unix(Mapi mid)
 
        // Attempt to connect
 
-       userver = (struct sockaddr_un) {
-               .sun_family = AF_UNIX,
-       };
-       strtcpy(userver.sun_path, sockname, sizeof(userver.sun_path));
-
        if (connect(s, (struct sockaddr *) &userver, sizeof(struct 
sockaddr_un)) == SOCKET_ERROR) {
                closesocket(s);
                return mapi_printError(
diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -3685,12 +3685,9 @@ mapi_query_part(MapiHdl hdl, const char 
        mid->active = hdl;
        /* remember the query just for the error messages */
        if (hdl->query == NULL) {
-               hdl->query = malloc(size + 1);
-               if (hdl->query) {
-                       strtcpy(hdl->query, query, size + 1);
-               }
+               hdl->query = strndup(query, size);
        } else {
-               size_t sz = strlen(hdl->query);
+               size_t sz = strnlen(hdl->query, size);
                char *q;
 
                if (sz < 512 &&
@@ -4070,8 +4067,7 @@ unquote(const char *msg, char **str, con
                        p++;
                }
                len = s - msg;
-               *str = malloc(len + 1);
-               strtcpy(*str, msg, len + 1);
+               *str = strndup(msg, len);
 
                if (next)
                        *next = p;
diff --git a/clients/odbc/driver/ODBCUtil.c b/clients/odbc/driver/ODBCUtil.c
--- a/clients/odbc/driver/ODBCUtil.c
+++ b/clients/odbc/driver/ODBCUtil.c
@@ -52,25 +52,6 @@ DllMain(HINSTANCE hinstDLL, DWORD reason
 }
 #endif
 
-/*
- * Utility function to duplicate an ODBC string (with a length
- * specified, may not be null terminated) to a normal C string (null
- * terminated).
- *
- * Precondition: inStr != NULL
- * Postcondition: returns a newly allocated null terminated string.
- */
-char *
-dupODBCstring(const SQLCHAR *inStr, size_t length)
-{
-       char *tmp = (char *) malloc((length + 1) * sizeof(char));
-
-       if (tmp == NULL)
-               return NULL;
-       strtcpy(tmp, (const char *) inStr, length + 1);
-       return tmp;
-}
-
 /* Convert a SQLWCHAR (UTF-16 encoded string) to UTF-8.  On success,
    clears the location pointed to by errmsg and returns NULL or a
    newly allocated buffer.  On error, assigns a string with an error
diff --git a/clients/odbc/driver/ODBCUtil.h b/clients/odbc/driver/ODBCUtil.h
--- a/clients/odbc/driver/ODBCUtil.h
+++ b/clients/odbc/driver/ODBCUtil.h
@@ -44,7 +44,11 @@
  * Precondition: inStr != NULL
  * Postcondition: returns a newly allocated null terminated string
  */
-extern char *dupODBCstring(const SQLCHAR *inStr, size_t length);
+static inline char *
+dupODBCstring(const SQLCHAR *inStr, size_t length)
+{
+       return strndup((const char *) inStr, length);
+}
 
 /*
  * Utility macro to fix up args that represent an ODBC string.  If len
diff --git a/clients/odbc/driver/SQLDriverConnect.c 
b/clients/odbc/driver/SQLDriverConnect.c
--- a/clients/odbc/driver/SQLDriverConnect.c
+++ b/clients/odbc/driver/SQLDriverConnect.c
@@ -55,10 +55,9 @@ ODBCGetKeyAttr(const SQLCHAR **conn, SQL
        if (*nconn == 0 || !**conn || **conn == ';')
                return 0;
        len = *conn - p;
-       *key = (char *) malloc(len + 1);
+       *key = strndup((const char *) p, len);
        if (*key == NULL)
                return -1;
-       strtcpy(*key, (char *) p, len + 1);
        (*conn)++;
        (*nconn)--;
        p = *conn;
@@ -72,13 +71,12 @@ ODBCGetKeyAttr(const SQLCHAR **conn, SQL
                        (*nconn)--;
                }
                len = *conn - p;
-               *attr = (char *) malloc(len + 1);
+               *attr = strndup((const char *) p, len);
                if (*attr == NULL) {
                        free(*key);
                        *key = NULL;
                        return -1;
                }
-               strtcpy(*attr, (char *) p, len + 1);
                (*conn)++;
                (*nconn)--;
                /* should check that *nconn == 0 || **conn == ';' */
@@ -88,13 +86,12 @@ ODBCGetKeyAttr(const SQLCHAR **conn, SQL
                        (*nconn)--;
                }
                len = *conn - p;
-               *attr = (char *) malloc(len + 1);
+               *attr = strndup((const char *) p, len);
                if (*attr == NULL) {
                        free(*key);
                        *key = NULL;
                        return -1;
                }
-               strtcpy(*attr, (char *) p, len + 1);
        }
        if (*nconn > 0 && **conn) {
                (*conn)++;
diff --git a/clients/odbc/driver/SQLGetDiagField.c 
b/clients/odbc/driver/SQLGetDiagField.c
--- a/clients/odbc/driver/SQLGetDiagField.c
+++ b/clients/odbc/driver/SQLGetDiagField.c
@@ -36,7 +36,7 @@
                        size_t _l;                                      \
                        if (len < 0)                                    \
                                return SQL_ERROR;                       \
-                       _l = strlcpy((char *) buf, str, len);   \
+                       _l = strlcpy((char *) buf, str, len);           \
                        if (lenp)                                       \
                                *lenp = (SQLSMALLINT) _l;               \
                        if (buf == NULL || _l >= (size_t) len)          \
diff --git a/cmake/monetdb-defines.cmake b/cmake/monetdb-defines.cmake
--- a/cmake/monetdb-defines.cmake
+++ b/cmake/monetdb-defines.cmake
@@ -103,6 +103,8 @@ function(monetdb_configure_defines)
   check_function_exists("siglongjmp" HAVE_SIGLONGJMP)
   check_function_exists("stpcpy" HAVE_STPCPY)
   check_function_exists("strlcpy" HAVE_STRLCPY)
+  check_function_exists("strndup" HAVE_STRNDUP)
+  check_function_exists("strnlen" HAVE_STRNLEN)
   check_function_exists("strptime" HAVE_STRPTIME)
   check_symbol_exists("sysconf" "unistd.h" HAVE_SYSCONF)
   check_function_exists("task_info" HAVE_TASK_INFO)
diff --git a/common/utils/msabaoth.c b/common/utils/msabaoth.c
--- a/common/utils/msabaoth.c
+++ b/common/utils/msabaoth.c
@@ -1196,8 +1196,7 @@ msab_deserialise(sabdb **ret, const char
                                 "string does not contain dbname: %s", sdb);
                return(strdup(buf));
        }
-       dbname = malloc(lasts - sdb + 1);
-       strtcpy(dbname, sdb, lasts - sdb + 1);
+       dbname = strndup(sdb, lasts - sdb);
        sdb = ++lasts;
        lasts = strchr(sdb, ',');
        if (lasts == NULL) {
@@ -1206,8 +1205,7 @@ msab_deserialise(sabdb **ret, const char
                free(dbname);
                return(strdup(buf));
        }
-       uri = malloc(lasts - sdb + 1);
-       strtcpy(uri, sdb, lasts - sdb + 1);
+       uri = strndup(sdb, lasts - sdb);
        sdb = ++lasts;
        int locked, state, n;
        switch (sscanf(sdb, "%d,%d%n", &locked, &state, &n)) {
@@ -1239,8 +1237,7 @@ msab_deserialise(sabdb **ret, const char
                return(strdup(buf));
        }
        if (lasts > sdb) {
-               scens = malloc(lasts - sdb + 1);
-               strtcpy(scens, sdb, lasts - sdb + 1);
+               scens = strndup(sdb, lasts - sdb);
        } else {
                scens = NULL;
        }
diff --git a/common/utils/mstring.h b/common/utils/mstring.h
--- a/common/utils/mstring.h
+++ b/common/utils/mstring.h
@@ -51,6 +51,33 @@
  * stpe*: chainable with end-of-buffer pointer.
  */
 
+#ifndef HAVE_STRNLEN
+__attribute__((__nonnull__(1)))
+static inline size_t
+strnlen(const char *s, size_t n)
+{
+       for (size_t i = 0; i < n; i++)
+               if (s[i] == 0)
+                       return i;
+       return n;
+}
+#endif
+
+#ifndef HAVE_STRNDUP
+__attribute__((__nonnull__(1)))
+static inline char *
+strndup(const char *s, size_t n)
+{
+       n = strnlen(s, n);
+       char *p = malloc(n + 1);
+       if (p != NULL) {
+               memcpy(p, s, n);
+               p[n] = 0;
+       }
+       return p;
+}
+#endif
+
 #ifndef HAVE_STRLCPY
 /* Copy the input string into a destination string.  If the destination
  * buffer, limited by its size, isn't large enough to hold the copy, the
diff --git a/common/utils/mutils.c b/common/utils/mutils.c
--- a/common/utils/mutils.c
+++ b/common/utils/mutils.c
@@ -662,9 +662,9 @@ MT_getcwd(char *buffer, size_t size)
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to