fix for linux snprintf issue - snprintf(buf,bufsize,"%s append",buf)
authorLudek Finstrle <luf@pzkagis.cz>
Thu, 15 Jun 2006 06:58:49 +0000 (06:58 +0000)
committerLudek Finstrle <luf@pzkagis.cz>
Thu, 15 Jun 2006 06:58:49 +0000 (06:58 +0000)
connection.c
info.c
misc.c
misc.h
psqlodbc.h
results.c

index 05e1298ded4c92adde6f67048cec8db0445d6899..8118d8baa5ca824c4a1fd2ce0a034b4d6481d2e1 100644 (file)
@@ -2318,7 +2318,7 @@ cleanup:
        {
            if (CC_is_in_error_trans(self))
                snprintf(cmd, sizeof(cmd), "ROLLBACK TO %s;", per_query_svp);
-           snprintf(cmd, sizeof(cmd), "%sRELEASE %s", cmd, per_query_svp);
+           snprintf_add(cmd, sizeof(cmd), "RELEASE %s", per_query_svp);
        }
        else if (CC_is_in_error_trans(self))
            strcpy(cmd, "ROLLBACK");
diff --git a/info.c b/info.c
index a77cb2fd6c2c49902307f499991ed0a6891e7ef7..49a3f6469a0dd8797f53838c622d3d0836d089ad 100644 (file)
--- a/info.c
+++ b/info.c
@@ -1955,11 +1955,11 @@ retry_public_schema:
            " inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace",
            sizeof(columns_query));
        if (search_by_ids)
-           snprintf(columns_query, sizeof(columns_query), "%s and c.oid = %u", columns_query, reloid);
+           snprintf_add(columns_query, sizeof(columns_query), " and c.oid = %u", reloid);
        else
        {
            if (escTableName)
-               snprintf(columns_query, sizeof(columns_query), "%s and c.relname %s '%s'", columns_query, like_or_eq, escTableName);
+               snprintf_add(columns_query, sizeof(columns_query), " and c.relname %s '%s'", like_or_eq, escTableName);
            schema_strcat1(columns_query, " and n.nspname %s '%.*s'", like_or_eq, escSchemaName, SQL_NTS, szTableName, cbTableName, conn);
        }
        strcat(columns_query, ") inner join pg_catalog.pg_attribute a"
@@ -1969,10 +1969,10 @@ retry_public_schema:
        if (search_by_ids)
        {
            if (attnum != 0)
-               snprintf(columns_query, sizeof(columns_query), "%s and a.attnum = %d", columns_query, attnum);
+               snprintf_add(columns_query, sizeof(columns_query), " and a.attnum = %d", attnum);
        }
        else if (escColumnName)
-           snprintf(columns_query, sizeof(columns_query), "%s and a.attname %s '%s'", columns_query, like_or_eq, escColumnName);
+           snprintf_add(columns_query, sizeof(columns_query), " and a.attname %s '%s'", like_or_eq, escColumnName);
        strcat(columns_query,
            " and a.attrelid = c.oid) inner join pg_catalog.pg_type t"
            " on t.oid = a.atttypid) left outer join pg_attrdef d"
@@ -1990,10 +1990,10 @@ retry_public_schema:
            "  and a.atttypid = t.oid and (a.attnum > 0)",
            PG_VERSION_LE(conn, 6.2) ? "a.attlen" : "a.atttypmod");
        if (escTableName)
-           snprintf(columns_query, sizeof(columns_query), "%s and c.relname %s '%s'", columns_query, like_or_eq, escTableName);
+           snprintf_add(columns_query, sizeof(columns_query), " and c.relname %s '%s'", like_or_eq, escTableName);
        my_strcat1(columns_query, " and u.usename %s '%.*s'", like_or_eq, escSchemaName, SQL_NTS);
        if (escColumnName)
-           snprintf(columns_query, sizeof(columns_query), "%s and a.attname %s '%s'", columns_query, like_or_eq, escColumnName);
+           snprintf_add(columns_query, sizeof(columns_query), " and a.attname %s '%s'", like_or_eq, escColumnName);
        strcat(columns_query, " order by c.relname, attnum");
    }
 
@@ -2566,7 +2566,7 @@ retry_public_schema:
    /* TableName cannot contain a string search pattern */
    /* my_strcat(columns_query, " and c.relname = '%.*s'", szTableName, cbTableName); */
    if (escTableName)
