pg.ddx.io pgsql-odbc@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Ludek Finstrle <luf@pzkagis.cz>
To: pgsql-odbc@postgresql.org
Subject: Re: Patch for snprintf problem (bug #1000650)
Date: Wed, 7 Jun 2006 22:15:16 +0200
Message-ID: <20060607201516.GA28714@soptik.pzkagis.cz> (raw )
In-Reply-To: <20060607173910.GA28221@soptik.pzkagis.cz >
References: <20060607173910.GA28221@soptik.pzkagis.cz >
> I solve it this way (main idea):
> snprintf(buf + strlen(buf), " append");
Ugh typo mistake:
snprintf(buf + strlen(buf), sizeof(buf), " append");
My hands are faster than my head :-(
There is buffer overrun erron becouse I have to decrease sizeof(buf) with
strlen(buf).
So I attach second try of this patch againist CVS HEAD.
Comments are welcome
Luf
Attachments:
psqlodbc_snprintf_2try.diff (text/plain, 10.7K) download
diff -c psqlodbc.orig\info.c psqlodbc\info.c
*** psqlodbc.orig\info.c Mon May 29 20:26:44 2006
--- psqlodbc\info.c Wed Jun 07 21:39:03 2006
***************
*** 1943,1961 ****
" 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);
else
{
if (escTableName)
! snprintf(columns_query, sizeof(columns_query), "%s and c.relname %s '%s'", columns_query, 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"
" on (not a.attisdropped) and a.attnum > 0");
if (search_by_ids)
! snprintf(columns_query, sizeof(columns_query), "%s and a.attnum = %d", columns_query, attnum);
else if (escColumnName)
! snprintf(columns_query, sizeof(columns_query), "%s and a.attname %s '%s'", columns_query, 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"
--- 1943,1962 ----
" inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace",
sizeof(columns_query));
if (search_by_ids)
! snprintf_add(columns_query, strlen(columns_query), sizeof(columns_query), " and c.oid = %u", reloid);
else
{
if (escTableName)
! snprintf_add(columns_query, strlen(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"
" on (not a.attisdropped) and a.attnum > 0");
+
if (search_by_ids)
! snprintf_add(columns_query, strlen(columns_query), sizeof(columns_query), " and a.attnum = %d", attnum);
else if (escColumnName)
! snprintf_add(columns_query, strlen(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"
***************
*** 1973,1982 ****
" 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);
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);
strcat(columns_query, " order by c.relname, attnum");
}
--- 1974,1983 ----
" and a.atttypid = t.oid and (a.attnum > 0)",
PG_VERSION_LE(conn, 6.2) ? "a.attlen" : "a.atttypmod");
if (escTableName)
! snprintf_add(columns_query, strlen(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_add(columns_query, strlen(columns_query), sizeof(columns_query), " and a.attname %s '%s'", like_or_eq, escColumnName);
strcat(columns_query, " order by c.relname, attnum");
}
***************
*** 2537,2543 ****
/* 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);
/* 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);
--- 2538,2544 ----
/* TableName cannot contain a string search pattern */
/* my_strcat(columns_query, " and c.relname = '%.*s'", szTableName, cbTableName); */
if (escTableName)
! snprintf_add(columns_query, strlen(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);
***************
*** 3217,3222 ****
--- 3218,3224 ----
char *escSchemaName = NULL, *escTableName = NULL, *escColumnName = NULL;
const char *like_or_eq;
char column_query[INFO_INQUIRY_LEN];
+ int cq_len;
BOOL search_pattern;
QResultClass *res;
***************
*** 3245,3259 ****
" table_name, column_name, grantor, grantee,"
" privilege_type as PRIVILEGE, is_grantable from"
" information_schema.column_privileges where true");
if (escSchemaName)
! snprintf(column_query, sizeof(column_query),
! "%s and table_schem = '%s'", column_query, escSchemaName);
if (escTableName)
! snprintf(column_query, sizeof(column_query),
! "%s and table_name = '%s'", column_query, escTableName);
if (escColumnName)
! snprintf(column_query, sizeof(column_query),
! "%s and column_name %s '%s'", column_query, 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);
--- 3247,3262 ----
" table_name, column_name, grantor, grantee,"
" privilege_type as PRIVILEGE, is_grantable from"
" information_schema.column_privileges where true");
+ cq_len = strlen(column_query);
if (escSchemaName)
! cq_len += snprintf_addlen(column_query, cq_len, sizeof(column_query),
! " and table_schem = '%s'", escSchemaName);
if (escTableName)
! cq_len += snprintf_addlen(column_query, cq_len, sizeof(column_query),
! " and table_name = '%s'", escTableName);
if (escColumnName)
! cq_len += snprintf_addlen(column_query, cq_len, sizeof(column_query),
! " 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);
***************
*** 4570,4576 ****
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);
strcat(proc_query, " order by nspname, proname, p.oid, attnum");
}
else
--- 4573,4579 ----
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_add(proc_query, strlen(proc_query), sizeof(proc_query), " and proname %s '%s'", like_or_eq, escProcName);
strcat(proc_query, " order by nspname, proname, p.oid, attnum");
}
else
***************
*** 4580,4586 ****
"(not proretset)");
ret_col = 5;
if (escProcName)
! snprintf(proc_query, sizeof(proc_query), " and proname %s '%s'", proc_query, 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))
--- 4583,4589 ----
"(not proretset)");
ret_col = 5;
if (escProcName)
! snprintf_add(proc_query, strlen(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))
***************
*** 5115,5121 ****
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);
if (conn->schema_support)
{
strcat(proc_query, " pg_namespace.oid = relnamespace and relkind in ('r', 'v') and");
--- 5118,5124 ----
schema_strcat1(proc_query, " nspname %s '%.*s' and", like_or_eq, escSchemaName, SQL_NTS, szTableName, cbTableName, conn);
}
if (escTableName)
! snprintf_add(proc_query, strlen(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 -c psqlodbc.orig\misc.c psqlodbc\misc.c
*** psqlodbc.orig\misc.c Mon May 29 20:26:44 2006
--- psqlodbc\misc.c Wed Jun 07 21:56:09 2006
***************
*** 291,293 ****
--- 291,328 ----
}
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 pos, size_t size, const char *format, ...)
+ {
+ int len;
+ 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_add
+ * It returns strlen of buf every time (not -1 when truncated)
+ */
+
+ int
+ snprintf_addlen(char *buf, size_t pos, size_t size, const char *format, ...)
+ {
+ int len;
+ va_list arglist;
+
+ va_start(arglist, format);
+ if ((len = vsnprintf(buf + pos, size - pos, format, arglist)) < 0)
+ len = size;
+ va_end(arglist);
+ return len;
+ }
diff -c psqlodbc.orig\misc.h psqlodbc\misc.h
*** psqlodbc.orig\misc.h Mon May 29 20:26:44 2006
--- psqlodbc\misc.h Wed Jun 07 21:40:25 2006
***************
*** 132,137 ****
--- 132,139 ----
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 pos, size_t size, const char *format, ...);
+ int snprintf_addlen(char *buf, size_t pos, size_t size, const char *format, ...);
/* #define GET_SCHEMA_NAME(nspname) (stricmp(nspname, "public") ? nspname : "") */
#define GET_SCHEMA_NAME(nspname) (nspname)
diff -c psqlodbc.orig\psqlodbc.h psqlodbc\psqlodbc.h
*** psqlodbc.orig\psqlodbc.h Mon May 29 20:28:38 2006
--- psqlodbc\psqlodbc.h Wed Jun 07 18:15:47 2006
***************
*** 110,115 ****
--- 110,116 ----
#define strnicmp _strnicmp
#define stricmp _stricmp
#define strdup _strdup
+ #define vsnprintf _vsnprintf
#endif
#ifndef SQL_ATTR_APP_ROW_DESC
view thread (5+ messages) latest in thread
Message-ID: <20060607201516.GA28714@soptik.pzkagis.cz>
Permalink: ../20060607201516.GA28714@soptik.pzkagis.cz/
Also on: postgresql.org/message-id/20060607201516.GA28714@soptik.pzkagis.cz
copy link · copy postgr.es · copy git am
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-odbc@postgresql.org
Cc: luf@pzkagis.cz
Subject: Re: Patch for snprintf problem (bug #1000650)
In-Reply-To: <20060607201516.GA28714@soptik.pzkagis.cz>
References: <20060607173910.GA28221@soptik.pzkagis.cz> <20060607201516.GA28714@soptik.pzkagis.cz>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox