ODBC SQLSetDescRec fix

Amir Shamsuddin <[email protected]>
Newsgroups gmane.comp.db.tds.freetds
Message-ID <[email protected]>
Hi,
I'm using freeTDS with sqlapi and using the odbc interface as recommended by
sqlapi to connect to a MSSQL server.

My program was crashing, so I tracked the issue back to a call made from
sqlapi to the freeTDS SQLSetDescRec method. It seems that sqlapi and freetds
disagree over the meaning of the nRecordNumber parameter, with sqlapi
starting indexing at 1 and freetds starting at 0, with the end result that
memory past the end of desc->records was being written to (and thus causing
the crash).

Looking at the MSDN ODBC reference
http://msdn.microsoft.com/en-us/library/ms714675(VS.85).aspx  it looks to me
that nRecordNumber should start indexing at 1, with an index of 0 being
reserved for the 'bookmark record', though I'm not sure what the bookmark
record is....

so I modified freeTDS to adjust the offset before accessing the
desc->records array (very simple patch attached) and this fixed my problem.

hopefully this is a real bug report / patch and is helpful to you,

regards,

Amir

PS I'm using freeTDS version 0.64, but the patch is against current CVS
which still appears to have the same problem

--
Amir Shamsuddin

_______________________________________________
FreeTDS mailing list
[email protected]
http://lists.ibiblio.org/mailman/listinfo/freetds
setDescRec.patch (application/octet-stream, 1.2 KB)
diff -ur freetds-cvs/src/odbc/odbc.c freetds-patched/src/odbc/odbc.c
--- freetds-cvs/src/odbc/odbc.c	2008-12-17 11:04:34.000000000 +0000
+++ freetds-patched/src/odbc/odbc.c	2009-02-23 14:30:09.646452900 +0000
@@ -2199,12 +2199,12 @@
 		ODBC_RETURN(desc, SQL_ERROR);
 	}
 
-	if (nRecordNumber > desc->header.sql_desc_count || nRecordNumber < 0) {
+	if (nRecordNumber > desc->header.sql_desc_count || nRecordNumber <= 0) {
 		odbc_errs_add(&desc->errs, "07009", NULL);
 		ODBC_RETURN(desc, SQL_ERROR);
 	}
 
-	drec = &desc->records[nRecordNumber];
+	drec = &desc->records[nRecordNumber - 1];
 
 	/* check for valid types and return "HY021" if not */
 	if (desc->type == DESC_IPD) {
@@ -2257,12 +2257,12 @@
 		ODBC_RETURN(desc, SQL_ERROR);
 	}
 
-	if (RecordNumber > desc->header.sql_desc_count || RecordNumber < 0) {
+	if (RecordNumber > desc->header.sql_desc_count || RecordNumber <= 0) {
 		odbc_errs_add(&desc->errs, "07009", NULL);
 		ODBC_RETURN(desc, SQL_ERROR);
 	}
 
-	drec = &desc->records[RecordNumber];
+	drec = &desc->records[RecordNumber - 1];
 
 	if ((rc = odbc_set_string(Name, BufferLength, StringLength, tds_dstr_cstr(&drec->sql_desc_name), -1)) != SQL_SUCCESS)
 		odbc_errs_add(&desc->errs, "01004", NULL);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.