-       snprintf(columns_query, sizeof(columns_query), "%s and c.relname = '%s'", columns_query, escTableName);
+       snprintf_add(columns_query, sizeof(columns_query), " and c.relname = '%s'", escTableName);
    /* SchemaName cannot contain a string search pattern */
    if (conn->schema_support)
        schema_strcat(columns_query, " and u.nspname = '%.*s'", escSchemaName, SQL_NTS, szTableName, cbTableName, conn);
@@ -3246,6 +3246,8 @@ PGAPI_ColumnPrivileges(
    char    *escSchemaName = NULL, *escTableName = NULL, *escColumnName = NULL;
    const char  *like_or_eq;
    char    column_query[INFO_INQUIRY_LEN];
+   int     cq_len,cq_size;
+   char        *col_query;
    BOOL    search_pattern;
    QResultClass    *res;
 
@@ -3274,15 +3276,31 @@ PGAPI_ColumnPrivileges(
            " table_name, column_name, grantor, grantee,"
            " privilege_type as PRIVILEGE, is_grantable from"
            " information_schema.column_privileges where true");
+   cq_len = strlen(column_query);
+   cq_size = sizeof(column_query);
+   col_query = column_query;
    if (escSchemaName)
-       snprintf(column_query, sizeof(column_query),
-           "%s and table_schem = '%s'", column_query, escSchemaName);  
+   {
+       col_query += cq_len;
+       cq_size -= cq_len;
+       cq_len = snprintf_len(col_query, cq_size,
+           " and table_schem = '%s'", escSchemaName);  
+       
+   }
    if (escTableName)
-       snprintf(column_query, sizeof(column_query),
-           "%s and table_name = '%s'", column_query, escTableName);  
+   {
+       col_query += cq_len;
+       cq_size -= cq_len;
+       cq_len += snprintf_len(col_query, cq_size,
+           " and table_name = '%s'", escTableName);  
+   }
    if (escColumnName)
-       snprintf(column_query, sizeof(column_query),
-           "%s and column_name %s '%s'", column_query, like_or_eq, escColumnName);
+   {
+       col_query += cq_len;
+       cq_size -= cq_len;
+       cq_len += snprintf_len(col_query, cq_size,
+           " and column_name %s '%s'", like_or_eq, escColumnName);
+   }
    if (res = CC_send_query(conn, column_query, NULL, IGNORE_ABORT_ON_CONN, stmt), !QR_command_maybe_successful(res))
    {
        SC_set_error(stmt, STMT_EXEC_ERROR, "PGAPI_ColumnPrivileges query error", func);
@@ -4599,7 +4617,7 @@ PGAPI_ProcedureColumns(
        strcat(proc_query, " has_function_privilege(p.oid, 'EXECUTE')");
        my_strcat1(proc_query, " and nspname %s '%.*s'", like_or_eq, escSchemaName, SQL_NTS);
        if (escProcName)
-           snprintf(proc_query, sizeof(proc_query), "%s and proname %s '%s'", proc_query, like_or_eq, escProcName);
+           snprintf_add(proc_query, sizeof(proc_query), " and proname %s '%s'", like_or_eq, escProcName);
        strcat(proc_query, " order by nspname, proname, p.oid, attnum");
    }
    else
@@ -4609,7 +4627,7 @@ PGAPI_ProcedureColumns(
                "(not proretset)");
        ret_col = 5;
        if (escProcName)
-           snprintf(proc_query, sizeof(proc_query), " and proname %s '%s'", proc_query, like_or_eq, escProcName);
+           snprintf_add(proc_query, sizeof(proc_query), " and proname %s '%s'", like_or_eq, escProcName);
        strcat(proc_query, " order by proname, proretset");
    }
    if (tres = CC_send_query(conn, proc_query, NULL, IGNORE_ABORT_ON_CONN, stmt), !QR_command_maybe_successful(tres))
@@ -5144,7 +5162,7 @@ retry_public_schema:
            schema_strcat1(proc_query, " nspname %s '%.*s' and", like_or_eq, escSchemaName, SQL_NTS, szTableName, cbTableName, conn);
    }
    if (escTableName)
