Removed the use of misused strcat_s together with snprintf_s (bug report from
authorHiroshi Inoue <inoue@tpf.co.jp>
Sun, 24 Jan 2010 07:56:13 +0000 (07:56 +0000)
committerHiroshi Inoue <inoue@tpf.co.jp>
Sun, 24 Jan 2010 07:56:13 +0000 (07:56 +0000)
Jap-Peter Seifert) and use strlcat instead of strncat.

configure.ac
connection.c
info.c
misc.c
misc.h
psqlodbc.h
qresult.c
version.h

index 083e0c2e662d43c1c8a64382030e7425f3937dab..dbca4a7f24fb856fb703bed2651dd7e8adc9ef3e 100644 (file)
@@ -274,7 +274,7 @@ AC_C_CONST
 # 7. Functions, global variables
 
 AC_FUNC_STRERROR_R
-AC_CHECK_FUNCS(strtoul strtoll)
+AC_CHECK_FUNCS(strtoul strtoll strlcpy)
 
 if test x"$enable_unicode" = xyes; then
   AC_CHECK_FUNCS(iswascii)
index 53c15557f48d1e9ddf7b552444b1a96b22908c66..924632d55e9a5359b5f1648291bcf626545d7799 100644 (file)
@@ -824,43 +824,24 @@ inolog("new_format=%d\n", new_format);
    truncated = SOCK_get_string(sock, new_format ? msgbuffer : msgbuf, new_format ? sizeof(msgbuffer) : buflen);
    if (new_format)
    {
-       size_t  msgl;
+       size_t  dstlen = 0;
 
        msgbuf[0] = '\0';
        for (;msgbuffer[0];)
        {
            mylog("%s: 'E' - %s\n", comment, msgbuffer);
            qlog("ERROR from backend during %s: '%s'\n", comment, msgbuffer);
-           msgl = strlen(msgbuffer + 1);
            switch (msgbuffer[0])
            {
                case 'S':
-                   if (buflen > 0)
-                   {
-                       strncat(msgbuf, msgbuffer + 1, buflen);
-                       buflen -= msgl;
-                   }
-                   if (buflen > 0)
-                   {
-                       strncat(msgbuf, ": ", buflen);
-                       buflen -= 2;
-                   }
+                   strlcat(msgbuf, msgbuffer + 1, buflen);
+                   dstlen = strlcat(msgbuf, ": ", buflen);
                    break;
                case 'M':
                case 'D':
-                   if (buflen > 0)
-                   {
-                       if (hasmsg)
-                       {
-                           strcat(msgbuf, "\n");
-                           buflen--;
-                       }
-                       if (buflen > 0)
-                       {
-                           strncat(msgbuf, msgbuffer + 1, buflen);
-                           buflen -= msgl;
-                       }
-                   }
+                   if (hasmsg)
+                       strlcat(msgbuf, "\n", buflen);
+                   dstlen = strlcat(msgbuf, msgbuffer + 1, buflen);
                    if (truncated)
                        msg_truncated = truncated;
                    hasmsg = TRUE;
@@ -870,8 +851,6 @@ inolog("new_format=%d\n", new_format);
                        strncpy_null(sqlstate, msgbuffer + 1, 8);
                    break;
            }
-           if (buflen < 0)
-               buflen = 0;
            while (truncated)
                truncated = SOCK_get_string(sock, msgbuffer, sizeof(msgbuffer));
            truncated = SOCK_get_string(sock, msgbuffer, sizeof(msgbuffer));
@@ -929,7 +908,7 @@ handle_notice_message(ConnectionClass *self, char *msgbuf, size_t buflen, char *
 
    if (new_format)
    {
-       size_t  msgl;
+       size_t  dstlen = 0;
 
        msgbuf[0] = '\0';
        for (;;)
@@ -940,38 +919,17 @@ handle_notice_message(ConnectionClass *self, char *msgbuf, size_t buflen, char *
 
            mylog("%s: 'N' - %s\n", comment, msgbuffer);
            qlog("NOTICE from backend during %s: '%s'\n", comment, msgbuffer);
-           msgl = strlen(msgbuffer + 1);
            switch (msgbuffer[0])
            {
                case 'S':
-                   if (buflen > 0)
-                   {
-                       strncat(msgbuf, msgbuffer + 1, buflen);
-                       buflen -= msgl;
-                   }
-                   if (buflen > 0)
-                   {
-                       strncat(msgbuf, ": ", buflen);
-                       buflen -= 2;
-                   }
+                   strlcat(msgbuf, msgbuffer + 1, buflen);
+                   dstlen = strlcat(msgbuf, ": ", buflen);
                    break;
                case 'M':
                case 'D':
-                   if (buflen > 0)
-                   {
-                       if (hasmsg)
-                       {
-                           strcat(msgbuf, "\n");
-                           buflen--;
-                       }
-                       if (buflen > 0)
-                       {
-                           strncat(msgbuf, msgbuffer + 1, buflen);
-                           buflen -= msgl;
-                       }
-                   }
-                   else
-                       msg_truncated = TRUE;
+                   if (hasmsg)
+                       strlcat(msgbuf, "\n", buflen);
+                   dstlen = strlcat(msgbuf, msgbuffer + 1, buflen);
                    if (truncated)
                        msg_truncated = truncated;
                    hasmsg = TRUE;
@@ -981,7 +939,7 @@ handle_notice_message(ConnectionClass *self, char *msgbuf, size_t buflen, char *
                        strncpy_null(sqlstate, msgbuffer + 1, 8);
                    break;
            }
-           if (buflen < 0)
+           if (dstlen >= buflen)
                msg_truncated = TRUE;
            while (truncated)
                truncated = SOCK_get_string(sock, msgbuffer, sizeof(msgbuffer));
diff --git a/info.c b/info.c
index 2c71551c73d00079aad26e469aae7762dd1e8cd2..8ef8fe54910bfba6639b17a5b07b17a42c66ec3a 100644 (file)
--- a/info.c
+++ b/info.c
@@ -3713,7 +3713,7 @@ retry_public_schema:
                        " where tc.oid = " FORMAT_UINT4
                        , reloid);
 
-                   strncat(tables_query,
+                   strlcat(tables_query,
                        " AND tc.oid = i.indrelid"
                        " AND n.oid = tc.relnamespace"
                        " AND i.indisprimary = 't'"
@@ -3743,7 +3743,7 @@ retry_public_schema:
                        snprintf(tbqry, tsize,
                        " where tc.oid = " FORMAT_UINT4, reloid);
                        
-                   strncat(tables_query,
+                   strlcat(tables_query,
                        " AND tc.oid = i.indrelid"
                        " AND i.indisprimary = 't'"
                        " AND ia.attrelid = i.indexrelid"
diff --git a/misc.c b/misc.c
index a226d75475fac493726c29ff5f5e84d414aff218..71b94a0a7d5d6c033879753ab3b82051426c45df 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -330,3 +330,30 @@ snprintf_len(char *buf, size_t size, const char *format, ...)
    va_end(arglist);
    return len;
 }
+
+#ifndef    HAVE_STRLCAT
+size_t
+strlcat(char *dst, const char *src, size_t size)
+{
+   size_t ttllen;
+   char    *pd = dst;
+   const char *ps= src;
+   
+   for (ttllen = 0; ttllen < size; ttllen++, pd++)
+   {
+       if (0 == *pd)
+           break; 
+   }
+   if (ttllen >= size - 1)
+       return ttllen + strlen(src);
+   for (; ttllen < size - 1; ttllen++, pd++, ps++)
+   {
+       if (0 == (*pd = *ps))
+           return ttllen;
+   }
+   *pd = 0;
+   for (; *ps; ttllen++, ps++)
+       ;
+   return ttllen;
+}
+#endif /* HAVE_STRLCAT */
diff --git a/misc.h b/misc.h
index 46d042354831183666798320371853c27c5cd696..c4914093bb199dc69c25606ba4ebd3b6c2bba943 100644 (file)
--- a/misc.h
+++ b/misc.h
@@ -128,6 +128,9 @@ void    FinalizeLogging();
 
 void       remove_newlines(char *string);
 char      *strncpy_null(char *dst, const char *src, ssize_t len);
+#ifndef    HAVE_STRLCPY
+size_t     strlcat(char *, const char *, size_t);
+#endif /* HAVE_STRLCPY */
 char      *my_trim(char *string);
 char      *make_string(const char *s, ssize_t len, char *buf, size_t bufsize);
 char      *make_lstring_ifneeded(ConnectionClass *, const char *s, ssize_t len, BOOL);
index 1c806a34f8fba580f09835e6edf9681ed43d9fed..6b4314a3a811772b7f9006c0da77bf8ff1b96895 100644 (file)
@@ -5,7 +5,7 @@
  *
  * Comments:       See "notice.txt" for copyright and license information.
  *
- * $Id: psqlodbc.h,v 1.133 2010/01/17 13:10:41 hinoue Exp $
+ * $Id: psqlodbc.h,v 1.134 2010/01/24 07:56:13 hinoue Exp $
  *
  */
 
@@ -190,12 +190,7 @@ typedef double SDOUBLE;
 #define    FALSE   (BOOL)0
 #endif /* FALSE */
 #else
-#if (_MSC_VER >= 1400) && !defined(_WIN64)
-#define snprintf sprintf_s
-#define strncat(d, s, l) strcat_s(d, l, s)
-#else
 #define snprintf _snprintf
-#endif
 #ifndef strdup
 #define strdup _strdup
 #endif /* strdup */
index 3d86cfee7f9c17b3a67eacb73c05ca8254ee4caa..01c24db92328c27a80697454f499bb98843b71e4 100644 (file)
--- a/qresult.c
+++ b/qresult.c
@@ -644,7 +644,7 @@ QR_close(QResultClass *self)
                CC_cursor_count(conn) <= 1)
            {
                mylog("QResult: END transaction on conn=%p\n", conn);
-               strncat(buf, ";commit", sizeof(buf));
+               strlcat(buf, ";commit", sizeof(buf));
                flag |= END_WITH_COMMIT;
                QR_set_cursor(self, NULL);
            }
index 845fd7493978f57b135e6adc948fc658e75efc0c..25c78d7a89d3f63656643d7da6f21886245eb161 100644 (file)
--- a/version.h
+++ b/version.h
@@ -12,6 +12,6 @@
 #define POSTGRESDRIVERVERSION      "08.04.0201"
 #define POSTGRES_RESOURCE_VERSION  "08.04.0201\0"
 #define PG_DRVFILE_VERSION     8,4,02,01
-#define PG_BUILD_VERSION       "201001230001"
+#define PG_BUILD_VERSION       "201001240001"
 
 #endif