-       snprintf(proc_query, sizeof(proc_query), "%s relname %s '%s' and", proc_query, like_or_eq, escTableName);
+       snprintf_add(proc_query, sizeof(proc_query), " relname %s '%s' and", like_or_eq, escTableName);
    if (conn->schema_support)
    {
        strcat(proc_query, " pg_namespace.oid = relnamespace and relkind in ('r', 'v') and");
diff --git a/misc.c b/misc.c
index 4baf4980eb3fd5c96af0369a0f39d54fe7b42a75..328da25af06f4a1c03073f00d241e09ced3da5db 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -291,3 +291,39 @@ schema_strcat1(char *buf, const char *fmt, const char *s1, const char *s, int le
    }
    return my_strcat1(buf, fmt, s1, s, len);
 }
+
+/*
+ * snprintf_add is a extension to snprintf
+ * It add format to buf at given pos
+ */
+
+int
+snprintf_add(char *buf, size_t size, const char *format, ...)
+{
+   int len;
+   size_t pos = strlen(buf);
+   va_list arglist;
+   
+   va_start(arglist, format);
+   len = vsnprintf(buf + pos, size - pos, format, arglist);
+   va_end(arglist);
+   return len;
+}
+
+/*
+ * snprintf_addlen is a extension to snprintf
+ * It returns strlen of buf every time (not -1 when truncated)
+ */
+
+int
+snprintf_len(char *buf, size_t size, const char *format, ...)
+{
+   int len;
+   va_list arglist;
+   
+   va_start(arglist, format);
+   if ((len = vsnprintf(buf, size, format, arglist)) < 0)
+       len = size;
+   va_end(arglist);
+   return len;
+}
diff --git a/misc.h b/misc.h
index 67cbfff84cb3a2cb28d3f3c79ccde3196c077a72..eecf0549de789124c8b58466b01eb3263301aee0 100644 (file)
--- a/misc.h
+++ b/misc.h
@@ -132,6 +132,8 @@ char       *my_strcat1(char *buf, const char *fmt, const char *s1, const char *s, i
 char      *schema_strcat1(char *buf, const char *fmt, const char *s1,
                const char *s, int len,
                const char *, int, ConnectionClass *conn);
+int            snprintf_add(char *buf, size_t size, const char *format, ...);
+int            snprintf_len(char *buf, size_t size, const char *format, ...);
 /* #define GET_SCHEMA_NAME(nspname)    (stricmp(nspname, "public") ? nspname : "") */
 #define    GET_SCHEMA_NAME(nspname)    (nspname)
 
index bf9ad0eed4e428dda6c0478e333098f5f5fe1009..96b2f6e94e55c56f9ce7005d8f24849e39e50b42 100644 (file)
@@ -5,7 +5,7 @@
  *
  * Comments:       See "notice.txt" for copyright and license information.
  *
- * $Id: psqlodbc.h,v 1.101 2006/06/12 15:21:45 hinoue Exp $
+ * $Id: psqlodbc.h,v 1.102 2006/06/15 06:58:49 luf Exp $
  *
  */
 
@@ -109,6 +109,7 @@ typedef double SDOUBLE;
 #define strdup _strdup
 #define strnicmp _strnicmp
 #define stricmp _stricmp
+#define vsnprintf _vsnprintf
 #endif
 
 #ifndef    SQL_ATTR_APP_ROW_DESC
index 3c4e322f01881bb88007ee35dbb0df1cffdd0b71..fdff099fa48933ecee9433177137dc5f309f0fdf 100644 (file)
--- a/results.c
+++ b/results.c
@@ -3025,11 +3025,9 @@ inolog("%s bestitem=%s bestqual=%s\n", func, SAFE_NAME(ti->bestitem), SAFE_NAME(
        snprintf(selstr, len, "%s where ctid = currtid(0, '(,)') %s", stmt->load_statement, oideqstr);
    else if (bestitem && oidint)
    {
-       Int4    slen;
        /*snprintf(selstr, len, "%s where \"%s\" = %u", stmt->load_statement, bestitem, *oid);*/
        snprintf(selstr, len, "%s where ", stmt->load_statement);
-       slen = strlen(selstr);
-       snprintf(selstr + slen, len - slen, bestqual, *oidint);
+       snprintf_add(selstr, len, bestqual, *oidint);
    }
    else
    {